Nuke.Cola
Loading...
Searching...
No Matches
ToolArguments.cs
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Threading.Tasks;
5using Nuke.Common.Tooling;
6using Nuke.Common.Utilities;
7using Nuke.Common.Utilities.Collections;
8using Serilog;
9
10namespace Nuke.Cola.Tooling;
11
12/// <summary>
13/// A record listing Tool delegate parameters and provides a way to meaningfully merge multiple together
14/// </summary>
15/// <param name="Arguments"></param>
16/// <param name="WorkingDirectory"></param>
17/// <param name="EnvironmentVariables"></param>
18/// <param name="Timeout"></param>
19/// <param name="LogOutput"></param>
20/// <param name="LogInvocation"></param>
21/// <param name="Logger"></param>
22/// <param name="ExitHandler"></param>
23public record class ToolArguments(
24 string? Arguments = null,
25 string? WorkingDirectory = null,
26 IReadOnlyDictionary<string, string>? EnvironmentVariables = null,
27 int? Timeout = null,
28 bool? LogOutput = null,
29 bool? LogInvocation = null,
30 Action<OutputType, string>? Logger = null,
31 Action<IProcess>? ExitHandler = null
32) {
33
34 /// <summary>
35 /// Merge two Tool argument records together.
36 /// </summary>
37 /// <remarks>
38 /// <list>
39 /// <item><term>Arguments </term><description> will be concatenated</description></item>
40 /// <item><term>Working directory </term><description> B overrides the one from A but not when B doesn't have one</description></item>
41 /// <item><term>Environmnent variables </term><description> will be merged</description></item>
42 /// <item><term>TimeOut </term><description> will be maxed</description></item>
43 /// <item><term>LogOutput </term><description> is OR-ed</description></item>
44 /// <item><term>LogInvocation </term><description> is OR-ed</description></item>
45 /// <item><term>Logger / ExitHandler </term><description> A + B is invoked</description></item>
46 /// </list>
47 /// </remarks>
48 public static ToolArguments operator | (ToolArguments? a, ToolArguments? b)
49 {
50 var timeOut = Math.Max(a?.Timeout ?? -1, b?.Timeout ?? -1);
51 return new() {
52 Arguments = string.Join(' ',
53 new [] {a?.Arguments, b?.Arguments}
54 .Where(_ => !string.IsNullOrWhiteSpace(_))
55 ),
56
57 WorkingDirectory = string.IsNullOrWhiteSpace(b?.WorkingDirectory)
58 ? a?.WorkingDirectory
59 : b?.WorkingDirectory,
60
61 EnvironmentVariables = a?.EnvironmentVariables.Merge(b?.EnvironmentVariables),
62
63 Timeout = timeOut < 0 ? null : timeOut,
64
65 LogOutput = (a?.LogOutput == null && b?.LogOutput == null)
66 ? null
67 : (a?.LogOutput ?? false) || (b?.LogOutput ?? false),
68
69 LogInvocation = (a?.LogInvocation == null && b?.LogInvocation == null)
70 ? null
71 : (a?.LogInvocation ?? false) || (b?.LogInvocation ?? false),
72
73 Logger = a?.Logger + b?.Logger,
74
75 ExitHandler = a?.ExitHandler + b?.ExitHandler
76 };
77 }
78}
Extension class for dealing with passing arguments from the user through nuke to a tool.
Definition Arguments.cs:15
record class ToolArguments(string? Arguments=null, string? WorkingDirectory=null, IReadOnlyDictionary< string, string >? EnvironmentVariables=null, int? Timeout=null, bool? LogOutput=null, bool? LogInvocation=null, Action< OutputType, string >? Logger=null, Action< IProcess >? ExitHandler=null)
A record listing Tool delegate parameters and provides a way to meaningfully merge multiple together.