Skip to content

Commit 295718e

Browse files
committed
code_style: execute custom action has nothing to do with git command
Signed-off-by: leo <[email protected]>
1 parent 83f1f2f commit 295718e

File tree

2 files changed

+79
-88
lines changed

2 files changed

+79
-88
lines changed

src/Commands/ExecuteCustomAction.cs

Lines changed: 0 additions & 86 deletions
This file was deleted.

src/ViewModels/ExecuteCustomAction.cs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Text;
35
using System.Threading.Tasks;
46

57
using CommunityToolkit.Mvvm.ComponentModel;
@@ -136,10 +138,12 @@ public override Task<bool> Sure()
136138

137139
return Task.Run(() =>
138140
{
141+
log.AppendLine($"$ {CustomAction.Executable} {cmdline}\n");
142+
139143
if (CustomAction.WaitForExit)
140-
Commands.ExecuteCustomAction.RunAndWait(_repo.FullPath, CustomAction.Executable, cmdline, log);
144+
RunAndWait(cmdline, log);
141145
else
142-
Commands.ExecuteCustomAction.Run(_repo.FullPath, CustomAction.Executable, cmdline);
146+
Run(cmdline);
143147

144148
log.Complete();
145149
CallUIThread(() => _repo.SetWatcherEnabled(true));
@@ -171,6 +175,79 @@ private string GetWorkdir()
171175
return OperatingSystem.IsWindows() ? _repo.FullPath.Replace("/", "\\") : _repo.FullPath;
172176
}
173177

178+
private void Run(string args)
179+
{
180+
var start = new ProcessStartInfo();
181+
start.FileName = CustomAction.Executable;
182+
start.Arguments = args;
183+
start.UseShellExecute = false;
184+
start.CreateNoWindow = true;
185+
start.WorkingDirectory = _repo.FullPath;
186+
187+
try
188+
{
189+
Process.Start(start);
190+
}
191+
catch (Exception e)
192+
{
193+
CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message));
194+
}
195+
}
196+
197+
private void RunAndWait(string args, Models.ICommandLog log)
198+
{
199+
var start = new ProcessStartInfo();
200+
start.FileName = CustomAction.Executable;
201+
start.Arguments = args;
202+
start.UseShellExecute = false;
203+
start.CreateNoWindow = true;
204+
start.RedirectStandardOutput = true;
205+
start.RedirectStandardError = true;
206+
start.StandardOutputEncoding = Encoding.UTF8;
207+
start.StandardErrorEncoding = Encoding.UTF8;
208+
start.WorkingDirectory = _repo.FullPath;
209+
210+
var proc = new Process() { StartInfo = start };
211+
var builder = new StringBuilder();
212+
213+
proc.OutputDataReceived += (_, e) =>
214+
{
215+
if (e.Data != null)
216+
log?.AppendLine(e.Data);
217+
};
218+
219+
proc.ErrorDataReceived += (_, e) =>
220+
{
221+
if (e.Data != null)
222+
{
223+
log?.AppendLine(e.Data);
224+
builder.AppendLine(e.Data);
225+
}
226+
};
227+
228+
try
229+
{
230+
proc.Start();
231+
proc.BeginOutputReadLine();
232+
proc.BeginErrorReadLine();
233+
proc.WaitForExit();
234+
235+
var exitCode = proc.ExitCode;
236+
if (exitCode != 0)
237+
{
238+
var errMsg = builder.ToString().Trim();
239+
if (!string.IsNullOrEmpty(errMsg))
240+
CallUIThread(() => App.RaiseException(_repo.FullPath, errMsg));
241+
}
242+
}
243+
catch (Exception e)
244+
{
245+
CallUIThread(() => App.RaiseException(_repo.FullPath, e.Message));
246+
}
247+
248+
proc.Close();
249+
}
250+
174251
private readonly Repository _repo = null;
175252
private readonly string _commandline = string.Empty;
176253
}

0 commit comments

Comments
 (0)