Skip to content

Commit

Permalink
replace cmd.exe usage with optional params for windows command name a…
Browse files Browse the repository at this point in the history
…nd args
  • Loading branch information
adamralph committed Jun 1, 2019
1 parent 976eda9 commit 309ef2a
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 16 deletions.
24 changes: 16 additions & 8 deletions SimpleExec/Command.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,18 @@ public static class Command
/// <param name="args">The arguments to pass to the command.</param>
/// <param name="workingDirectory">The working directory in which to run the command.</param>
/// <param name="noEcho">Whether or not to echo the resulting command line and working directory (if specified) to standard error (stderr).</param>
/// <param name="windowsName">The name of the command to use on Windows only.</param>
/// <param name="windowsArgs">The arguments to pass to the command on Windows only.</param>
/// <exception cref="CommandException">The command exited with non-zero exit code.</exception>
/// <remarks>
/// By default, the resulting command line and the working directory (if specified) are echoed to standard error (stderr).
/// To suppress this behavior, provide the <paramref name="noEcho"/> parameter with a value of <c>true</c>.
/// </remarks>
public static void Run(string name, string args = null, string workingDirectory = null, bool noEcho = false)
public static void Run(string name, string args = null, string workingDirectory = null, bool noEcho = false, string windowsName = null, string windowsArgs = null)
{
using (var process = new Process())
{
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, false);
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, false, windowsName, windowsArgs);
process.Run(noEcho);

