-
Notifications
You must be signed in to change notification settings - Fork 652
/
Copy pathGitVersionTaskExecutor.cs
112 lines (94 loc) · 4.78 KB
/
GitVersionTaskExecutor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System.IO.Abstractions;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Helpers;
using GitVersion.MsBuild.Tasks;
using GitVersion.OutputVariables;
using Microsoft.Extensions.Options;
namespace GitVersion.MsBuild;
internal class GitVersionTaskExecutor(
IFileSystem fileSystem,
IGitVersionOutputTool gitVersionOutputTool,
IVersionVariableSerializer serializer,
IConfigurationProvider configurationProvider,
IOptions<GitVersionOptions> options)
: IGitVersionTaskExecutor
{
private readonly IFileSystem fileSystem = fileSystem.NotNull();
private readonly IGitVersionOutputTool gitVersionOutputTool = gitVersionOutputTool.NotNull();
private readonly IVersionVariableSerializer serializer = serializer.NotNull();
private readonly IConfigurationProvider configurationProvider = configurationProvider.NotNull();
private readonly IOptions<GitVersionOptions> options = options.NotNull();
public void GetVersion(GetVersion task)
{
var versionVariables = GitVersionVariables(task);
var outputType = typeof(GetVersion);
foreach (var (key, value) in versionVariables)
{
outputType.GetProperty(key)?.SetValue(task, value, null);
}
}
public void UpdateAssemblyInfo(UpdateAssemblyInfo task)
{
var versionVariables = GitVersionVariables(task);
AssemblyInfoFileHelper.CheckForInvalidFiles(this.fileSystem, task.CompileFiles, task.ProjectFile);
if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
{
// Ensure provided output path exists first. Fixes issue #2815.
fileSystem.Directory.CreateDirectory(task.IntermediateOutputPath);
}
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo");
task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
if (!this.fileSystem.Directory.Exists(fileWriteInfo.WorkingDirectory))
{
this.fileSystem.Directory.CreateDirectory(fileWriteInfo.WorkingDirectory);
}
var gitVersionOptions = this.options.Value;
gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory;
gitVersionOptions.AssemblySettingsInfo.UpdateAssemblyInfo = true;
gitVersionOptions.AssemblySettingsInfo.EnsureAssemblyInfo = true;
gitVersionOptions.AssemblySettingsInfo.Files.Add(fileWriteInfo.FileName);
gitVersionOutputTool.UpdateAssemblyInfo(versionVariables);
}
public void GenerateGitVersionInformation(GenerateGitVersionInformation task)
{
var versionVariables = GitVersionVariables(task);
if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
{
// Ensure provided output path exists first. Fixes issue #2815.
fileSystem.Directory.CreateDirectory(task.IntermediateOutputPath);
}
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation");
task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
if (!this.fileSystem.Directory.Exists(fileWriteInfo.WorkingDirectory))
{
this.fileSystem.Directory.CreateDirectory(fileWriteInfo.WorkingDirectory);
}
var gitVersionOptions = this.options.Value;
gitVersionOptions.WorkingDirectory = fileWriteInfo.WorkingDirectory;
var targetNamespace = GetTargetNamespace(task);
gitVersionOutputTool.GenerateGitVersionInformation(versionVariables, fileWriteInfo, targetNamespace);
return;
static string? GetTargetNamespace(GenerateGitVersionInformation task)
{
string? targetNamespace = null;
if (bool.TryParse(task.UseProjectNamespaceForGitVersionInformation, out var useTargetPathAsRootNamespace) && useTargetPathAsRootNamespace)
{
targetNamespace = task.RootNamespace;
if (string.IsNullOrWhiteSpace(targetNamespace))
{
targetNamespace = PathHelper.GetFileNameWithoutExtension(task.ProjectFile);
}
}
return targetNamespace;
}
}
public void WriteVersionInfoToBuildLog(WriteVersionInfoToBuildLog task)
{
var versionVariables = GitVersionVariables(task);
var gitVersionOptions = this.options.Value;
var configuration = this.configurationProvider.Provide(gitVersionOptions.ConfigurationInfo.OverrideConfiguration);
gitVersionOutputTool.OutputVariables(versionVariables, configuration.UpdateBuildNumber);
}
private GitVersionVariables GitVersionVariables(GitVersionTaskBase task) => serializer.FromFile(task.VersionFile);
}