Skip to content

Commit c4a7057

Browse files
committed
split GitVersionTool class into GitVersionCalculateTool and GitVersionOutputTool
1 parent cdf4648 commit c4a7057

File tree

12 files changed

+123
-85
lines changed

12 files changed

+123
-85
lines changed

src/GitVersionCore.Tests/Core/DynamicRepositoryTests.cs

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

88-
var gitVersionCalculator = sp.GetService<IGitVersionTool>();
88+
var gitVersionCalculator = sp.GetService<IGitVersionCalculateTool>();
8989

9090
var versionVariables = gitVersionCalculator.CalculateVersionVariables();
9191

src/GitVersionCore.Tests/Core/GitVersionExecutorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ public void CalculateVersionFromWorktreeHead()
566566
version.Sha.ShouldBe(commits.First().Sha);
567567
}
568568

569-
private IGitVersionTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
569+
private IGitVersionCalculateTool GetGitVersionCalculator(GitVersionOptions gitVersionOptions, ILog logger = null, IRepository repository = null, IFileSystem fs = null)
570570
{
571571
sp = GetServiceProvider(gitVersionOptions, logger, repository, fs);
572572

@@ -575,7 +575,7 @@ private IGitVersionTool GetGitVersionCalculator(GitVersionOptions gitVersionOpti
575575
gitVersionCache = sp.GetService<IGitVersionCache>();
576576
gitPreparer = sp.GetService<IGitPreparer>();
577577

578-
return sp.GetService<IGitVersionTool>();
578+
return sp.GetService<IGitVersionCalculateTool>();
579579
}
580580

581581
private static IServiceProvider GetServiceProvider(GitVersionOptions gitVersionOptions, ILog log = null, IRepository repository = null, IFileSystem fileSystem = null, IEnvironment environment = null)

src/GitVersionCore.Tests/Core/GitVersionToolDirectoryTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public void FindsGitDirectory()
4242
services.AddSingleton(options);
4343
});
4444

45-
var gitVersionCalculator = sp.GetService<IGitVersionTool>();
45+
var gitVersionCalculator = sp.GetService<IGitVersionCalculateTool>();
4646

4747
gitVersionCalculator.CalculateVersionVariables();
4848
}
@@ -70,7 +70,7 @@ public void FindsGitDirectoryInParent()
7070
services.AddSingleton(options);
7171
});
7272

73-
var gitVersionCalculator = sp.GetService<IGitVersionTool>();
73+
var gitVersionCalculator = sp.GetService<IGitVersionCalculateTool>();
7474

7575
gitVersionCalculator.CalculateVersionVariables();
7676
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using GitVersion.OutputVariables;
2+
3+
namespace GitVersion
4+
{
5+
public interface IGitVersionCalculateTool
6+
{
7+
VersionVariables CalculateVersionVariables();
8+
}
9+
}

