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
148 string? workingDirectory =
null,
149 IReadOnlyDictionary<string, string>? environmentVariables =
null,
151 bool? logOutput =
null,
152 bool? logInvocation =
null,
153 Action<OutputType, string>? logger =
null,
154 Action<IProcess>? exitHandler =
null
156 arguments.ToStringAndClear(),
158 environmentVariables,
197 string? workingDirectory =
null,
198 IReadOnlyDictionary<string, string>? environmentVariables =
null,
200 bool? logOutput =
null,
201 bool? logInvocation =
null,
202 Action<OutputType, string>? logger =
null,
203 Action<IProcess>? exitHandler =
null,
204 Action<StreamWriter>? input =
null,
205 Encoding? standardOutputEncoding =
null,
206 Encoding? standardInputEncoding =
null
209 arguments.ToStringAndClear(),
211 environmentVariables,
219 standardOutputEncoding,
220 standardInputEncoding
230 =>
new(Logger: (t, l) =>
232 if (!(filter?.Invoke(l) ??
true))
return;
234 if (l.ContainsAnyOrdinalIgnoreCase(
"success",
"complete",
"ready",
"start",
"***"))
238 else if (l.ContainsOrdinalIgnoreCase(
"warning"))
242 else if (l.ContainsAnyOrdinalIgnoreCase(
"error",
"fail"))
248 if (normalOutputLogger !=
null)
249 normalOutputLogger(t, l);
264 public static Tool
WithSemanticLogging(
this Tool tool, Func<string, bool>? filter =
null, Action<OutputType, string>? normalOutputLogger =
null)
287 var result = includeParentEnvironment
288 ? EnvironmentInfo.Variables.ToDictionary()
291 result[key] = value.ToString();
292 return new(EnvironmentVariables: result);
300 var parent = includeParentEnvironment
301 ? EnvironmentInfo.Variables.ToDictionary()
303 var itemsDict = items.ToDictionary(i => i.key, i => i.value!.ToString()!);
304 return new(EnvironmentVariables: parent.Merge(itemsDict));
315 public static Tool
WithEnvVar(
this Tool tool,
string key,
object value,
bool includeParentEnvironment =
true)
316 => tool.With(
EnvVar(key, value, includeParentEnvironment));
321 public static Tool
WithEnvVars(
this Tool tool,
bool includeParentEnvironment, params (
string key,
object value)[] items)
322 => tool.With(
EnvVars(includeParentEnvironment, items));
327 public static Tool
WithEnvVars(
this Tool tool, params (
string key,
object value)[] items)
336 EnvironmentInfo.Paths.Union([ path.ToString() ]).JoinSemicolon()
343 => tool.With(
EnvVar(key, value, includeParentEnvironment));
355 => tool.With(
EnvVars(includeParentEnvironment, items));
363 EnvironmentInfo.Paths.Union([ path.ToString() ]).JoinSemicolon()
370 => toolOutput.Select(l =>
new Output
373 Text = l.Text.ReplaceRegex(
"\x1b\\[[0-9;]*[mK]", m =>
"")
391 public static ToolEx Pipe(
this IEnumerable<Output> previous,
ToolEx next,
bool pipeError =
false,
bool close =
true)
392 => next.With(input: s =>
394 foreach (var line
in previous)
396 if (line.Type == OutputType.Std || pipeError)
397 s.WriteLine(line.Text);
399 if (close) s.Close();
409 => tool.With(input: s =>
411 foreach (var line
in lines)
421 => tool.With(input: s => s.WriteLine(line));
435 if (!EnvironmentInfo.IsWin)
return;
436 var processPaths = EnvironmentInfo.Paths;
437 var userPaths = Environment.GetEnvironmentVariable(
"PATH", EnvironmentVariableTarget.User)!.Split(
';');
438 var machinePaths = Environment.GetEnvironmentVariable(
"PATH", EnvironmentVariableTarget.Machine)!.Split(
';');
440 var result = processPaths.Union(userPaths).Union(machinePaths).JoinSemicolon();
441 Environment.SetEnvironmentVariable(
"PATH", result);
448 public static ValueOrError<Tool>
Use(
string tool, Action? setup =
null)
449 =>
ErrorHandling.TryGet(() => ToolResolver.GetPathTool(tool))
450 .
Else(setup !=
null, () =>
452 Log.Warning($
"{tool} was not installed, but it's OK we're installing it now.");
455 return ToolResolver.GetPathTool(tool);
466 public static ValueOrError<Tool>
ElseTrySetup(
this ValueOrError<Tool> result,
bool condition,
string tool, Action setup)
467 => result.Else(condition, () =>
471 return ToolResolver.GetPathTool(tool);
481 public static ValueOrError<Tool>
ElseTrySetup(
this ValueOrError<Tool> result,
string tool, Action setup)
482 => 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)