Skip to content

Commit d1e1c4b

Browse files
committed
introduce IGitVersionTool
1 parent 6ea1752 commit d1e1c4b

File tree

17 files changed

+210
-188
lines changed

17 files changed

+210
-188
lines changed

src/GitVersionCore.Tests/DynamicRepositoryTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public void FindsVersionInDynamicRepo(string name, string url, string targetBran
8282
var gitPreparer = sp.GetService<IGitPreparer>();
8383
gitPreparer.Prepare();
8484

85-
var gitVersionCalculator = sp.GetService<IGitVersionCalculator>();
85+
var gitVersionCalculator = sp.GetService<IGitVersionTool>();
8686

8787
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
8888

src/GitVersionCore.Tests/GitVersionExecutorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ public void GetDotGitDirectoryWorktree()
505505
}
506506
}
507507

508-
private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
508+
private IGitVersionTool GetGitVersionCalculator(Arguments arguments, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
509509
{
510510
sp = GetServiceProvider(arguments, logger, repository, fs);
511511

@@ -514,7 +514,7 @@ private IGitVersionCalculator GetGitVersionCalculator(Arguments arguments, ILog
514514
gitVersionCache = sp.GetService<IGitVersionCache>();
515515
gitPreparer = sp.GetService<IGitPreparer>();
516516

517-
return sp.GetService<IGitVersionCalculator>();
517+
return sp.GetService<IGitVersionTool>();
518518
}
519519

520520
private static IServiceProvider GetServiceProvider(Arguments arguments, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null)

src/GitVersionCore.Tests/GitVersionInformationGeneratorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ public void ShouldCreateFile(string fileExtension)
4747
var variableProvider = sp.GetService<IVariableProvider>();
4848

4949
var variables = variableProvider.GetVariablesFor(semanticVersion, new TestEffectiveConfiguration(), false);
50-
var generator = new GitVersionInformationGenerator(fileName, directory, variables, fileSystem);
50+
var generator = new GitVersionInformationGenerator(fileSystem);
5151

52-
generator.Generate();
52+
generator.Generate(variables, new FileWriteInfo(directory, fileName, fileExtension));
5353

5454
fileSystem.ReadAllText(fullPath).ShouldMatchApproved(c => c.SubFolder(Path.Combine("Approved", fileExtension)));
5555
}

src/GitVersionCore/Common/IGitVersionCalculator.cs

Lines changed: 0 additions & 9 deletions
This file was deleted.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using GitVersion.Extensions.GitVersionInformationResources;
3+
using GitVersion.OutputVariables;
4+
5+
namespace GitVersion
6+
{
7+
public interface IGitVersionTool
8+
{
9+
VersionVariables CalculateVersionVariables();
10+
void OutputVariables(VersionVariables variables, Action<string> writter);
11+
void UpdateAssemblyInfo(VersionVariables variables);
12+
void UpdateWixVersionFile(VersionVariables variables);
13+
void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo);
14+
}
15+
}

