2using System.Collections.Generic;
5using System.Threading.Tasks;
8using Nuke.Common.Tooling;
9using Nuke.Common.Utilities;
31 $
"{args.Arguments:nq}",
32 args.WorkingDirectory,
33 args.EnvironmentVariables,
48 $
"{args.ToolArgs.Arguments:nq}",
49 args.ToolArgs.WorkingDirectory,
50 args.ToolArgs.EnvironmentVariables,
51 args.ToolArgs.Timeout,
52 args.ToolArgs.LogOutput,
53 args.ToolArgs.LogInvocation,
55 args.ToolArgs.ExitHandler,
57 args.StandardOutputEncoding,
58 args.StandardInputEncoding,
149 string? workingDirectory =
null,
150 IReadOnlyDictionary<string, string>? environmentVariables =
null,
152 bool? logOutput =
null,
153 bool? logInvocation =
null,
154 Action<OutputType, string>? logger =
null,
155 Action<IProcess>? exitHandler =
null
157 arguments.ToStringAndClear(),
159 environmentVariables,
199 string? workingDirectory =
null,
200 IReadOnlyDictionary<string, string>? environmentVariables =
null,
202 bool? logOutput =
null,
203 bool? logInvocation =
null,
204 Action<OutputType, string>? logger =
null,
205 Action<IProcess>? exitHandler =
null,
206 Action<StreamWriter>? input =
null,
207 Encoding? standardOutputEncoding =
null,
208 Encoding? standardInputEncoding =
null,
209 ToolExRetry? retry =
null
212 arguments.ToStringAndClear(),
214 environmentVariables,
222 standardOutputEncoding,
223 standardInputEncoding,
234 =>
new(Logger: (t, l) =>
236 if (!(filter?.Invoke(l) ??
true))
return;
238 if (l.ContainsAnyOrdinalIgnoreCase(
"success",
"complete",
"ready",
"start",
"***"))
242 else if (l.ContainsOrdinalIgnoreCase(
"warning"))
246 else if (l.ContainsAnyOrdinalIgnoreCase(
"error",
"fail"))
252 if (normalOutputLogger !=
null)
253 normalOutputLogger(t, l);
268 public static Tool
WithSemanticLogging(
this Tool tool, Func<string, bool>? filter =
null, Action<OutputType, string>? normalOutputLogger =
null)
291 var result = includeParentEnvironment
292 ? EnvironmentInfo.Variables.ToDictionary()
295 result[key] = value.ToString();
296 return new(EnvironmentVariables: result);
304 var parent = includeParentEnvironment
305 ? EnvironmentInfo.Variables.ToDictionary()
307 var itemsDict = items.ToDictionary(i => i.key, i => i.value!.ToString()!);
308 return new(EnvironmentVariables: parent.Merge(itemsDict));
319 public static Tool
WithEnvVar(
this Tool tool,
string key,
object value,
bool includeParentEnvironment =
true)
320 => tool.With(
EnvVar(key, value, includeParentEnvironment));
325 public static Tool
WithEnvVars(
this Tool tool,
bool includeParentEnvironment, params (
string key,
object value)[] items)
326 => tool.With(
EnvVars(includeParentEnvironment, items));
331 public static Tool
WithEnvVars(
this Tool tool, params (
string key,
object value)[] items)
340 EnvironmentInfo.Paths.Union([ path.ToString() ]).JoinSemicolon()
347 => tool.With(
EnvVar(key, value, includeParentEnvironment));
359 => tool.With(
EnvVars(includeParentEnvironment, items));
367 EnvironmentInfo.Paths.Union([ path.ToString() ]).JoinSemicolon()
374 => toolOutput.Select(l =>
new Output
377 Text = l.Text.ReplaceRegex(
"\x1b\\[[0-9;]*[mK]", m =>
"")
395 public static ToolEx Pipe(
this IEnumerable<Output> previous,
ToolEx next,
bool pipeError =
false,
bool close =
true)
396 => next.With(input: s =>
398 foreach (var line
in previous)
400 if (line.Type == OutputType.Std || pipeError)
401 s.WriteLine(line.Text);
403 if (close) s.Close();
413 => tool.With(input: s =>
415 foreach (var line
in lines)
425 => tool.With(input: s => s.WriteLine(line));
439 if (!EnvironmentInfo.IsWin)
return;
440 var processPaths = EnvironmentInfo.Paths;
441 var userPaths = Environment.GetEnvironmentVariable(
"PATH", EnvironmentVariableTarget.User)!.Split(
';');
442 var machinePaths = Environment.GetEnvironmentVariable(
"PATH", EnvironmentVariableTarget.Machine)!.Split(
';');
444 var result = processPaths.Union(userPaths).Union(machinePaths).JoinSemicolon();
445 Environment.SetEnvironmentVariable(
"PATH", result);
452 public static ValueOrError<ToolEx>
Use(
string tool, Action? setup =
null)
454 .
Else(setup !=
null, () =>
456 Log.Warning($
"{tool} was not installed, but it's OK we're installing it now.");
470 public static ValueOrError<ToolEx>
ElseTrySetup(
this ValueOrError<ToolEx> result,
bool condition,
string tool, Action setup)
471 => result.Else(condition, () =>
485 public static ValueOrError<ToolEx>
ElseTrySetup(
this ValueOrError<ToolEx> result,
string tool, Action setup)
486 => result.ElseTrySetup(
true, tool, setup);
static Attempt Else(this Attempt self, Action action, Action< Exception >? onFailure=null)
If input attempt is an error then attempt to execute another input action (which may also fail)