src/GitVersionCore/Core/Abstractions/IGitVersionTool.cs renamed to src/GitVersionCore/Core/Abstractions/IGitVersionOutputTool.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
namespace GitVersion
44
{
5-
public interface IGitVersionTool
5+
public interface IGitVersionOutputTool
66
{
7-
VersionVariables CalculateVersionVariables();
87
void OutputVariables(VersionVariables variables);
98
void UpdateAssemblyInfo(VersionVariables variables);
109
void UpdateWixVersionFile(VersionVariables variables);
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using System;
2+
using GitVersion.Logging;
3+
using GitVersion.OutputVariables;
4+
using GitVersion.VersionCalculation;
5+
using GitVersion.VersionCalculation.Cache;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace GitVersion
9+
{
10+
public class GitVersionCalculateTool : IGitVersionCalculateTool
11+
{
12+
private readonly ILog log;
13+
private readonly IGitVersionCache gitVersionCache;
14+
private readonly INextVersionCalculator nextVersionCalculator;
15+
private readonly IVariableProvider variableProvider;
16+
private readonly IGitPreparer gitPreparer;
17+
private readonly IGitVersionCacheKeyFactory cacheKeyFactory;
18+
19+
private readonly IOptions<GitVersionOptions> options;
20+
private readonly Lazy<GitVersionContext> versionContext;
21+
private GitVersionContext context => versionContext.Value;
22+
23+
public GitVersionCalculateTool(ILog log, INextVersionCalculator nextVersionCalculator,
24+
IVariableProvider variableProvider, IGitPreparer gitPreparer,
25+
IGitVersionCache gitVersionCache, IGitVersionCacheKeyFactory cacheKeyFactory,
26+
IOptions<GitVersionOptions> options, Lazy<GitVersionContext> versionContext)
27+
{
28+
this.log = log ?? throw new ArgumentNullException(nameof(log));
29+
30+
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
31+
this.variableProvider = variableProvider ?? throw new ArgumentNullException(nameof(variableProvider));
32+
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
33+
34+
this.cacheKeyFactory = cacheKeyFactory ?? throw new ArgumentNullException(nameof(cacheKeyFactory));
35+
this.gitVersionCache = gitVersionCache ?? throw new ArgumentNullException(nameof(gitVersionCache));
36+
37+
this.options = options ?? throw new ArgumentNullException(nameof(options));
38+
this.versionContext = versionContext ?? throw new ArgumentNullException(nameof(versionContext));
39+
}
40+
41+
public VersionVariables CalculateVersionVariables()
42+
{
43+
gitPreparer.Prepare(); //we need to prepare the repository before using it for version calculation
44+
45+
var gitVersionOptions = options.Value;
46+
47+
var cacheKey = cacheKeyFactory.Create(gitVersionOptions.ConfigInfo.OverrideConfig);
48+
var versionVariables = gitVersionOptions.Settings.NoCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(cacheKey);
49+
50+
if (versionVariables != null) return versionVariables;
51+
52+
var semanticVersion = nextVersionCalculator.FindVersion();
53+
versionVariables = variableProvider.GetVariablesFor(semanticVersion, context.Configuration, context.IsCurrentCommitTagged);
54+
55+
if (gitVersionOptions.Settings.NoCache) return versionVariables;
56+
try
57+
{
58+
gitVersionCache.WriteVariablesToDiskCache(cacheKey, versionVariables);
59+
}
60+
catch (AggregateException e)
61+
{
62+
log.Warning($"One or more exceptions during cache write:{System.Environment.NewLine}{e}");
63+
}
64+
65+
return versionVariables;
66+
}
67+
}
68+
}

src/GitVersionCore/Core/GitVersionTool.cs renamed to src/GitVersionCore/Core/GitVersionOutputTool.cs

Lines changed: 14 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Linq;
3-
using GitVersion.Logging;
3+
using GitVersion.Configuration;
44
using GitVersion.OutputVariables;
5-
using GitVersion.VersionCalculation;
6-
using GitVersion.VersionCalculation.Cache;
75
using GitVersion.VersionConverters.AssemblyInfo;
86
using GitVersion.VersionConverters.GitVersionInfo;
97
using GitVersion.VersionConverters.OutputGenerator;
@@ -12,73 +10,30 @@
1210

1311
namespace GitVersion
1412
{
15-
public class GitVersionTool : IGitVersionTool
13+
public class GitVersionOutputTool : IGitVersionOutputTool
1614
{
17-
private readonly ILog log;
18-
private readonly IGitVersionCache gitVersionCache;
19-
private readonly INextVersionCalculator nextVersionCalculator;
20-
private readonly IVariableProvider variableProvider;
21-
private readonly IGitPreparer gitPreparer;
22-
private readonly IGitVersionCacheKeyFactory cacheKeyFactory;
15+
private readonly IOptions<GitVersionOptions> options;
16+
private readonly IConfigProvider configProvider;
2317
private readonly IOutputGenerator outputGenerator;
2418
private readonly IWixVersionFileUpdater wixVersionFileUpdater;
2519
private readonly IGitVersionInfoGenerator gitVersionInfoGenerator;
2620
private readonly IAssemblyInfoFileUpdater assemblyInfoFileUpdater;
2721
private readonly IProjectFileUpdater projectFileUpdater;
2822

29-
private readonly IOptions<GitVersionOptions> options;
30-
private readonly Lazy<GitVersionContext> versionContext;
31-
private GitVersionContext context => versionContext.Value;
32-
33-
public GitVersionTool(ILog log, INextVersionCalculator nextVersionCalculator, IVariableProvider variableProvider, IGitPreparer gitPreparer,
34-
IGitVersionCache gitVersionCache, IGitVersionCacheKeyFactory cacheKeyFactory,
35-
IOutputGenerator outputGenerator, IWixVersionFileUpdater wixVersionFileUpdater, IGitVersionInfoGenerator gitVersionInfoGenerator, IAssemblyInfoFileUpdater assemblyInfoFileUpdater,
36-
IOptions<GitVersionOptions> options, Lazy<GitVersionContext> versionContext, IProjectFileUpdater projectFileUpdater)
23+
public GitVersionOutputTool(IOptions<GitVersionOptions> options, IConfigProvider configProvider,
24+
IOutputGenerator outputGenerator, IWixVersionFileUpdater wixVersionFileUpdater,
25+
IGitVersionInfoGenerator gitVersionInfoGenerator, IAssemblyInfoFileUpdater assemblyInfoFileUpdater,
26+
IProjectFileUpdater projectFileUpdater)
3727
{
38-
this.log = log ?? throw new ArgumentNullException(nameof(log));
39-
40-
this.nextVersionCalculator = nextVersionCalculator ?? throw new ArgumentNullException(nameof(nextVersionCalculator));
41-
this.variableProvider = variableProvider ?? throw new ArgumentNullException(nameof(variableProvider));
42-
this.gitPreparer = gitPreparer ?? throw new ArgumentNullException(nameof(gitPreparer));
43-
44-
this.cacheKeyFactory = cacheKeyFactory ?? throw new ArgumentNullException(nameof(cacheKeyFactory));
45-
this.gitVersionCache = gitVersionCache ?? throw new ArgumentNullException(nameof(gitVersionCache));
28+
this.options = options ?? throw new ArgumentNullException(nameof(options));
29+
this.configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider));
4630

4731
this.outputGenerator = outputGenerator ?? throw new ArgumentNullException(nameof(outputGenerator));
32+
4833
this.wixVersionFileUpdater = wixVersionFileUpdater ?? throw new ArgumentNullException(nameof(wixVersionFileUpdater));
4934
this.gitVersionInfoGenerator = gitVersionInfoGenerator ?? throw new ArgumentNullException(nameof(gitVersionInfoGenerator));
5035
this.assemblyInfoFileUpdater = assemblyInfoFileUpdater ?? throw new ArgumentNullException(nameof(gitVersionInfoGenerator));
51-
52-
this.options = options ?? throw new ArgumentNullException(nameof(options));
53-
this.versionContext = versionContext ?? throw new ArgumentNullException(nameof(versionContext));
54-
this.projectFileUpdater = projectFileUpdater;
55-
}
56-
57-
public VersionVariables CalculateVersionVariables()
58-
{
59-
gitPreparer.Prepare(); //we need to prepare the repository before using it for version calculation
60-
61-
var gitVersionOptions = options.Value;
62-
63-
var cacheKey = cacheKeyFactory.Create(gitVersionOptions.ConfigInfo.OverrideConfig);
64-
var versionVariables = gitVersionOptions.Settings.NoCache ? default : gitVersionCache.LoadVersionVariablesFromDiskCache(cacheKey);
65-
66-
if (versionVariables != null) return versionVariables;
67-
68-
var semanticVersion = nextVersionCalculator.FindVersion();
69-
versionVariables = variableProvider.GetVariablesFor(semanticVersion, context.Configuration, context.IsCurrentCommitTagged);
70-
71-
if (gitVersionOptions.Settings.NoCache) return versionVariables;
72-
try
73-
{
74-
gitVersionCache.WriteVariablesToDiskCache(cacheKey, versionVariables);
75-
}
76-
catch (AggregateException e)
77-
{
78-
log.Warning($"One or more exceptions during cache write:{System.Environment.NewLine}{e}");
79-
}
80-
81-
return versionVariables;
36+
this.projectFileUpdater = projectFileUpdater ?? throw new ArgumentNullException(nameof(projectFileUpdater));
8237
}
8338

8439
public void OutputVariables(VersionVariables variables)
@@ -87,7 +42,8 @@ public void OutputVariables(VersionVariables variables)
8742

8843
using (outputGenerator)
8944
{
90-
outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile));
45+
var configuration = configProvider.Provide(overrideConfig: options.Value.ConfigInfo.OverrideConfig);
46+
outputGenerator.Execute(variables, new OutputContext(gitVersionOptions.WorkingDirectory, gitVersionOptions.OutputFile, configuration.UpdateBuildNumber));
9147
}
9248
}
9349

