2using System.Collections.Generic;
5using System.Threading.Tasks;
8using Nuke.Common.Tooling;
9using Nuke.Common.Utilities;
23 $
"{args.Arguments:nq}",
24 args.WorkingDirectory,
25 args.EnvironmentVariables,
40 $
"{args.ToolArgs.Arguments:nq}",
41 args.ToolArgs.WorkingDirectory,
42 args.ToolArgs.EnvironmentVariables,
43 args.ToolArgs.Timeout,
44 args.ToolArgs.LogOutput,
45 args.ToolArgs.LogInvocation,
47 args.ToolArgs.ExitHandler,
137 ArgumentStringHandler arguments =
default,
138 string? workingDirectory =
null,
139 IReadOnlyDictionary<string, string>? environmentVariables =
null,
141 bool? logOutput =
null,
142 bool? logInvocation =
null,
143 Action<OutputType, string>? logger =
null,
144 Action<IProcess>? exitHandler =
null
146 arguments.ToStringAndClear(),
148 environmentVariables,
186 ArgumentStringHandler arguments =
default,
187 string? workingDirectory =
null,
188 IReadOnlyDictionary<string, string>? environmentVariables =
null,
190 bool? logOutput =
null,
191 bool? logInvocation =
null,
192 Action<OutputType, string>? logger =
null,
193 Action<IProcess>? exitHandler =
null,
194 Action<StreamWriter>? input =
null,
195 Encoding? standardOutputEncoding =
null,
196 Encoding? standardInputEncoding =
null
199 arguments.ToStringAndClear(),
201 environmentVariables,
209 standardOutputEncoding,
210 standardInputEncoding
220 =>
new(Logger: (t, l) =>
222 if (!(filter?.Invoke(l) ??
true))
return;
224 if (l.ContainsAnyOrdinalIgnoreCase(
"success",
"complete",
"ready",
"start",
"***"))
228 else if (l.ContainsOrdinalIgnoreCase(
"warning"))
232 else if (l.ContainsAnyOrdinalIgnoreCase(
"error",
"fail"))
238 if (normalOutputLogger !=
null)
239 normalOutputLogger(t, l);
254 public static Tool
WithSemanticLogging(
this Tool tool, Func<string, bool>? filter =
null, Action<OutputType, string>? normalOutputLogger =
null)
277 var result = includeParentEnvironment
278 ? EnvironmentInfo.Variables.ToDictionary()
281 result[key] = value.ToString();
282 return new(EnvironmentVariables: result);
290 var parent = includeParentEnvironment
291 ? EnvironmentInfo.Variables.ToDictionary()
293 var itemsDict = items.ToDictionary(i => i.key, i => i.value!.ToString()!);
294 return new(EnvironmentVariables: parent.Merge(itemsDict));
305 public static Tool
WithEnvVar(
this Tool tool,
string key,
object value,
bool includeParentEnvironment =
true)
306 => tool.With(
EnvVar(key, value, includeParentEnvironment));
311 public static Tool
WithEnvVars(
this Tool tool,
bool includeParentEnvironment, params (
string key,
object value)[] items)
312 => tool.With(
EnvVars(includeParentEnvironment, items));
317 public static Tool
WithEnvVars(
this Tool tool, params (
string key,
object value)[] items)
326 EnvironmentInfo.Paths.Union([ path.ToString() ]).JoinSemicolon()
333 => tool.With(
EnvVar(key, value, includeParentEnvironment));
345 => tool.With(
EnvVars(includeParentEnvironment, items));
353 EnvironmentInfo.Paths.Union([ path.ToString() ]).JoinSemicolon()
360 => toolOutput.Select(l =>
new Output
363 Text = l.Text.ReplaceRegex(
"\x1b\\[[0-9;]*[mK]", m =>
"")
381 public static ToolEx Pipe(
this IEnumerable<Output> previous,
ToolEx next,
bool pipeError =
false,
bool close =
true)
382 => next.With(input: s =>
384 foreach (var line
in previous)
386 if (line.Type == OutputType.Std || pipeError)
387 s.WriteLine(line.Text);
389 if (close) s.Close();
399 => tool.With(input: s =>
401 foreach (var line
in lines)
411 => tool.With(input: s => s.WriteLine(line));
425 if (!EnvironmentInfo.IsWin)
return;
426 var processPaths = EnvironmentInfo.Paths;
427 var userPaths = Environment.GetEnvironmentVariable(
"PATH", EnvironmentVariableTarget.User)!.Split(
';');
428 var machinePaths = Environment.GetEnvironmentVariable(
"PATH", EnvironmentVariableTarget.Machine)!.Split(
';');
430 var result = processPaths.Union(userPaths).Union(machinePaths).JoinSemicolon();
431 Environment.SetEnvironmentVariable(
"PATH", result);
438 public static ValueOrError<Tool>
Use(
string tool, Action? setup =
null)
439 =>
ErrorHandling.TryGet(() => ToolResolver.GetPathTool(tool))
440 .
Else(setup !=
null, () =>
442 Log.Warning($
"{tool} was not installed, but it's OK we're installing it now.");
445 return ToolResolver.GetPathTool(tool);
456 public static ValueOrError<Tool>
ElseTrySetup(
this ValueOrError<Tool> result,
bool condition,
string tool, Action setup)
457 => result.Else(condition, () =>
461 return ToolResolver.GetPathTool(tool);
471 public static ValueOrError<Tool>
ElseTrySetup(
this ValueOrError<Tool> result,
string tool, Action setup)
472 => 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)