Skip to content

Commit 71a9808

Browse files
committed
added GitVersion.MsBuild as an improvement over GitVersionTask
1 parent 7e8d067 commit 71a9808

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+2173
-7
lines changed

build.cake

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,24 @@ Task("Copy-Files")
234234
MSBuildSettings = parameters.MSBuildSettings
235235
});
236236

237+
DotNetCorePublish("./src/GitVersion.MsBuild/GitVersion.MsBuild.csproj", new DotNetCorePublishSettings
238+
{
239+
Framework = parameters.FullFxVersion,
240+
NoBuild = true,
241+
NoRestore = true,
242+
Configuration = parameters.Configuration,
243+
MSBuildSettings = parameters.MSBuildSettings
244+
});
245+
246+
DotNetCorePublish("./src/GitVersion.MsBuild/GitVersion.MsBuild.csproj", new DotNetCorePublishSettings
247+
{
248+
Framework = parameters.CoreFxVersion,
249+
NoBuild = true,
250+
NoRestore = true,
251+
Configuration = parameters.Configuration,
252+
MSBuildSettings = parameters.MSBuildSettings
253+
});
254+
237255
var ilMergeDir = parameters.Paths.Directories.ArtifactsBinFullFxILMerge;
238256
var portableDir = parameters.Paths.Directories.ArtifactsBinFullFxPortable.Combine("tools");
239257
var cmdlineDir = parameters.Paths.Directories.ArtifactsBinFullFxCmdline.Combine("tools");
@@ -358,6 +376,7 @@ Task("Pack-Nuget")
358376
};
359377

360378
// GitVersionCore, GitVersionTask, & global tool
379+
DotNetCorePack("./src/GitVersion.MsBuild", settings);
361380
DotNetCorePack("./src/GitVersionCore", settings);
362381
DotNetCorePack("./src/GitVersionTask", settings);
363382
DotNetCorePack("./src/GitVersionExe/GitVersion.Tool.csproj", settings);

build/parameters.cake

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public class BuildParameters
9898
Packages = BuildPackages.GetPackages(
9999
Paths.Directories.NugetRoot,
100100
Version.SemVersion,
101-
new [] { "GitVersion.CommandLine.DotNetCore", "GitVersion.CommandLine", "GitVersionCore", "GitVersionTask", "GitVersion.Tool" },
101+
new [] { "GitVersion.CommandLine.DotNetCore", "GitVersion.CommandLine", "GitVersionCore", "GitVersionTask", "GitVersion.MsBuild", "GitVersion.Tool" },
102102
new [] { "GitVersion.Portable" });
103103

104104
var files = Paths.Files;
@@ -116,8 +116,7 @@ public class BuildParameters
116116
{
117117
["GitVersion.CommandLine.DotNetCore"] = Paths.Directories.ArtifactsBinCoreFx,
118118
["GitVersion.CommandLine"] = Paths.Directories.ArtifactsBinFullFxCmdline,
119-
["GitVersion.Portable"] = Paths.Directories.ArtifactsBinFullFxPortable,
120-
["GitVersion.Tool"] = Paths.Directories.ArtifactsBinCoreFx,
119+
["GitVersion.Portable"] = Paths.Directories.ArtifactsBinFullFxPortable
121120
};
122121

123122
Credentials = BuildCredentials.GetCredentials(context);

