-
Notifications
You must be signed in to change notification settings - Fork 653
/
GitVersionTaskExecutor.cs
135 lines (115 loc) · 5.28 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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);
DeleteTempFiles();
FileHelper.CheckForInvalidFiles(task.CompileFiles, task.ProjectFile);
if (!string.IsNullOrEmpty(task.IntermediateOutputPath))
{
// Ensure provided output path exists first. Fixes issue #2815.
fileSystem.CreateDirectory(task.IntermediateOutputPath);
}
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "AssemblyInfo");
task.AssemblyInfoTempFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
if (!this.fileSystem.DirectoryExists(fileWriteInfo.WorkingDirectory))
{
this.fileSystem.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.CreateDirectory(task.IntermediateOutputPath);
}
var fileWriteInfo = task.IntermediateOutputPath.GetFileWriteInfo(task.Language, task.ProjectFile, "GitVersionInformation");
task.GitVersionInformationFilePath = PathHelper.Combine(fileWriteInfo.WorkingDirectory, fileWriteInfo.FileName);
if (!this.fileSystem.DirectoryExists(fileWriteInfo.WorkingDirectory))
{
this.fileSystem.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 = Path.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 void DeleteTempFiles()
{
var tempPath = FileHelper.TempPath;
if (!this.fileSystem.DirectoryExists(tempPath))
{
return;
}
foreach (var file in this.fileSystem.GetFiles(tempPath))
{
if (this.fileSystem.GetLastWriteTime(file) >= DateTime.Now.AddDays(-1).Ticks)
continue;
try
{
this.fileSystem.Delete(file);
}
catch (UnauthorizedAccessException)
{
//ignore contention
}
}
}
private GitVersionVariables GitVersionVariables(GitVersionTaskBase task) => serializer.FromFile(task.VersionFile);
}