forked from GitTools/GitVersion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGitVersionExecutor.cs
195 lines (165 loc) · 7.15 KB
/
GitVersionExecutor.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System;
using System.IO;
using System.Reflection;
using System.Threading;
using GitVersion.Configuration;
using GitVersion.Extensions;
using GitVersion.Logging;
using GitVersion.Model;
namespace GitVersion
{
public class GitVersionExecutor : IGitVersionExecutor
{
private readonly ILog log;
private readonly IConsole console;
private readonly IConfigFileLocator configFileLocator;
private readonly IHelpWriter helpWriter;
private readonly IGitRepositoryInfo repositoryInfo;
private readonly IConfigProvider configProvider;
private readonly IGitVersionCalculateTool gitVersionCalculateTool;
private readonly IGitVersionOutputTool gitVersionOutputTool;
private readonly IVersionWriter versionWriter;
public GitVersionExecutor(ILog log, IConsole console,
IConfigFileLocator configFileLocator, IConfigProvider configProvider,
IGitVersionCalculateTool gitVersionCalculateTool, IGitVersionOutputTool gitVersionOutputTool,
IVersionWriter versionWriter, IHelpWriter helpWriter, IGitRepositoryInfo repositoryInfo)
{
this.log = log ?? throw new ArgumentNullException(nameof(log));
this.console = console ?? throw new ArgumentNullException(nameof(console));
this.configFileLocator = configFileLocator ?? throw new ArgumentNullException(nameof(configFileLocator));
this.configProvider = configProvider ?? throw new ArgumentNullException(nameof(configProvider));
this.gitVersionCalculateTool = gitVersionCalculateTool ?? throw new ArgumentNullException(nameof(gitVersionCalculateTool));
this.gitVersionOutputTool = gitVersionOutputTool ?? throw new ArgumentNullException(nameof(gitVersionOutputTool));
this.versionWriter = versionWriter ?? throw new ArgumentNullException(nameof(versionWriter));
this.helpWriter = helpWriter ?? throw new ArgumentNullException(nameof(helpWriter));
this.repositoryInfo = repositoryInfo ?? throw new ArgumentNullException(nameof(repositoryInfo));
}
public int Execute(GitVersionOptions gitVersionOptions)
{
if (!HandleNonMainCommand(gitVersionOptions, out var exitCode))
{
exitCode = RunGitVersionTool(gitVersionOptions);
}
if (exitCode != 0)
{
// Dump log to console if we fail to complete successfully
console.Write(log.ToString());
}
return exitCode;
}
private int RunGitVersionTool(GitVersionOptions gitVersionOptions)
{
var mutexName = repositoryInfo.DotGitDirectory.Replace(Path.DirectorySeparatorChar.ToString(), "");
using var mutex = new Mutex(true, $@"Global\gitversion{mutexName}", out var acquired);
try
{
if (!acquired)
{
mutex.WaitOne();
}
var variables = gitVersionCalculateTool.CalculateVersionVariables();
var configuration = configProvider.Provide(overrideConfig: gitVersionOptions.ConfigInfo.OverrideConfig);
gitVersionOutputTool.OutputVariables(variables, configuration.UpdateBuildNumber ?? true);
gitVersionOutputTool.UpdateAssemblyInfo(variables);
gitVersionOutputTool.UpdateWixVersionFile(variables);
}
catch (WarningException exception)
{
var error = $"An error occurred:{System.Environment.NewLine}{exception.Message}";
log.Warning(error);
return 1;
}
catch (Exception exception)
{
var error = $"An unexpected error occurred:{System.Environment.NewLine}{exception}";
log.Error(error);
if (gitVersionOptions == null) return 1;
log.Info("Attempting to show the current git graph (please include in issue): ");
log.Info("Showing max of 100 commits");
try
{
GitExtensions.DumpGraph(gitVersionOptions.WorkingDirectory, mess => log.Info(mess), 100);
}
catch (Exception dumpGraphException)
{
log.Error("Couldn't dump the git graph due to the following error: " + dumpGraphException);
}
return 1;
}
finally
{
mutex.ReleaseMutex();
}
return 0;
}
private bool HandleNonMainCommand(GitVersionOptions gitVersionOptions, out int exitCode)
{
if (gitVersionOptions == null)
{
helpWriter.Write();
exitCode = 1;
return true;
}
if (gitVersionOptions.IsVersion)
{
var assembly = Assembly.GetExecutingAssembly();
versionWriter.Write(assembly);
exitCode = 0;
return true;
}
if (gitVersionOptions.IsHelp)
{
helpWriter.Write();
exitCode = 0;
return true;
}
if (gitVersionOptions.Diag)
{
gitVersionOptions.Settings.NoCache = true;
gitVersionOptions.Output.Add(OutputType.BuildServer);
}
ConfigureLogging(gitVersionOptions, log);
var workingDirectory = gitVersionOptions.WorkingDirectory;
if (gitVersionOptions.Diag)
{
log.Info("Dumping commit graph: ");
GitExtensions.DumpGraph(workingDirectory, mess => log.Info(mess), 100);
}
if (!Directory.Exists(workingDirectory))
{
log.Warning($"The working directory '{workingDirectory}' does not exist.");
}
else
{
log.Info("Working directory: " + workingDirectory);
}
configFileLocator.Verify(gitVersionOptions, repositoryInfo);
if (gitVersionOptions.Init)
{
configProvider.Init(workingDirectory);
exitCode = 0;
return true;
}
if (gitVersionOptions.ConfigInfo.ShowConfig)
{
var config = configProvider.Provide(workingDirectory);
console.WriteLine(config.ToString());
exitCode = 0;
return true;
}
exitCode = 0;
return false;
}
private static void ConfigureLogging(GitVersionOptions gitVersionOptions, ILog log)
{
if (gitVersionOptions.Output.Contains(OutputType.BuildServer) || gitVersionOptions.LogFilePath == "console" || gitVersionOptions.Init)
{
log.AddLogAppender(new ConsoleAppender());
}
if (gitVersionOptions.LogFilePath != null && gitVersionOptions.LogFilePath != "console")
{
log.AddLogAppender(new FileAppender(gitVersionOptions.LogFilePath));
}
}
}
}