|
1 | 1 | using System;
|
2 | 2 | using System.Collections.Generic;
|
| 3 | +using System.Diagnostics; |
| 4 | +using System.Text; |
3 | 5 | using System.Threading.Tasks;
|
4 | 6 |
|
5 | 7 | using CommunityToolkit.Mvvm.ComponentModel;
|
@@ -136,10 +138,12 @@ public override Task<bool> Sure()
|
136 | 138 |
|
137 | 139 | return Task.Run(() =>
|
138 | 140 | {
|
| 141 | + log.AppendLine($"$ {CustomAction.Executable} {cmdline}\n"); |
| 142 | + |
139 | 143 | if (CustomAction.WaitForExit)
|
140 |
| - Commands.ExecuteCustomAction.RunAndWait(_repo.FullPath, CustomAction.Executable, cmdline, log); |
| 144 | + RunAndWait(cmdline, log); |
141 | 145 | else
|
142 |
| - Commands.ExecuteCustomAction.Run(_repo.FullPath, CustomAction.Executable, cmdline); |
| 146 | + Run(cmdline); |
143 | 147 |
|
144 | 148 | log.Complete();
|
145 | 149 | CallUIThread(() => _repo.SetWatcherEnabled(true));
|
@@ -171,6 +175,79 @@ private string GetWorkdir()
|
171 | 175 | return OperatingSystem.IsWindows() ? _repo.FullPath.Replace("/", "\\") : _repo.FullPath;
|
172 | 176 | }
|
173 | 177 |
|
| 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 | + |
174 | 251 | private readonly Repository _repo = null;
|
175 | 252 | private readonly string _commandline = string.Empty;
|
176 | 253 | }
|
|
0 commit comments