src/GitVersionCore/Core/GitVersionCalculator.cs

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System;
2+
using GitVersion.Extensions;
3+
using GitVersion.Extensions.GitVersionInformationResources;
4+
using GitVersion.Extensions.VersionAssemblyInfoResources;
5+
using GitVersion.Logging;
6+
using GitVersion.Model;
7+
using GitVersion.OutputVariables;
8+
using GitVersion.VersionCalculation;
9+
using GitVersion.VersionCalculation.Cache;
10+
using Microsoft.Extensions.Options;
11+
12+
namespace GitVersion
13+
{
14+
public class GitVersionTool : IGitVersionTool
15+
{
16+
private readonly ILog log;
17+
private readonly IGitVersionCache gitVersionCache;
18+
private readonly INextVersionCalculator nextVersionCalculator;
19+
private readonly IVariableProvider variableProvider;
20+
private readonly IGitVersionCacheKeyFactory cacheKeyFactory;
21+
private readonly IFileSystem fileSystem;
22+
private readonly IOptions<Arguments> options;
23+
private readonly GitVersionContext context;
24+
private readonly IBuildServer buildServer;
25+
26+
public GitVersionTool(ILog log, IGitVersionCache gitVersionCache, INextVersionCalculator nextVersionCalculator, IVariableProvider variableProvider,
27+
IGitVersionCacheKeyFactory cacheKeyFactory, IBuildServerResolver buildServerResolver, IFileSystem fileSystem,
28+
IOptions<Arguments> options, IOptions<GitVersionContext> versionContext)
29+
{
30+
this.log = log ?? throw new ArgumentNullException(nameof(log));
31+
this.gitVersionCache = gitVersionCache ?? throw new ArgumentNullException(nameof(gitVersionCache));
32+
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
33+
this.variableProvider = variableProvider ?? throw new ArgumentNullException(nameof(variableProvider));
34+
this.options = options ?? throw new ArgumentNullException(nameof(options));
35+
this.cacheKeyFactory = cacheKeyFactory ?? throw new ArgumentNullException(nameof(cacheKeyFactory));
36+
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
37+
context = versionContext.Value;
38+
buildServer = buildServerResolver.Resolve();
39+
}
40+
41+
public VersionVariables CalculateVersionVariables()
42+
{
43+
var arguments = options.Value;
44+
45+
var cacheKey = cacheKeyFactory.Create(arguments.OverrideConfig);
46+
var versionVariables = arguments.NoCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(cacheKey);
47+
48+
if (versionVariables != null) return versionVariables;
49+
50+
versionVariables = ExecuteInternal();
51+
52+
if (arguments.NoCache) return versionVariables;
53+
try
54+
{
55+
gitVersionCache.WriteVariablesToDiskCache(cacheKey, versionVariables);
56+
}
57+
catch (AggregateException e)
58+
{
59+
log.Warning($"One or more exceptions during cache write:{System.Environment.NewLine}{e}");
60+
}
61+
62+
return versionVariables;
63+
}
64+
65+
public void OutputVariables(VersionVariables variables, Action<string> writter)
66+
{
67+
var arguments = options.Value;
68+
if (arguments.Output.Contains(OutputType.BuildServer))
69+
{
70+
buildServer?.WriteIntegration(writter, variables);
71+
}
72+
if (arguments.Output.Contains(OutputType.Json))
73+
{
74+
switch (arguments.ShowVariable)
75+
{
76+
case null:
77+
Console.WriteLine(variables.ToString());
78+
break;
79+
80+
default:
81+
if (!variables.TryGetValue(arguments.ShowVariable, out var part))
82+
{
83+
throw new WarningException($"'{arguments.ShowVariable}' variable does not exist");
84+
}
85+
86+
Console.WriteLine(part);
87+
break;
88+
}
89+
}
90+
}
91+
92+
public void UpdateAssemblyInfo(VersionVariables variables)
93+
{
94+
var arguments = options.Value;
95+
96+
if (arguments.UpdateAssemblyInfo)
97+
{
98+
using var assemblyInfoUpdater = new AssemblyInfoFileUpdater(arguments.UpdateAssemblyInfoFileName, arguments.TargetPath, variables, fileSystem, log, arguments.EnsureAssemblyInfo);
99+
assemblyInfoUpdater.Update();
100+
assemblyInfoUpdater.CommitChanges();
101+
}
102+
}
103+
104+
public void UpdateWixVersionFile(VersionVariables variables)
105+
{
106+
var arguments = options.Value;
107+
108+
if (arguments.UpdateWixVersionFile)
109+
{
110+
using var wixVersionFileUpdater = new WixVersionFileUpdater(arguments.TargetPath, variables, fileSystem, log);
111+
wixVersionFileUpdater.Update();
112+
}
113+
}
114+
115+
public void GenerateGitVersionInformation(VersionVariables variables, FileWriteInfo fileWriteInfo)
116+
{
117+
var generator = new GitVersionInformationGenerator(fileSystem);
118+
generator.Generate(variables, fileWriteInfo);
119+
}
120+
121+
private VersionVariables ExecuteInternal()
122+
{
123+
var semanticVersion = nextVersionCalculator.FindVersion();
124+
return variableProvider.GetVariablesFor(semanticVersion, context.Configuration, context.IsCurrentCommitTagged);
125+
}
126+
}
127+
}

src/GitVersionCore/Extensions/GitVersionInformationResources/GitVersionInformationGenerator.cs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1+
using System;
12
using System.IO;
23
using System.Linq;
34
using GitVersion.OutputVariables;
45

