forked from cake-build/cake
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDotNetAliases.Tool.Common.cs
More file actions
308 lines (271 loc) · 13.3 KB
/
Copy pathDotNetAliases.Tool.Common.cs
File metadata and controls
308 lines (271 loc) · 13.3 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// 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 Cake.Common.Tools.DotNet.Tool;
using Cake.Core;
using Cake.Core.Configuration;
using Cake.Core.IO;
namespace Cake.Common.Tools.DotNet
{
/// <summary>
/// <para>Contains functionality related to <see href="https://github.com/dotnet/cli">.NET CLI</see>.</para>
/// <para>
/// In order to use the commands for this alias, the .NET CLI tools will need to be installed on the machine where
/// the Cake script is being executed. See this <see href="https://www.microsoft.com/net/core">page</see> for information
/// on how to install.
/// </para>
/// </summary>
public static partial class DotNetAliases
{
private static void RunDotNetToolCommand(
ICakeContext context,
FilePath projectPath,
ProcessArgumentBuilder arguments,
DotNetToolSettings settings)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(arguments);
ArgumentNullException.ThrowIfNull(settings);
context.DotNetTool(projectPath, "tool", arguments, settings);
}
private static void RunDotNetToolCommandWithoutVerbosity(
ICakeContext context,
FilePath projectPath,
ProcessArgumentBuilder arguments,
DotNetToolSettings settings)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(arguments);
ArgumentNullException.ThrowIfNull(settings);
var verbosity = settings.Verbosity;
settings.Verbosity = null;
try
{
context.DotNetTool(projectPath, "tool", arguments, settings);
}
finally
{
settings.Verbosity = verbosity;
}
}
private static void RunDotNetToolCommandWithForwardedArguments(
ICakeContext context,
FilePath projectPath,
ProcessArgumentBuilder arguments,
ProcessArgumentBuilder forwardedArguments,
DotNetToolSettings settings)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(arguments);
ArgumentNullException.ThrowIfNull(settings);
if (forwardedArguments.IsNullOrEmpty())
{
context.DotNetTool(projectPath, "tool", arguments, settings);
return;
}
var argumentCustomization = settings.ArgumentCustomization;
settings.ArgumentCustomization = dotnetArguments =>
{
var customizedArguments = argumentCustomization?.Invoke(dotnetArguments) ?? dotnetArguments;
customizedArguments.Append("--");
forwardedArguments.CopyTo(customizedArguments);
return customizedArguments;
};
try
{
context.DotNetTool(projectPath, "tool", arguments, settings);
}
finally
{
settings.ArgumentCustomization = argumentCustomization;
}
}
private static void RunDotNetToolCommandWithForwardedArgumentsWithoutVerbosity(
ICakeContext context,
FilePath projectPath,
ProcessArgumentBuilder arguments,
ProcessArgumentBuilder forwardedArguments,
DotNetToolSettings settings)
{
ArgumentNullException.ThrowIfNull(context);
ArgumentNullException.ThrowIfNull(arguments);
ArgumentNullException.ThrowIfNull(settings);
var verbosity = settings.Verbosity;
settings.Verbosity = null;
try
{
RunDotNetToolCommandWithForwardedArguments(context, projectPath, arguments, forwardedArguments, settings);
}
finally
{
settings.Verbosity = verbosity;
}
}
private static void AppendToolExecuteSettings(ProcessArgumentBuilder builder, DotNetToolExecuteSettings settings, ICakeEnvironment environment)
{
AppendPackageResolutionSettings(builder, settings.Version, settings.ConfigFile, settings.Source, settings.AddSource, settings.Prerelease, environment);
AppendSwitch(builder, "--allow-roll-forward", settings.AllowRollForward);
AppendRestoreSettings(builder, settings.DisableParallel, settings.IgnoreFailedSources, settings.NoHttpCache, settings.Interactive);
}
private static void AppendToolInstallSettings(ProcessArgumentBuilder builder, DotNetToolInstallSettings settings, ICakeConfiguration configuration, ICakeEnvironment environment)
{
AppendInstallationScope(builder, settings.InstallationScope, settings.ToolInstallationPath, settings.WorkingDirectory, configuration, environment);
AppendPackageResolutionSettings(builder, settings.Version, settings.ConfigFile, settings.Source, settings.AddSource, settings.Prerelease, environment);
AppendFilePath(builder, "--tool-manifest", settings.ToolManifest, environment);
AppendString(builder, "--framework", settings.Framework);
AppendRestoreSettings(builder, settings.DisableParallel, settings.IgnoreFailedSources, settings.NoHttpCache, settings.Interactive);
AppendSwitch(builder, "--allow-downgrade", settings.AllowDowngrade);
AppendString(builder, "--arch", settings.Architecture);
AppendSwitch(builder, "--create-manifest-if-needed", settings.CreateManifestIfNeeded);
AppendSwitch(builder, "--allow-roll-forward", settings.AllowRollForward);
}
private static void AppendToolListSettings(ProcessArgumentBuilder builder, DotNetToolListSettings settings, ICakeConfiguration configuration, ICakeEnvironment environment)
{
AppendInstallationScope(builder, settings.InstallationScope, settings.ToolInstallationPath, settings.WorkingDirectory, configuration, environment);
if (settings.Format.HasValue)
{
builder.AppendSwitch("--format", settings.Format.Value == DotNetToolListFormat.Json ? "json" : "table");
}
}
private static void AppendToolRestoreSettings(ProcessArgumentBuilder builder, DotNetToolRestoreSettings settings, ICakeEnvironment environment)
{
AppendFilePath(builder, "--configfile", settings.ConfigFile, environment);
AppendSources(builder, "--add-source", settings.AddSource);
AppendFilePath(builder, "--tool-manifest", settings.ToolManifest, environment);
AppendRestoreSettings(builder, settings.DisableParallel, settings.IgnoreFailedSources, settings.NoHttpCache, settings.Interactive);
}
private static void AppendToolRunSettings(ProcessArgumentBuilder builder, DotNetToolRunSettings settings)
{
AppendSwitch(builder, "--allow-roll-forward", settings.AllowRollForward);
}
private static void AppendToolSearchSettings(ProcessArgumentBuilder builder, DotNetToolSearchSettings settings)
{
AppendSwitch(builder, "--detail", settings.Detail);
if (settings.Skip.HasValue)
{
builder.AppendSwitch("--skip", settings.Skip.Value.ToString());
}
if (settings.Take.HasValue)
{
builder.AppendSwitch("--take", settings.Take.Value.ToString());
}
AppendSwitch(builder, "--prerelease", settings.Prerelease);
}
private static void AppendToolUninstallSettings(ProcessArgumentBuilder builder, DotNetToolUninstallSettings settings, ICakeConfiguration configuration, ICakeEnvironment environment)
{
AppendInstallationScope(builder, settings.InstallationScope, settings.ToolInstallationPath, settings.WorkingDirectory, configuration, environment);
AppendFilePath(builder, "--tool-manifest", settings.ToolManifest, environment);
}
private static void AppendToolUpdateSettings(ProcessArgumentBuilder builder, DotNetToolUpdateSettings settings, ICakeConfiguration configuration, ICakeEnvironment environment)
{
AppendInstallationScope(builder, settings.InstallationScope, settings.ToolInstallationPath, settings.WorkingDirectory, configuration, environment);
AppendPackageResolutionSettings(builder, settings.Version, settings.ConfigFile, settings.Source, settings.AddSource, settings.Prerelease, environment);
AppendFilePath(builder, "--tool-manifest", settings.ToolManifest, environment);
AppendString(builder, "--framework", settings.Framework);
AppendRestoreSettings(builder, settings.DisableParallel, settings.IgnoreFailedSources, settings.NoHttpCache, settings.Interactive);
AppendSwitch(builder, "--allow-downgrade", settings.AllowDowngrade);
AppendSwitch(builder, "--all", settings.All);
}
private static void AppendInstallationScope(
ProcessArgumentBuilder builder,
DotNetToolInstallationScope scope,
DirectoryPath toolInstallationPath,
DirectoryPath settingsWorkingDirectory,
ICakeConfiguration configuration,
ICakeEnvironment environment)
{
switch (scope)
{
case DotNetToolInstallationScope.Global:
builder.Append("--global");
break;
case DotNetToolInstallationScope.Local:
builder.Append("--local");
break;
case DotNetToolInstallationScope.ToolPath:
var root = settingsWorkingDirectory ?? environment.WorkingDirectory;
var path = toolInstallationPath ?? configuration.GetToolPath(root, environment);
AppendDirectoryPath(builder, "--tool-path", path, environment);
break;
}
}
private static void AppendPackageResolutionSettings(
ProcessArgumentBuilder builder,
string version,
FilePath configFile,
ICollection<string> source,
ICollection<string> addSource,
bool prerelease,
ICakeEnvironment environment)
{
AppendString(builder, "--version", version);
AppendFilePath(builder, "--configfile", configFile, environment);
AppendSources(builder, "--source", source);
AppendSources(builder, "--add-source", addSource);
AppendSwitch(builder, "--prerelease", prerelease);
}
private static void AppendRestoreSettings(ProcessArgumentBuilder builder, bool disableParallel, bool ignoreFailedSources, bool noHttpCache, bool interactive)
{
AppendSwitch(builder, "--disable-parallel", disableParallel);
AppendSwitch(builder, "--ignore-failed-sources", ignoreFailedSources);
AppendSwitch(builder, "--no-http-cache", noHttpCache);
AppendSwitch(builder, "--interactive", interactive);
}
private static void AppendRequiredArgument(ProcessArgumentBuilder builder, string argument, string parameterName)
{
if (string.IsNullOrWhiteSpace(argument))
{
throw new ArgumentNullException(parameterName);
}
builder.Append(argument);
}
private static void AppendOptionalArgument(ProcessArgumentBuilder builder, string argument)
{
if (!string.IsNullOrWhiteSpace(argument))
{
builder.Append(argument);
}
}
private static void AppendSwitch(ProcessArgumentBuilder builder, string switchName, bool value)
{
if (value)
{
builder.Append(switchName);
}
}
private static void AppendString(ProcessArgumentBuilder builder, string switchName, string value)
{
if (!string.IsNullOrWhiteSpace(value))
{
builder.AppendSwitchQuoted(switchName, value);
}
}
private static void AppendFilePath(ProcessArgumentBuilder builder, string switchName, FilePath value, ICakeEnvironment environment)
{
if (value != null)
{
builder.AppendSwitchQuoted(switchName, value.MakeAbsolute(environment).FullPath);
}
}
private static void AppendDirectoryPath(ProcessArgumentBuilder builder, string switchName, DirectoryPath value, ICakeEnvironment environment)
{
if (value != null)
{
builder.AppendSwitchQuoted(switchName, value.MakeAbsolute(environment).FullPath);
}
}
private static void AppendSources(ProcessArgumentBuilder builder, string switchName, ICollection<string> sources)
{
if (sources == null)
{
return;
}
foreach (var source in sources)
{
AppendString(builder, switchName, source);
}
}
}
}