src/GitVersionCore/GitVersionCoreModule.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ public void RegisterTypes(IServiceCollection services)
3939
services.AddSingleton<IBaseVersionCalculator, BaseVersionCalculator>();
4040
services.AddSingleton<IMainlineVersionCalculator, MainlineVersionCalculator>();
4141
services.AddSingleton<INextVersionCalculator, NextVersionCalculator>();
42-
services.AddSingleton<IGitVersionTool, GitVersionTool>();
4342
services.AddSingleton<IBranchConfigurationCalculator, BranchConfigurationCalculator>();
4443

44+
services.AddSingleton<IGitVersionCalculateTool, GitVersionCalculateTool>();
45+
services.AddSingleton<IGitVersionOutputTool, GitVersionOutputTool>();
46+
4547
services.AddSingleton<IBuildAgentResolver, BuildAgentResolver>();
4648
services.AddSingleton<IGitPreparer, GitPreparer>();
4749
services.AddSingleton<IRepositoryMetadataProvider, RepositoryMetadataProvider>();

src/GitVersionCore/VersionConverters/OutputGenerator/OutputContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ namespace GitVersion.VersionConverters.OutputGenerator
22
{
33
public readonly struct OutputContext : IConverterContext
44
{
5-
public OutputContext(string workingDirectory, string outputFile)
5+
public OutputContext(string workingDirectory, string outputFile, bool? updateBuildNumber)
66
{
77
WorkingDirectory = workingDirectory;
88
OutputFile = outputFile;
9+
UpdateBuildNumber = updateBuildNumber;
910
}
1011

1112
public string WorkingDirectory { get; }
1213
public string OutputFile { get; }
14+
public bool? UpdateBuildNumber { get; }
1315
}
1416
}

