-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandLine.cs
105 lines (89 loc) · 3.47 KB
/
CommandLine.cs
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
// ReSharper disable UnusedMember.Global
// ReSharper disable MemberCanBeProtected.Global
// ReSharper disable ReturnTypeCanBeEnumerable.Global
namespace HostApi;
using System.Diagnostics;
using System.Text;
/// <summary>
/// Runs an arbitrary executable with arguments and environment variables from the working directory.
/// <example>
/// <code>
/// var cmd = new CommandLine("whoami");
/// cmd.Run().EnsureSuccess();
/// </code>
/// </example>
/// </summary>
/// <param name="ExecutablePath">Specifies the application executable path.</param>
/// <param name="WorkingDirectory">Specifies the working directory for the application to be started.</param>
/// <param name="Args">Specifies the set of command line arguments to use when starting the application.</param>
/// <param name="Vars">Specifies the set of environment variables that apply to this process and its child processes.</param>
/// <param name="ShortName">Specifies a short name for this command line.</param>
/// <seealso cref="ICommandLineRunner.Run"/>
/// <seealso cref="ICommandLineRunner.RunAsync"/>
/// <seealso cref="IBuildRunner.Build"/>
/// <seealso cref="IBuildRunner.BuildAsync"/>
[Target]
[DebuggerTypeProxy(typeof(CommandLineDebugView))]
public partial record CommandLine(
string ExecutablePath,
string WorkingDirectory,
IEnumerable<string> Args,
IEnumerable<(string name, string value)> Vars,
string ShortName = "")
: IStartInfo
{
private readonly string _shortName = ShortName;
/// <summary>
/// Creates a new command line.
/// </summary>
/// <param name="executablePath">Path to the executable file.</param>
/// <param name="args">Command line arguments.</param>
public CommandLine(string executablePath, params string[] args)
: this(executablePath, string.Empty, args, Array.Empty<(string name, string value)>())
{ }
internal CommandLine(IStartInfo startInfo)
: this(startInfo.ExecutablePath, startInfo.WorkingDirectory, startInfo.Args, startInfo.Vars, startInfo.ShortName)
{ }
/// <inheritdoc/>
public string ShortName =>
!string.IsNullOrWhiteSpace(_shortName)
? _shortName
: Path.GetFileNameWithoutExtension(ExecutablePath);
/// <inheritdoc/>
public IStartInfo GetStartInfo(IHost host)
{
if (host == null) throw new ArgumentNullException(nameof(host));
return this;
}
/// <inheritdoc/>
public override string ToString()
{
var sb = new StringBuilder();
if (!string.IsNullOrWhiteSpace(ShortName))
{
sb.Append(ShortName);
sb.Append(": ");
}
sb.Append(Escape(ExecutablePath));
foreach (var arg in Args)
{
sb.Append(' ');
sb.Append(Escape(arg));
}
return sb.ToString();
}
private static string Escape(string text) =>
!text.TrimStart().StartsWith("\"") && text.Contains(' ')
? $"\"{text}\""
: text;
internal class CommandLineDebugView(IStartInfo startInfo)
{
public string ShortName => startInfo.ShortName;
public string ExecutablePath => startInfo.ExecutablePath;
public string WorkingDirectory => startInfo.WorkingDirectory;
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public IEnumerable<string> Args => startInfo.Args;
[DebuggerBrowsable(DebuggerBrowsableState.Collapsed)]
public IEnumerable<(string name, string value)> Vars => startInfo.Vars;
}
}