if (process.ExitCode != 0)
Expand All @@ -43,17 +45,19 @@ public static void Run(string name, string args = null, string workingDirectory
/// <param name="args">The arguments to pass to the command.</param>
/// <param name="workingDirectory">The working directory in which to run the command.</param>
/// <param name="noEcho">Whether or not to echo the resulting command line and working directory (if specified) to standard error (stderr).</param>
/// <param name="windowsName">The name of the command to use on Windows only.</param>
/// <param name="windowsArgs">The arguments to pass to the command on Windows only.</param>
/// <returns>A <see cref="Task"/> that represents the asynchronous running of the command.</returns>
/// <exception cref="CommandException">The command exited with non-zero exit code.</exception>
/// <remarks>
/// By default, the resulting command line and the working directory (if specified) are echoed to standard error (stderr).
/// To suppress this behavior, provide the <paramref name="noEcho"/> parameter with a value of <c>true</c>.
/// </remarks>
public static async Task RunAsync(string name, string args = null, string workingDirectory = null, bool noEcho = false)
public static async Task RunAsync(string name, string args = null, string workingDirectory = null, bool noEcho = false, string windowsName = null, string windowsArgs = null)
{
using (var process = new Process())
{
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, false);
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, false, windowsName, windowsArgs);
await process.RunAsync(noEcho).ConfigureAwait(false);

if (process.ExitCode != 0)
Expand All @@ -71,17 +75,19 @@ public static async Task RunAsync(string name, string args = null, string workin
/// <param name="args">The arguments to pass to the command.</param>
/// <param name="workingDirectory">The working directory in which to run the command.</param>
/// <param name="noEcho">Whether or not to echo the resulting command line and working directory (if specified) to standard error (stderr).</param>
/// <param name="windowsName">The name of the command to use on Windows only.</param>
/// <param name="windowsArgs">The arguments to pass to the command on Windows only.</param>
/// <returns>A <see cref="string"/> representing the contents of standard output (stdout).</returns>
/// <exception cref="CommandException">The command exited with non-zero exit code.</exception>
/// <remarks>
/// By default, the resulting command line and the working directory (if specified) are echoed to standard error (stderr).
/// To suppress this behavior, provide the <paramref name="noEcho"/> parameter with a value of <c>true</c>.
/// </remarks>
public static string Read(string name, string args = null, string workingDirectory = null, bool noEcho = false)
public static string Read(string name, string args = null, string workingDirectory = null, bool noEcho = false, string windowsName = null, string windowsArgs = null)
{
using (var process = new Process())
{
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, true);
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, true, windowsName, windowsArgs);

var runProcess = process.RunAsync(noEcho);
var readOutput = process.StandardOutput.ReadToEndAsync();
Expand All @@ -105,6 +111,8 @@ public static string Read(string name, string args = null, string workingDirecto
/// <param name="args">The arguments to pass to the command.</param>
/// <param name="workingDirectory">The working directory in which to run the command.</param>
/// <param name="noEcho">Whether or not to echo the resulting command line and working directory (if specified) to standard error (stderr).</param>
/// <param name="windowsName">The name of the command to use on Windows only.</param>
/// <param name="windowsArgs">The arguments to pass to the command on Windows only.</param>
/// <returns>
/// A <see cref="Task{string}"/> representing the asynchronous running of the command and reading of standard output (stdout).
/// The task result contains the contents of standard output (stdout).
Expand All @@ -114,11 +122,11 @@ public static string Read(string name, string args = null, string workingDirecto
/// By default, the resulting command line and the working directory (if specified) are echoed to standard error (stderr).
/// To suppress this behavior, provide the <paramref name="noEcho"/> parameter with a value of <c>true</c>.
/// </remarks>
public static async Task<string> ReadAsync(string name, string args = null, string workingDirectory = null, bool noEcho = false)
public static async Task<string> ReadAsync(string name, string args = null, string workingDirectory = null, bool noEcho = false, string windowsName = null, string windowsArgs = null)
{
using (var process = new Process())
{
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, true);
process.StartInfo = ProcessStartInfo.Create(name, args, workingDirectory, true, windowsName, windowsArgs);

var runProcess = process.RunAsync(noEcho);
var readOutput = process.StandardOutput.ReadToEndAsync();
Expand Down
6 changes: 3 additions & 3 deletions SimpleExec/ProcessStartInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ namespace SimpleExec
internal static class ProcessStartInfo
{
public static System.Diagnostics.ProcessStartInfo Create(
string name, string args, string workingDirectory, bool captureOutput) =>
string name, string args, string workingDirectory, bool captureOutput, string windowsName, string windowsArgs) =>
(RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
? new System.Diagnostics.ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = $"/c \"\"{name}\" {args}\"",
FileName = windowsName ?? name,
Arguments = windowsArgs ?? args,
WorkingDirectory = workingDirectory,
UseShellExecute = false,
RedirectStandardError = false,
Expand Down
2 changes: 1 addition & 1 deletion SimpleExec/SimpleExec.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<Description>Runs external commands.</Description>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<LangVersion>latest</LangVersion>
<MinVerMinimumMajorMinor>4.0</MinVerMinimumMajorMinor>
<MinVerMinimumMajorMinor>6.0</MinVerMinimumMajorMinor>
<PackageIconUrl>https://raw.githubusercontent.com/adamralph/simple-exec/master/assets/simple-exec.png</PackageIconUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageProjectUrl>https://github.com/adamralph/simple-exec</PackageProjectUrl>
Expand Down
8 changes: 4 additions & 4 deletions SimpleExecTests/api-netcoreapp2_2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ namespace SimpleExec
{
public class static Command
{
public static string Read(string name, string args = null, string workingDirectory = null, bool noEcho = False) { }
public static System.Threading.Tasks.Task<string> ReadAsync(string name, string args = null, string workingDirectory = null, bool noEcho = False) { }
public static void Run(string name, string args = null, string workingDirectory = null, bool noEcho = False) { }
public static System.Threading.Tasks.Task RunAsync(string name, string args = null, string workingDirectory = null, bool noEcho = False) { }
public static string Read(string name, string args = null, string workingDirectory = null, bool noEcho = False, string windowsName = null, string windowsArgs = null) { }
public static System.Threading.Tasks.Task<string> ReadAsync(string name, string args = null, string workingDirectory = null, bool noEcho = False, string windowsName = null, string windowsArgs = null) { }
public static void Run(string name, string args = null, string workingDirectory = null, bool noEcho = False, string windowsName = null, string windowsArgs = null) { }
public static System.Threading.Tasks.Task RunAsync(string name, string args = null, string workingDirectory = null, bool noEcho = False, string windowsName = null, string windowsArgs = null) { }
}
public class NonZeroExitCodeException : System.Exception
{
Expand Down

0 comments on commit 309ef2a

Please sign in to comment.