Skip to content

Commit be4dee3

Browse files
asbjornuJakeGinnivan
authored andcommitted
Refactored VersionAndBranchFinder to a non-static file.
1 parent 1f9973e commit be4dee3

File tree

8 files changed

+105
-53
lines changed

8 files changed

+105
-53
lines changed

src/GitVersionTask.Tests/GitVersionTaskDirectoryTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
public class GitVersionTaskDirectoryTests
1010
{
1111
string gitDirectory;
12+
VersionAndBranchFinder versionAndBranchFinder;
1213
string workDirectory;
1314

1415

@@ -18,7 +19,7 @@ public void CreateTemporaryRepository()
1819
this.workDirectory = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
1920
this.gitDirectory = Repository.Init(this.workDirectory)
2021
.TrimEnd(Path.DirectorySeparatorChar);
21-
22+
this.versionAndBranchFinder = new VersionAndBranchFinder(new TestFileSystem());
2223
Assert.NotNull(this.gitDirectory);
2324
}
2425

@@ -35,7 +36,7 @@ public void Finds_GitDirectory()
3536
{
3637
try
3738
{
38-
VersionAndBranchFinder.GetVersion(this.workDirectory, null, true, null);
39+
this.versionAndBranchFinder.GetVersion(this.workDirectory, null, true);
3940
}
4041
catch (Exception ex)
4142
{
@@ -54,7 +55,7 @@ public void Finds_GitDirectory_In_Parent()
5455

5556
try
5657
{
57-
VersionAndBranchFinder.GetVersion(childDir, null, true, null);
58+
this.versionAndBranchFinder.GetVersion(childDir, null, true);
5859
}
5960
catch (Exception ex)
6061
{

src/GitVersionTask.Tests/VersionAndBranchFinderTests.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,16 @@
1212
[TestFixture]
1313
public class VersionAndBranchFinderTests
1414
{
15+
IFileSystem fileSystem;
16+
17+
18+
[SetUp]
19+
public void SetUp()
20+
{
21+
this.fileSystem = new FileSystem();
22+
}
23+
24+
1525
[Test]
1626
public void CacheFileExistsOnDisk()
1727
{
@@ -40,16 +50,12 @@ public void CacheFileExistsOnDisk()
4050
CommitDate: 2015-11-10
4151
";
4252

43-
var info = RepositoryScope((fixture, vv, fs) =>
44-
{
45-
fs.WriteAllText(vv.FileName, versionCacheFileContent);
46-
47-
// I would rather see that VersionAndBranchFinder was non-static and could be reinstantiated to
48-
// clear the in-memory cache, but that's not the case, so I have to perform this ugly hack. @asbjornu
49-
VersionAndBranchFinder.VersionCacheVersions = new ConcurrentDictionary<string, VersionVariables>();
50-
51-
vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fs);
53+
var versionAndBranchFinder = new VersionAndBranchFinder(this.fileSystem);
5254

55+
var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
56+
{
57+
this.fileSystem.WriteAllText(vv.FileName, versionCacheFileContent);
58+
vv = versionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false);
5359
vv.AssemblySemVer.ShouldBe("4.10.3.0");
5460
});
5561

@@ -60,9 +66,12 @@ public void CacheFileExistsOnDisk()
6066
[Test]
6167
public void CacheFileExistsInMemory()
6268
{
63-
var info = RepositoryScope((fixture, vv, fs) =>
69+
var cache = new ConcurrentDictionary<string, VersionVariables>();
70+
var versionAndBranchFinder = new VersionAndBranchFinder(this.fileSystem, cache.GetOrAdd);
71+
72+
var info = RepositoryScope(versionAndBranchFinder, (fixture, vv) =>
6473
{
65-
vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fs);
74+
vv = versionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false);
6675
vv.AssemblySemVer.ShouldBe("0.1.0.0");
6776
});
6877

@@ -79,25 +88,25 @@ public void CacheFileIsMissing()
7988
}
8089

8190

82-
static string RepositoryScope(Action<EmptyRepositoryFixture, VersionVariables, IFileSystem> fixtureAction = null)
91+
string RepositoryScope(VersionAndBranchFinder versionAndBranchFinder = null, Action<EmptyRepositoryFixture, VersionVariables> fixtureAction = null)
8392
{
8493
var infoBuilder = new StringBuilder();
8594
Action<string> infoLogger = s => { infoBuilder.AppendLine(s); };
95+
versionAndBranchFinder = versionAndBranchFinder ?? new VersionAndBranchFinder(this.fileSystem);
8696

8797
Logger.SetLoggers(infoLogger, null, null);
8898

8999
using (var fixture = new EmptyRepositoryFixture(new Config()))
90100
{
91101
fixture.Repository.MakeACommit();
92-
var fileSystem = new FileSystem();
93-
var vv = VersionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false, fileSystem);
102+
var vv = versionAndBranchFinder.GetVersion(fixture.RepositoryPath, null, false);
94103

95104
vv.AssemblySemVer.ShouldBe("0.1.0.0");
96105
vv.FileName.ShouldNotBeNullOrEmpty();
97106

98107
if (fixtureAction != null)
99108
{
100-
fixtureAction(fixture, vv, fileSystem);
109+
fixtureAction(fixture, vv);
101110
}
102111
}
103112

src/GitVersionTask/AssemblyInfoBuilder/UpdateAssemblyInfo.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
using System.Text;
66

77
using GitVersion;
8-
using GitVersion.Helpers;
98

109
using Microsoft.Build.Framework;
1110

12-
public class UpdateAssemblyInfo : Task
11+
public class UpdateAssemblyInfo : GitVersionTaskBase
1312
{
14-
IFileSystem fileSystem;
15-
1613
TaskLogger logger;
1714

1815

@@ -22,7 +19,6 @@ public UpdateAssemblyInfo()
2219
{
2320
};
2421
this.logger = new TaskLogger(this);
25-
this.fileSystem = new FileSystem();
2622
Logger.SetLoggers(this.LogInfo, this.LogWarning, s => this.LogError(s));
2723
}
2824

src/GitVersionTask/GetVersion.cs

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
namespace GitVersionTask
22
{
33
using System;
4+
45
using GitVersion;
5-
using GitVersion.Helpers;
6+
67
using Microsoft.Build.Framework;
7-
using Microsoft.Build.Utilities;
8-
using Logger = GitVersion.Logger;
98

10-
public class GetVersion : Task
9+
public class GetVersion : GitVersionTaskBase
1110
{
1211
TaskLogger logger;
1312

1413

1514
public GetVersion()
1615
{
1716
this.logger = new TaskLogger(this);
18-
this.fileSystem = new FileSystem();
1917
Logger.SetLoggers(this.LogInfo, this.LogWarning, s => this.LogError(s));
2018
}
2119

@@ -91,15 +89,14 @@ public GetVersion()
9189
[Output]
9290
public string CommitsSinceVersionSourcePadded { get; set; }
9391

94-
IFileSystem fileSystem;
9592

9693
public override bool Execute()
9794
{
9895
try
9996
{
10097
VersionVariables variables;
10198

102-
if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out variables, NoFetch, new Authentication(), fileSystem))
99+
if (VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out variables, NoFetch, new Authentication()))
103100
{
104101
var thisType = typeof(GetVersion);
105102
foreach (var variable in variables)

src/GitVersionTask/GitVersionTask.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
<ItemGroup>
6161
<Compile Include="AssemblyInfoBuilder\AssemblyInfoBuilder.cs" />
6262
<Compile Include="BuildLogger.cs" />
63+
<Compile Include="GitVersionTaskBase.cs" />
6364
<Compile Include="WriteVersionInfoToBuildLog.cs" />
6465
<Compile Include="GetVersion.cs" />
6566
<Compile Include="InvalidFileChecker.cs" />
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
namespace GitVersionTask
2+
{
3+
using System.Collections.Concurrent;
4+
5+
using GitVersion;
6+
using GitVersion.Helpers;
7+
8+
using Microsoft.Build.Utilities;
9+
10+
public abstract class GitVersionTaskBase : Task
11+
{
12+
static readonly ConcurrentDictionary<string, VersionVariables> versionVariablesCache;
13+
readonly VersionAndBranchFinder versionAndBranchFinder;
14+
15+
16+
static GitVersionTaskBase()
17+
{
18+
versionVariablesCache = new ConcurrentDictionary<string, VersionVariables>();
19+
}
20+
21+
22+
protected GitVersionTaskBase()
23+
{
24+
var fileSystem = new FileSystem();
25+
this.versionAndBranchFinder = new VersionAndBranchFinder(fileSystem, versionVariablesCache.GetOrAdd);
26+
}
27+
28+
29+
protected VersionAndBranchFinder VersionAndBranchFinder
30+
{
31+
get { return this.versionAndBranchFinder; }
32+
}
33+
}
34+
}

src/GitVersionTask/VersionAndBranchFinder.cs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Concurrent;
32
using System.Collections.Generic;
43
using System.IO;
54
using System.Linq;
@@ -13,16 +12,29 @@
1312

1413
using YamlDotNet.Serialization;
1514

16-
public static class VersionAndBranchFinder
15+
public class VersionAndBranchFinder
1716
{
18-
internal static ConcurrentDictionary<string, VersionVariables> VersionCacheVersions = new ConcurrentDictionary<string, VersionVariables>();
17+
readonly IFileSystem fileSystem;
18+
readonly Func<string, Func<string, VersionVariables>, VersionVariables> getVersionVariablesFromMemoryCache;
1919

2020

21-
public static bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication, IFileSystem fileSystem)
21+
public VersionAndBranchFinder(IFileSystem fileSystem, Func<string, Func<string, VersionVariables>, VersionVariables> getVersionVariablesFromMemoryCache = null)
22+
{
23+
if (fileSystem == null)
24+
{
25+
throw new ArgumentNullException("fileSystem");
26+
}
27+
28+
this.getVersionVariablesFromMemoryCache = getVersionVariablesFromMemoryCache;
29+
this.fileSystem = fileSystem;
30+
}
31+
32+
33+
public bool TryGetVersion(string directory, out VersionVariables versionVariables, bool noFetch, Authentication authentication)
2234
{
2335
try
2436
{
25-
versionVariables = GetVersion(directory, authentication, noFetch, fileSystem);
37+
versionVariables = GetVersion(directory, authentication, noFetch);
2638
return true;
2739
}
2840
catch (Exception ex)
@@ -34,25 +46,30 @@ public static bool TryGetVersion(string directory, out VersionVariables versionV
3446
}
3547

3648

37-
public static VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem)
49+
public VersionVariables GetVersion(string directory, Authentication authentication, bool noFetch)
3850
{
3951
var gitDir = Repository.Discover(directory);
40-
using (var repo = fileSystem.GetRepository(gitDir))
52+
using (var repo = this.fileSystem.GetRepository(gitDir))
4153
{
4254
// Maybe using timestamp in .git/refs directory is enough?
43-
var ticks = fileSystem.GetLastDirectoryWrite(Path.Combine(gitDir, "refs"));
44-
string key = string.Format("{0}:{1}:{2}:{3}", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
55+
var ticks = this.fileSystem.GetLastDirectoryWrite(Path.Combine(gitDir, "refs"));
56+
var key = string.Format("{0}:{1}:{2}:{3}", gitDir, repo.Head.CanonicalName, repo.Head.Tip.Sha, ticks);
4557

46-
return VersionCacheVersions.GetOrAdd(key, k =>
58+
if (this.getVersionVariablesFromMemoryCache != null)
4759
{
48-
Logger.WriteInfo("Version not in memory cache. Attempting to load version from disk cache.");
49-
return LoadVersionVariablesFromDiskCache(k, directory, authentication, noFetch, fileSystem, gitDir, ticks);
50-
});
60+
return this.getVersionVariablesFromMemoryCache(key, k =>
61+
{
62+
Logger.WriteInfo("Version not in memory cache. Attempting to load version from disk cache.");
63+
return LoadVersionVariablesFromDiskCache(key, directory, authentication, noFetch, gitDir);
64+
});
65+
}
66+
67+
return LoadVersionVariablesFromDiskCache(key, directory, authentication, noFetch, gitDir);
5168
}
5269
}
5370

5471

55-
static VersionVariables LoadVersionVariablesFromDiskCache(string key, string directory, Authentication authentication, bool noFetch, IFileSystem fileSystem, string gitDir, long ticks)
72+
VersionVariables LoadVersionVariablesFromDiskCache(string key, string directory, Authentication authentication, bool noFetch, string gitDir)
5673
{
5774
using (Logger.IndentLog("Loading version variables from disk cache"))
5875
{
@@ -65,11 +82,11 @@ static VersionVariables LoadVersionVariablesFromDiskCache(string key, string dir
6582

6683
var cacheDir = Path.Combine(gitDir, "gitversion_cache");
6784
// If the cacheDir already exists, CreateDirectory just won't do anything (it won't fail). @asbjornu
68-
fileSystem.CreateDirectory(cacheDir);
85+
this.fileSystem.CreateDirectory(cacheDir);
6986

7087
var cacheFileName = string.Concat(Path.Combine(cacheDir, cacheKey), ".yml");
7188
VersionVariables vv = null;
72-
if (fileSystem.Exists(cacheFileName))
89+
if (this.fileSystem.Exists(cacheFileName))
7390
{
7491
using (Logger.IndentLog("Deserializing version variables from cache file " + cacheFileName))
7592
{
@@ -83,7 +100,7 @@ static VersionVariables LoadVersionVariablesFromDiskCache(string key, string dir
83100
Logger.WriteInfo(ex.ToString());
84101
try
85102
{
86-
fileSystem.Delete(cacheFileName);
103+
this.fileSystem.Delete(cacheFileName);
87104
}
88105
catch (Exception deleteEx)
89106
{
@@ -99,7 +116,7 @@ static VersionVariables LoadVersionVariablesFromDiskCache(string key, string dir
99116

100117
if (vv == null)
101118
{
102-
vv = ExecuteCore.ExecuteGitVersion(fileSystem, null, null, authentication, null, noFetch, directory, null);
119+
vv = ExecuteCore.ExecuteGitVersion(this.fileSystem, null, null, authentication, null, noFetch, directory, null);
103120
vv.FileName = cacheFileName;
104121

105122
using (var stream = fileSystem.OpenWrite(cacheFileName))

src/GitVersionTask/WriteVersionInfoToBuildLog.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,17 @@
44
using System.Collections.Generic;
55

66
using GitVersion;
7-
using GitVersion.Helpers;
87

98
using Microsoft.Build.Framework;
109

11-
public class WriteVersionInfoToBuildLog : Task
10+
public class WriteVersionInfoToBuildLog : GitVersionTaskBase
1211
{
13-
readonly IFileSystem fileSystem;
1412
readonly TaskLogger logger;
1513

1614

1715
public WriteVersionInfoToBuildLog()
1816
{
1917
this.logger = new TaskLogger(this);
20-
this.fileSystem = new FileSystem();
2118
Logger.SetLoggers(this.LogInfo, this.LogWarning, s => this.LogError(s));
2219
}
2320

@@ -55,7 +52,7 @@ public override bool Execute()
5552
void InnerExecute()
5653
{
5754
VersionVariables result;
58-
if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out result, NoFetch, new Authentication(), fileSystem))
55+
if (!VersionAndBranchFinder.TryGetVersion(SolutionDirectory, out result, NoFetch, new Authentication()))
5956
{
6057
return;
6158
}

0 commit comments

Comments
 (0)