diff --git a/src/Stack/Git/GitClient.cs b/src/Stack/Git/GitClient.cs index 28e9277f..19a25e55 100644 --- a/src/Stack/Git/GitClient.cs +++ b/src/Stack/Git/GitClient.cs @@ -1,6 +1,4 @@ using System.Diagnostics; -using System.Text; -using Octopus.Shellfish; using Spectre.Console; using Stack.Infrastructure; @@ -334,50 +332,15 @@ private string ExecuteGitCommandAndReturnOutput( bool captureStandardError = false, Func? exceptionHandler = null) { - if (settings.Verbose) - logger.Debug($"git {command}"); - - var infoBuilder = new StringBuilder(); - var errorBuilder = new StringBuilder(); - - var result = ShellExecutor.ExecuteCommand( + return ProcessHelpers.ExecuteProcessAndReturnOutput( "git", command, - settings.WorkingDirectory ?? ".", - (_) => { }, - (info) => infoBuilder.AppendLine(info), - (error) => errorBuilder.AppendLine(error)); - - if (result != 0) - { - logger.Error(Markup.Escape(errorBuilder.ToString())); - if (exceptionHandler != null) - { - var exception = exceptionHandler(result); - if (exception != null) - { - throw exception; - } - } - else - { - throw new Exception($"Failed to execute git command: {command}. Exit code: {result}. Error: {errorBuilder}."); - } - } - - if (settings.Verbose && infoBuilder.Length > 0) - { - logger.Debug(Markup.Escape(infoBuilder.ToString())); - } - - var output = infoBuilder.ToString(); - - if (captureStandardError) - { - output += $"{Environment.NewLine}{errorBuilder}"; - } - - return output; + settings.WorkingDirectory, + logger, + settings.Verbose, + captureStandardError, + exceptionHandler + ); } private void ExecuteGitCommand( diff --git a/src/Stack/Git/GitHubClient.cs b/src/Stack/Git/GitHubClient.cs index d9005d8e..7e635738 100644 --- a/src/Stack/Git/GitHubClient.cs +++ b/src/Stack/Git/GitHubClient.cs @@ -1,7 +1,6 @@ using System.Text; using System.Text.Json; using System.Text.Json.Serialization; -using Octopus.Shellfish; using Spectre.Console; using Stack.Infrastructure; @@ -114,32 +113,15 @@ public void OpenPullRequest(GitHubPullRequest pullRequest) private string ExecuteGitHubCommandAndReturnOutput(string command) { - if (settings.Verbose) - logger.Debug($"gh {command}"); - - var infoBuilder = new StringBuilder(); - var errorBuilder = new StringBuilder(); - - var result = ShellExecutor.ExecuteCommand( + return ProcessHelpers.ExecuteProcessAndReturnOutput( "gh", command, - settings.WorkingDirectory ?? ".", - (_) => { }, - (info) => infoBuilder.AppendLine(info), - (error) => errorBuilder.AppendLine(error)); - - if (result != 0) - { - logger.Error(Markup.Escape(errorBuilder.ToString())); - throw new Exception("Failed to execute gh command."); - } - - if (settings.Verbose && infoBuilder.Length > 0) - { - logger.Debug(Markup.Escape(infoBuilder.ToString())); - } - - return infoBuilder.ToString(); + settings.WorkingDirectory, + logger, + settings.Verbose, + false, + null + ); } private void ExecuteGitHubCommand(string command) @@ -147,34 +129,7 @@ private void ExecuteGitHubCommand(string command) if (settings.Verbose) logger.Debug($"gh {command}"); - ExecuteGitHubCommandInternal(command); - } - - private void ExecuteGitHubCommandInternal(string command) - { - var infoBuilder = new StringBuilder(); - var errorBuilder = new StringBuilder(); - - var result = ShellExecutor.ExecuteCommand( - "gh", - command, - settings.WorkingDirectory ?? ".", - (_) => { }, - (info) => infoBuilder.AppendLine(info), - (error) => errorBuilder.AppendLine(error)); - - if (result != 0) - { - logger.Error($"{errorBuilder}"); - throw new Exception("Failed to execute gh command."); - } - else - { - if (infoBuilder.Length > 0) - { - logger.Debug(Markup.Escape(infoBuilder.ToString())); - } - } + ExecuteGitHubCommandAndReturnOutput(command); } private string Sanitize(string value) diff --git a/src/Stack/Git/ProcessHelpers.cs b/src/Stack/Git/ProcessHelpers.cs new file mode 100644 index 00000000..686234b5 --- /dev/null +++ b/src/Stack/Git/ProcessHelpers.cs @@ -0,0 +1,86 @@ +using System.Diagnostics; +using System.Text; +using Spectre.Console; +using Stack.Infrastructure; + +namespace Stack.Git; + +public static class ProcessHelpers +{ + public static string ExecuteProcessAndReturnOutput( + string fileName, + string command, + string? workingDirectory, + ILogger logger, + bool verbose = false, + bool captureStandardError = false, + Func? exceptionHandler = null) + { + if (verbose) + logger.Debug($"{fileName} {command}"); + + var infoBuilder = new StringBuilder(); + var errorBuilder = new StringBuilder(); + + var psi = new ProcessStartInfo + { + FileName = fileName, + Arguments = command, + WorkingDirectory = workingDirectory ?? ".", + RedirectStandardOutput = true, + RedirectStandardError = true, + UseShellExecute = false, + CreateNoWindow = true + }; + + using var process = Process.Start(psi); + if (process is null) return string.Empty; + + process.OutputDataReceived += (_, e) => + { + if (e.Data is null) return; + infoBuilder.AppendLine(e.Data); + }; + process.ErrorDataReceived += (_, e) => + { + if (e.Data is null) return; + errorBuilder.AppendLine(e.Data); + }; + process.BeginErrorReadLine(); + process.BeginOutputReadLine(); + + process.WaitForExit(); + int result = process.ExitCode; + + if (result != 0) + { + logger.Error(Markup.Escape(errorBuilder.ToString())); + if (exceptionHandler != null) + { + var exception = exceptionHandler(result); + if (exception != null) + { + throw exception; + } + } + else + { + throw new Exception($"Failed to execute command: {fileName} {command}. Exit code: {result}. Error: {errorBuilder}."); + } + } + + if (verbose && infoBuilder.Length > 0) + { + logger.Debug(Markup.Escape(infoBuilder.ToString())); + } + + var output = infoBuilder.ToString(); + + if (captureStandardError) + { + output += $"{Environment.NewLine}{errorBuilder}"; + } + + return output; + } +} \ No newline at end of file diff --git a/src/Stack/Stack.csproj b/src/Stack/Stack.csproj index ed8d537a..5e262c30 100644 --- a/src/Stack/Stack.csproj +++ b/src/Stack/Stack.csproj @@ -15,7 +15,6 @@ -