src/Directory.Build.props

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
</PropertyGroup>
1616
<PropertyGroup>
1717
<PackageVersion_LibGit2Sharp>0.26.0</PackageVersion_LibGit2Sharp>
18-
<PackageVersion_LibGit2SharpNativeBinaries>[2.0.267]</PackageVersion_LibGit2SharpNativeBinaries>
1918
<PackageVersion_JetBrainsAnnotations>2019.1.1</PackageVersion_JetBrainsAnnotations>
2019
<PackageVersion_YamlDotNet>6.0.0</PackageVersion_YamlDotNet>
2120
</PropertyGroup>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.IO;
3+
4+
public static class AssemblyLocation
5+
{
6+
public static string CurrentDirectory()
7+
{
8+
var assembly = typeof(AssemblyLocation).Assembly;
9+
var uri = new UriBuilder(assembly.CodeBase);
10+
var path = Uri.UnescapeDataString(uri.Path);
11+
12+
return Path.GetDirectoryName(path);
13+
}
14+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using NUnit.Framework;
2+
3+
[assembly: Parallelizable(ParallelScope.Fixtures)]
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System.Linq;
2+
using GitVersion;
3+
using GitVersion.MsBuild.Task;
4+
using GitVersionTask;
5+
using Microsoft.Build.Framework;
6+
using NUnit.Framework;
7+
using Shouldly;
8+
9+
[TestFixture]
10+
public class GetVersionTaskTests : TestBase
11+
{
12+
[Test]
13+
public void OutputsShouldMatchVariableProvider()
14+
{
15+
var taskProperties = typeof(GetVersion)
16+
.GetProperties()
17+
.Where(p => p.GetCustomAttributes(typeof(OutputAttribute), false).Any())
18+
.Select(p => p.Name);
19+
20+
var variablesProperties = VersionVariables.AvailableVariables;
21+
22+
taskProperties.ShouldBe(variablesProperties, ignoreOrder: true);
23+
}
24+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
<PropertyGroup>
3+
<TargetFramework>net461</TargetFramework>
4+
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
5+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
6+
</PropertyGroup>
7+
<ItemGroup>
8+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.0.1" />
9+
<PackageReference Include="FluentDateTime" Version="1.15.0"></PackageReference>
10+
<PackageReference Include="JetBrains.Annotations" Version="$(PackageVersion_JetBrainsAnnotations)"></PackageReference>
11+
<PackageReference Include="Newtonsoft.Json" Version="12.0.1"></PackageReference>
12+
<PackageReference Include="NSubstitute" Version="4.0.0"></PackageReference>
13+
<PackageReference Include="coverlet.msbuild" Version="2.6.0">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="NUnit" Version="3.11.0"></PackageReference>
18+
<packagereference Include="NUnit3TestAdapter" Version="3.13.0"></packagereference>
19+
<PackageReference Include="Shouldly" Version="3.0.2"></PackageReference>
20+
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="16.0.461" />
21+
</ItemGroup>
22+
<ItemGroup>
23+
<Content Include="Approved\**\*.txt" />
24+
</ItemGroup>
25+
<ItemGroup>
26+
<ProjectReference Include="..\GitVersion.MsBuild\GitVersion.MsBuild.csproj" />
27+
</ItemGroup>
28+
<ItemGroup>
29+
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
30+
</ItemGroup>
31+
32+
</Project>
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.IO;
3+
4+
using GitVersion;
5+
6+
using LibGit2Sharp;
7+
8+
using NUnit.Framework;
9+
10+
[TestFixture]
11+
public class GitVersionTaskDirectoryTests : TestBase
12+
{
13+
ExecuteCore executeCore;
14+
string gitDirectory;
15+
string workDirectory;
16+
17+
18+
[SetUp]
19+
public void CreateTemporaryRepository()
20+
{
21+
workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
22+
gitDirectory = Repository.Init(workDirectory)
23+
.TrimEnd(Path.DirectorySeparatorChar);
24+
executeCore = new ExecuteCore(new TestFileSystem());
25+
Assert.NotNull(gitDirectory);
26+
}
27+
28+
29+
[TearDown]
30+
public void Cleanup()
31+
{
32+
Directory.Delete(workDirectory, true);
33+
}
34+
35+
36+
[Test]
37+
public void Finds_GitDirectory()
38+
{
39+
try
40+
{
41+
executeCore.ExecuteGitVersion(null, null, null, null, true, workDirectory, null);
42+
}
43+
catch (Exception ex)
44+
{
45+
// `RepositoryNotFoundException` means that it couldn't find the .git directory,
46+
// any other exception means that the .git was found but there was some other issue that this test doesn't care about.
47+
Assert.IsNotAssignableFrom<RepositoryNotFoundException>(ex);
48+
}
49+
}
50+
51+
52+
[Test]
53+
public void Finds_GitDirectory_In_Parent()
54+
{
55+
var childDir = Path.Combine(workDirectory, "child");
56+
Directory.CreateDirectory(childDir);
57+
58+
try
59+
{
60+
executeCore.ExecuteGitVersion(null, null, null, null, true, childDir, null);
61+
}
62+
catch (Exception ex)
63+
{
64+
// TODO I think this test is wrong.. It throws a different exception
65+
// `RepositoryNotFoundException` means that it couldn't find the .git directory,
66+
// any other exception means that the .git was found but there was some other issue that this test doesn't care about.
67+
Assert.IsNotAssignableFrom<RepositoryNotFoundException>(ex);
68+
}
69+
}
70+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.IO;
5+
6+
public static class DirectoryHelper
7+
{
8+
static Dictionary<string, string> toRename = new Dictionary<string, string>
9+
{
10+
{"gitted", ".git"},
11+
{"gitmodules", ".gitmodules"},
12+
};
13+
14+
public static void CopyFilesRecursively(DirectoryInfo source, DirectoryInfo target)
15+
{
16+
// From http://stackoverflow.com/questions/58744/best-way-to-copy-the-entire-contents-of-a-directory-in-c/58779#58779
17+
18+
foreach (var dir in source.GetDirectories())
19+
{
20+
CopyFilesRecursively(dir, target.CreateSubdirectory(Rename(dir.Name)));
21+
}
22+
foreach (var file in source.GetFiles())
23+
{
24+
file.CopyTo(Path.Combine(target.FullName, Rename(file.Name)));
25+
}
26+
}
27+
28+
static string Rename(string name)
29+
{
30+
return toRename.ContainsKey(name) ? toRename[name] : name;
31+
}
32+
33+
public static void DeleteSubDirectories(string parentPath)
34+
{
35+
var dirs = Directory.GetDirectories(parentPath);
36+
foreach (var dir in dirs)
37+
{
38+
DeleteDirectory(dir);
39+
}
40+
}
41+
42+
public static void DeleteDirectory(string directoryPath)
43+
{
44+
// From http://stackoverflow.com/questions/329355/cannot-delete-directory-with-directory-deletepath-true/329502#329502
45+
46+
if (!Directory.Exists(directoryPath))
47+
{
48+
Trace.WriteLine(
49+
string.Format("Directory '{0}' is missing and can't be removed.",
50+
directoryPath));
51+
52+
return;
53+
}
54+
55+
var files = Directory.GetFiles(directoryPath);
56+
var dirs = Directory.GetDirectories(directoryPath);
57+
58+
foreach (var file in files)
59+
{
60+
File.SetAttributes(file, FileAttributes.Normal);
61+
File.Delete(file);
62+
}
63+
64+
foreach (var dir in dirs)
65+
{
66+
DeleteDirectory(dir);
67+
}
68+
69+
File.SetAttributes(directoryPath, FileAttributes.Normal);
70+
try
71+
{
72+
Directory.Delete(directoryPath, false);
73+
}
74+
catch (IOException)
75+
{
76+
Trace.WriteLine(string.Format("{0}The directory '{1}' could not be deleted!" +
77+
"{0}Most of the time, this is due to an external process accessing the files in the temporary repositories created during the test runs, and keeping a handle on the directory, thus preventing the deletion of those files." +
78+
"{0}Known and common causes include:" +
79+
"{0}- Windows Search Indexer (go to the Indexing Options, in the Windows Control Panel, and exclude the bin folder of LibGit2Sharp.Tests)" +
80+
"{0}- Antivirus (exclude the bin folder of LibGit2Sharp.Tests from the paths scanned by your real-time antivirus){0}",
81+
Environment.NewLine, Path.GetFullPath(directoryPath)));
82+
}
83+
}
84+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
public interface IPostTestDirectoryRemover
2+
{
3+
void Register(string directoryPath);
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.IO;
3+
using System.Reflection;
4+
5+
public static class PathHelper
6+
{
7+
public static string GetCurrentDirectory()
8+
{
9+
return Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
10+
}
11+
12+
public static string GetTempPath()
13+
{
14+
return Path.Combine(GetCurrentDirectory(), "TestRepositories", Guid.NewGuid().ToString());
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System.Text.RegularExpressions;
2+
3+
public static class Scrubbers
4+
{
5+
public static string GuidScrubber(string value)
6+
{
7+
return Regex.Replace(value, @"\b[a-f0-9]{40}\b", "000000000000000000000000000000000000000");
8+
}
9+
public static string GuidAndDateScrubber(string value)
10+
{
11+
return Regex.Replace(GuidScrubber(value), @"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(.\d*?)?Z", "<date replaced>");
12+
}
13+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.IO;
3+
4+
public class SelfCleaningDirectory
5+
{
6+
public SelfCleaningDirectory(IPostTestDirectoryRemover directoryRemover, string path)
7+
{
8+
if (Directory.Exists(path))
9+
{
10+
throw new InvalidOperationException(string.Format("Directory '{0}' already exists.", path));
11+
}
12+
13+
DirectoryPath = path;
14+
directoryRemover.Register(DirectoryPath);
15+
}
16+
17+
public string DirectoryPath;
18+
}

0 commit comments

Comments
 (0)