code_style: execute custom action has nothing to do with git command
Some checks are pending
Continuous Integration / Build (push) Waiting to run
Continuous Integration / Prepare version string (push) Waiting to run
Continuous Integration / Package (push) Blocked by required conditions
Localization Check / localization-check (push) Waiting to run

Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
leo 2025-06-25 19:22:57 +08:00
parent 83f1f2f6cd
commit 295718effe
No known key found for this signature in database
2 changed files with 79 additions and 88 deletions

View file

@ -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();
}
}
}

View file

@ -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;
}