Skip to content

Commit

Permalink
feat: add configure ProcessStartInfo delegate for CommandExecutor
Browse files Browse the repository at this point in the history
  • Loading branch information
WeihanLi committed Nov 11, 2023
1 parent c9f95cf commit 3d7ef08
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions src/WeihanLi.Common/Helpers/CommandExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,28 @@ public static class CommandExecutor
/// </summary>
/// <param name="command">command with arguments</param>
/// <param name="workingDirectory">working directory for the command</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <returns>exit code</returns>
public static int ExecuteCommand(string command, string? workingDirectory = null)
public static int ExecuteCommand(string command, string? workingDirectory = null, Action<ProcessStartInfo>? configure = null)
{
Guard.NotNullOrEmpty(command);
var cmd = command.Split(new[] { ' ' }, 2);
return Execute(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory);
return Execute(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure);
}

/// <summary>
/// Execute command async
/// </summary>
/// <param name="command">command with arguments</param>
/// <param name="workingDirectory">working directory for the command</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <param name="cancellationToken">cancellationToken</param>
/// <returns>exit code</returns>
public static Task<int> ExecuteCommandAsync(string command, string? workingDirectory = null, CancellationToken cancellationToken = default)
public static Task<int> ExecuteCommandAsync(string command, string? workingDirectory = null, Action<ProcessStartInfo>? configure = null, CancellationToken cancellationToken = default)
{
Guard.NotNullOrEmpty(command);
var cmd = command.Split(new[] { ' ' }, 2);
return ExecuteAsync(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory);
return ExecuteAsync(cmd[0], cmd.Length > 1 ? cmd[1] : null, workingDirectory, configure, cancellationToken);
}

/// <summary>
Expand All @@ -41,16 +43,18 @@ public static Task<int> ExecuteCommandAsync(string command, string? workingDirec
/// <param name="commandPath">executable command path</param>
/// <param name="arguments">command arguments</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure ProcessStartInfo</param>
/// <returns>exit code</returns>
public static int Execute(string commandPath, string? arguments = null, string? workingDirectory = null)
public static int Execute(string commandPath, string? arguments = null, string? workingDirectory = null, Action<ProcessStartInfo>? configure = null)
{
return new ProcessStartInfo(commandPath, arguments ?? string.Empty)
var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
{
UseShellExecute = false,
CreateNoWindow = true,

WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
}.Execute();
};
configure?.Invoke(processStartInfo);
return processStartInfo.Execute();
}

/// <summary>
Expand All @@ -59,17 +63,19 @@ public static int Execute(string commandPath, string? arguments = null, string?
/// <param name="commandPath">executable command path</param>
/// <param name="arguments">command arguments</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <param name="cancellationToken">cancellationToken</param>
/// <returns>exit code</returns>
public static async Task<int> ExecuteAsync(string commandPath, string? arguments = null, string? workingDirectory = null, CancellationToken cancellationToken = default)
public static async Task<int> ExecuteAsync(string commandPath, string? arguments = null, string? workingDirectory = null, Action<ProcessStartInfo>? configure = null, CancellationToken cancellationToken = default)
{
return await new ProcessStartInfo(commandPath, arguments ?? string.Empty)
var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
{
UseShellExecute = false,
CreateNoWindow = true,

WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
}.ExecuteAsync(cancellationToken);
};
configure?.Invoke(processStartInfo);
return await processStartInfo.ExecuteAsync(cancellationToken);
}

/// <summary>
Expand All @@ -78,15 +84,17 @@ public static async Task<int> ExecuteAsync(string commandPath, string? arguments
/// <param name="commandPath">executable command path</param>
/// <param name="arguments">command arguments</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <returns>command execute result</returns>
public static CommandResult ExecuteAndCapture(string commandPath, string? arguments = null, string? workingDirectory = null)
public static CommandResult ExecuteAndCapture(string commandPath, string? arguments = null, string? workingDirectory = null, Action<ProcessStartInfo>? configure = null)
{
var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
{
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
};
configure?.Invoke(processStartInfo);
return ExecuteAndCapture(processStartInfo);
}

Expand All @@ -106,16 +114,18 @@ public static CommandResult ExecuteAndCapture(this ProcessStartInfo processStart
/// <param name="commandPath">executable command path</param>
/// <param name="arguments">command arguments</param>
/// <param name="workingDirectory">working directory</param>
/// <param name="configure">configure the ProcessStartInfo</param>
/// <param name="cancellationToken">cancellationToken</param>
/// <returns>command execute result</returns>
public static Task<CommandResult> ExecuteAndCaptureAsync(string commandPath, string? arguments = null, string? workingDirectory = null, CancellationToken cancellationToken = default)
public static Task<CommandResult> ExecuteAndCaptureAsync(string commandPath, string? arguments = null, string? workingDirectory = null, Action<ProcessStartInfo>? configure = null, CancellationToken cancellationToken = default)
{
var processStartInfo = new ProcessStartInfo(commandPath, arguments ?? string.Empty)
{
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = workingDirectory ?? Environment.CurrentDirectory
};
configure?.Invoke(processStartInfo);
return ExecuteAndCaptureAsync(processStartInfo, cancellationToken);
}

Expand Down

0 comments on commit 3d7ef08

Please sign in to comment.