56
namespace GitVersion.Extensions.GitVersionInformationResources
67
{
8+
public sealed class FileWriteInfo
9+
{
10+
public FileWriteInfo(string workingDirectory, string fileName, string fileExtension)
11+
{
12+
WorkingDirectory = workingDirectory;
13+
FileName = fileName;
14+
FileExtension = fileExtension;
15+
}
16+
17+
public string WorkingDirectory { get; }
18+
public string FileName { get; }
19+
public string FileExtension { get; }
20+
}
21+
22+
723
public class GitVersionInformationGenerator
824
{
9-
private readonly string fileName;
10-
private readonly string directory;
11-
private readonly VersionVariables variables;
1225
private readonly IFileSystem fileSystem;
13-
1426
private readonly TemplateManager templateManager;
1527

16-
public GitVersionInformationGenerator(string fileName, string directory, VersionVariables variables, IFileSystem fileSystem)
28+
public GitVersionInformationGenerator(IFileSystem fileSystem)
1729
{
18-
this.fileName = fileName;
19-
this.directory = directory;
20-
this.variables = variables;
21-
this.fileSystem = fileSystem;
22-
30+
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
2331
templateManager = new TemplateManager(TemplateType.GitVersionInformationResources);
2432
}
2533

26-
public void Generate()
34+
public void Generate(VersionVariables variables, FileWriteInfo writeInfo)
2735
{
36+
var fileName = writeInfo.FileName;
37+
var directory = writeInfo.WorkingDirectory;
2838
var filePath = Path.Combine(directory, fileName);
2939

3040
string originalFileContents = null;

src/GitVersionCore/Extensions/VersionAssemblyInfoResources/AssemblyInfoFileUpdater.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,13 @@ public AssemblyInfoFileUpdater(string assemblyInfoFileName, string workingDirect
3636

3737
public AssemblyInfoFileUpdater(ISet<string> assemblyInfoFileNames, string workingDirectory, VersionVariables variables, IFileSystem fileSystem, ILog log, bool ensureAssemblyInfo)
3838
{
39+
this.fileSystem = fileSystem;
40+
this.log = log;
41+
3942
this.assemblyInfoFileNames = assemblyInfoFileNames;
4043
this.workingDirectory = workingDirectory;
4144
this.variables = variables;
42-
this.fileSystem = fileSystem;
43-
this.log = log;
45+
4446
this.ensureAssemblyInfo = ensureAssemblyInfo;
4547

4648
templateManager = new TemplateManager(TemplateType.VersionAssemblyInfoResources);
@@ -50,7 +52,7 @@ public void Update()
5052
{
5153
log.Info("Updating assembly info files");
5254

53-
var assemblyInfoFiles = GetAssemblyInfoFiles(workingDirectory, assemblyInfoFileNames, fileSystem, ensureAssemblyInfo).ToList();
55+
var assemblyInfoFiles = GetAssemblyInfoFiles().ToList();
5456
log.Info($"Found {assemblyInfoFiles.Count} files");
5557

5658
var assemblyVersion = variables.AssemblySemVer;
@@ -140,15 +142,15 @@ private string ReplaceOrInsertAfterLastAssemblyAttributeOrAppend(Regex replaceRe
140142
return inputString;
141143
}
142144

143-
private IEnumerable<FileInfo> GetAssemblyInfoFiles(string workingDirectory, ISet<string> assemblyInfoFileNames, IFileSystem fileSystem, bool ensureAssemblyInfo)
145+
private IEnumerable<FileInfo> GetAssemblyInfoFiles()
144146
{
145147
if (assemblyInfoFileNames != null && assemblyInfoFileNames.Any(x => !string.IsNullOrWhiteSpace(x)))
146148
{
147149
foreach (var item in assemblyInfoFileNames)
148150
{
149151
var fullPath = Path.Combine(workingDirectory, item);
150152

151-
if (EnsureVersionAssemblyInfoFile(ensureAssemblyInfo, fileSystem, fullPath))
153+
if (EnsureVersionAssemblyInfoFile(fullPath))
152154
{
153155
yield return new FileInfo(fullPath);
154156
}
@@ -168,8 +170,9 @@ private IEnumerable<FileInfo> GetAssemblyInfoFiles(string workingDirectory, ISet
168170
}
169171
}
170172

171-
private bool EnsureVersionAssemblyInfoFile(bool ensureAssemblyInfo, IFileSystem fileSystem, string fullPath)
173+
private bool EnsureVersionAssemblyInfoFile(string fullPath)
172174
{
175+
fullPath = fullPath ?? throw new ArgumentNullException(nameof(fullPath));
173176
if (fileSystem.Exists(fullPath))
174177
{
175178
return true;

src/GitVersionCore/GitVersionCoreModule.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public void RegisterTypes(IServiceCollection services)
3434
services.AddSingleton<IBaseVersionCalculator, BaseVersionCalculator>();
3535
services.AddSingleton<IMainlineVersionCalculator, MainlineVersionCalculator>();
3636
services.AddSingleton<INextVersionCalculator, NextVersionCalculator>();
37-
services.AddSingleton<IGitVersionCalculator, GitVersionCalculator>();
37+
services.AddSingleton<IGitVersionTool, GitVersionTool>();
3838
services.AddSingleton<IBranchConfigurationCalculator, BranchConfigurationCalculator>();
3939

4040
services.AddSingleton<IBuildServerResolver, BuildServerResolver>();
4141
services.AddSingleton<IGitPreparer, GitPreparer>();
4242
services.AddSingleton<IGitRepoMetadataProvider, GitRepoMetadataProvider>();
4343

44+
services.AddSingleton<IGitRepoMetadataProvider, GitRepoMetadataProvider>();
45+
4446
services.AddSingleton(sp => sp.GetService<IConfigFileLocatorFactory>().Create());
4547

4648
services.AddSingleton(sp =>

src/GitVersionExe.Tests/ExecutionResults.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ public virtual VersionVariables OutputVariables
2323
get
2424
{
2525
var jsonStartIndex = Output.IndexOf("{", StringComparison.Ordinal);
26-
var json = Output.Substring(jsonStartIndex);
26+
var jsonEndIndex = Output.IndexOf("}", StringComparison.Ordinal);
27+
var json = Output.Substring(jsonStartIndex, jsonEndIndex - jsonStartIndex + 1);
2728

2829
var outputVariables = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
2930
return VersionVariables.FromDictionary(outputVariables);

0 commit comments

Comments
 (0)