src/GitVersionCore/VersionConverters/OutputGenerator/OutputGenerator.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ public class OutputGenerator : IOutputGenerator
1515
private readonly IConsole console;
1616
private readonly IFileSystem fileSystem;
1717
private readonly IOptions<GitVersionOptions> options;
18-
private readonly Lazy<GitVersionContext> versionContext;
1918
private readonly ICurrentBuildAgent buildAgent;
2019

21-
public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IFileSystem fileSystem, IOptions<GitVersionOptions> options, Lazy<GitVersionContext> versionContext)
20+
public OutputGenerator(ICurrentBuildAgent buildAgent, IConsole console, IFileSystem fileSystem, IOptions<GitVersionOptions> options)
2221
{
2322
this.console = console ?? throw new ArgumentNullException(nameof(console));
2423
this.fileSystem = fileSystem ?? throw new ArgumentNullException(nameof(fileSystem));
2524
this.options = options ?? throw new ArgumentNullException(nameof(options));
26-
this.versionContext = versionContext;
2725
this.buildAgent = buildAgent;
2826
}
2927

@@ -32,7 +30,7 @@ public void Execute(VersionVariables variables, OutputContext context)
3230
var gitVersionOptions = options.Value;
3331
if (gitVersionOptions.Output.Contains(OutputType.BuildServer))
3432
{
35-
buildAgent?.WriteIntegration(console.WriteLine, variables, versionContext.Value.Configuration.UpdateBuildNumber);
33+
buildAgent?.WriteIntegration(console.WriteLine, variables, context.UpdateBuildNumber ?? true);
3634
}
3735
if (gitVersionOptions.Output.Contains(OutputType.File))
3836
{

0 commit comments

Comments
 (0)