diff --git a/src/Commands/ExecuteCustomAction.cs b/src/Commands/ExecuteCustomAction.cs deleted file mode 100644 index e59bc068..00000000 --- a/src/Commands/ExecuteCustomAction.cs +++ /dev/null @@ -1,86 +0,0 @@ -using System; -using System.Diagnostics; -using System.Text; - -using Avalonia.Threading; - -namespace SourceGit.Commands -{ - public static class ExecuteCustomAction - { - public static void Run(string repo, string file, string args) - { - var start = new ProcessStartInfo(); - start.FileName = file; - start.Arguments = args; - start.UseShellExecute = false; - start.CreateNoWindow = true; - start.WorkingDirectory = repo; - - try - { - Process.Start(start); - } - catch (Exception e) - { - Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, e.Message)); - } - } - - public static void RunAndWait(string repo, string file, string args, Models.ICommandLog log) - { - var start = new ProcessStartInfo(); - start.FileName = file; - start.Arguments = args; - start.UseShellExecute = false; - start.CreateNoWindow = true; - start.RedirectStandardOutput = true; - start.RedirectStandardError = true; - start.StandardOutputEncoding = Encoding.UTF8; - start.StandardErrorEncoding = Encoding.UTF8; - start.WorkingDirectory = repo; - - log?.AppendLine($"$ {file} {args}\n"); - - var proc = new Process() { StartInfo = start }; - var builder = new StringBuilder(); - - proc.OutputDataReceived += (_, e) => - { - if (e.Data != null) - log?.AppendLine(e.Data); - }; - - proc.ErrorDataReceived += (_, e) => - { - if (e.Data != null) - { - log?.AppendLine(e.Data); - builder.AppendLine(e.Data); - } - }; - - try - { - proc.Start(); - proc.BeginOutputReadLine(); - proc.BeginErrorReadLine(); - proc.WaitForExit(); - - var exitCode = proc.ExitCode; - if (exitCode != 0) - { - var errMsg = builder.ToString().Trim(); - if (!string.IsNullOrEmpty(errMsg)) - Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, errMsg)); - } - } - catch (Exception e) - { - Dispatcher.UIThread.Invoke(() => App.RaiseException(repo, e.Message)); - } - - proc.Close(); - } - } -} diff --git a/src/ViewModels/ExecuteCustomAction.cs b/src/ViewModels/ExecuteCustomAction.cs index b26e2f67..45f5da89 100644 --- a/src/ViewModels/ExecuteCustomAction.cs +++ b/src/ViewModels/ExecuteCustomAction.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; +using System.Text; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; @@ -136,10 +138,12 @@ namespace SourceGit.ViewModels return Task.Run(() => { + log.AppendLine($"$ {CustomAction.Executable} {cmdline}\n"); + if (CustomAction.WaitForExit) - Commands.ExecuteCustomAction.RunAndWait(_repo.FullPath, CustomAction.Executable, cmdline, log); + RunAndWait(cmdline, log); else - Commands.ExecuteCustomAction.Run(_repo.FullPath, CustomAction.Executable, cmdline); + Run(cmdline); log.Complete(); CallUIThread(() => _repo.SetWatcherEnabled(true)); @@ -171,6 +175,79 @@ namespace SourceGit.ViewModels return OperatingSystem.IsWindows() ? _repo.FullPath.Replace("/", "\\") : _repo.FullPath; } + private void Run(string args) + { + var start = new ProcessStartInfo(); + start.FileName = CustomAction.Executable; + start.Arguments = args; + start.UseShellExecute = false; + start.CreateNoWindow = true; + start.WorkingDirectory = _repo.FullPath; + + try + { + Process.Start(start); + } + catch (Exception e) + { + CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message)); + } + } + + private void RunAndWait(string args, Models.ICommandLog log) + { + var start = new ProcessStartInfo(); + start.FileName = CustomAction.Executable; + start.Arguments = args; + start.UseShellExecute = false; + start.CreateNoWindow = true; + start.RedirectStandardOutput = true; + start.RedirectStandardError = true; + start.StandardOutputEncoding = Encoding.UTF8; + start.StandardErrorEncoding = Encoding.UTF8; + start.WorkingDirectory = _repo.FullPath; + + var proc = new Process() { StartInfo = start }; + var builder = new StringBuilder(); + + proc.OutputDataReceived += (_, e) => + { + if (e.Data != null) + log?.AppendLine(e.Data); + }; + + proc.ErrorDataReceived += (_, e) => + { + if (e.Data != null) + { + log?.AppendLine(e.Data); + builder.AppendLine(e.Data); + } + }; + + try + { + proc.Start(); + proc.BeginOutputReadLine(); + proc.BeginErrorReadLine(); + proc.WaitForExit(); + + var exitCode = proc.ExitCode; + if (exitCode != 0) + { + var errMsg = builder.ToString().Trim(); + if (!string.IsNullOrEmpty(errMsg)) + CallUIThread(() => App.RaiseException(_repo.FullPath, errMsg)); + } + } + catch (Exception e) + { + CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message)); + } + + proc.Close(); + } + private readonly Repository _repo = null; private readonly string _commandline = string.Empty; }