-
-
Notifications
You must be signed in to change notification settings - Fork 740
/
Copy pathGitVersionRunner.cs
289 lines (254 loc) · 9.85 KB
/
GitVersionRunner.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Text.RegularExpressions;
using Cake.Core;
using Cake.Core.Diagnostics;
using Cake.Core.IO;
using Cake.Core.Tooling;
namespace Cake.Common.Tools.GitVersion
{
/// <summary>
/// The GitVersion runner.
/// </summary>
public sealed class GitVersionRunner : Tool<GitVersionSettings>
{
private readonly ICakeLog _log;
/// <summary>
/// Initializes a new instance of the <see cref="GitVersionRunner"/> class.
/// </summary>
/// <param name="fileSystem">The file system.</param>
/// <param name="environment">The environment.</param>
/// <param name="processRunner">The process runner.</param>
/// <param name="tools">The tool locator.</param>
/// <param name="log">The log.</param>
public GitVersionRunner(
IFileSystem fileSystem,
ICakeEnvironment environment,
IProcessRunner processRunner,
IToolLocator tools,
ICakeLog log) : base(fileSystem, environment, processRunner, tools)
{
_log = log;
}
/// <summary>
/// Runs GitVersion and processes the results.
/// </summary>
/// <param name="settings">The settings.</param>
/// <returns>A task with the GitVersion results.</returns>
public GitVersion Run(GitVersionSettings settings)
{
if (settings == null)
{
throw new ArgumentNullException(nameof(settings));
}
if (settings.OutputType != GitVersionOutput.BuildServer)
{
var output = string.Empty;
Run(settings, GetArguments(settings), new ProcessSettings { RedirectStandardOutput = true }, process =>
{
output = string.Join("\n", process.GetStandardOutput());
if (_log.Verbosity < Verbosity.Diagnostic)
{
var errors = Regex.Matches(output, @"( *ERROR:? [^\n]*)\n([^\n]*)").Cast<Match>()
.SelectMany(match => new[] { match.Groups[1].Value, match.Groups[2].Value });
foreach (var error in errors)
{
_log.Error(error);
}
}
});
var jsonSerializer = new DataContractJsonSerializer(typeof(GitVersionInternal));
using (var jsonStream = new MemoryStream(Encoding.UTF8.GetBytes(output)))
{
return (jsonSerializer.ReadObject(jsonStream) as GitVersionInternal)?.GitVersion;
}
}
Run(settings, GetArguments(settings));
return new GitVersion();
}
private ProcessArgumentBuilder GetArguments(GitVersionSettings settings)
{
var builder = new ProcessArgumentBuilder();
if (settings.OutputType.HasValue)
{
builder.Append("-output");
switch (settings.OutputType.Value)
{
case GitVersionOutput.Json:
builder.Append("json");
break;
case GitVersionOutput.BuildServer:
builder.Append("buildserver");
break;
case GitVersionOutput.File:
builder.Append("file");
if (settings.OutputFile != null)
{
builder.Append("-outputfile");
builder.AppendQuoted(settings.OutputFile.FullPath);
}
break;
}
}
if (!string.IsNullOrWhiteSpace(settings.ShowVariable))
{
builder.Append("-showvariable");
builder.Append(settings.ShowVariable);
}
if (!string.IsNullOrWhiteSpace(settings.UserName))
{
builder.Append("-u");
builder.AppendQuoted(settings.UserName);
builder.Append("-p");
builder.AppendQuotedSecret(settings.Password);
}
if (settings.UpdateAssemblyInfo)
{
builder.Append("-updateassemblyinfo");
if (settings.UpdateAssemblyInfoFilePath != null)
{
builder.AppendQuoted(settings.UpdateAssemblyInfoFilePath.FullPath);
}
}
if (settings.RepositoryPath != null)
{
builder.Append("-targetpath");
builder.AppendQuoted(settings.RepositoryPath.FullPath);
}
else if (!string.IsNullOrWhiteSpace(settings.Url))
{
builder.Append("-url");
builder.AppendQuoted(settings.Url);
if (!string.IsNullOrWhiteSpace(settings.Branch))
{
builder.Append("-b");
builder.Append(settings.Branch);
}
else
{
_log.Warning("If you leave the branch name for GitVersion unset, it will fallback to the default branch for the repository.");
}
if (!string.IsNullOrWhiteSpace(settings.Commit))
{
builder.Append("-c");
builder.AppendQuoted(settings.Commit);
}
if (settings.DynamicRepositoryPath != null)
{
builder.Append("-dynamicRepoLocation");
builder.AppendQuoted(settings.DynamicRepositoryPath.FullPath);
}
}
if (settings.LogFilePath != null)
{
builder.Append("-l");
builder.AppendQuoted(settings.LogFilePath.FullPath);
}
if (settings.ConfigFile != null)
{
builder.Append("-config");
builder.AppendQuoted(settings.ConfigFile.FullPath);
}
if (settings.NoFetch)
{
builder.Append("-nofetch");
}
if (settings.NoCache)
{
builder.Append("-nocache");
}
if (settings.NoNormalize)
{
builder.Append("-nonormalize");
}
if (settings.Diag)
{
builder.Append("-diag");
}
if (settings.UpdateProjectFiles)
{
builder.Append("-updateprojectfiles");
}
if (settings.EnsureAssemblyInfo)
{
builder.Append("-ensureassemblyinfo");
}
if (settings.UpdateWixVersionFile)
{
builder.Append("-updatewixversionfile");
}
if (settings.Verbosity.HasValue)
{
switch (settings.Verbosity.Value)
{
case GitVersionVerbosity.Quiet:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Quiet));
break;
case GitVersionVerbosity.Diagnostic:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Diagnostic));
break;
case GitVersionVerbosity.Verbose:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Verbose));
break;
case GitVersionVerbosity.Normal:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Normal));
break;
case GitVersionVerbosity.Minimal:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Minimal));
break;
}
}
else
{
switch (_log.Verbosity)
{
case Verbosity.Quiet:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Quiet));
break;
case Verbosity.Diagnostic:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Diagnostic));
break;
case Verbosity.Verbose:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Verbose));
break;
case Verbosity.Minimal:
builder.Append("-verbosity");
builder.Append(nameof(Verbosity.Minimal));
break;
}
}
return builder;
}
/// <summary>
/// Gets the name of the tool.
/// </summary>
/// <returns>The name of the tool.</returns>
protected override string GetToolName()
{
return "GitVersion";
}
/// <summary>
/// Gets the possible names of the tool executable.
/// </summary>
/// <returns>The tool executable name.</returns>
protected override IEnumerable<string> GetToolExecutableNames()
{
return new[] { "GitVersion.exe", "dotnet-gitversion", "dotnet-gitversion.exe" };
}
}
}