|
| 1 | +using System.Diagnostics; |
| 2 | +using System.Text.RegularExpressions; |
| 3 | + |
| 4 | +namespace WheelWizard.DolphinManagent.Linux; |
| 5 | + |
| 6 | +public interface ILinuxProcessService |
| 7 | +{ |
| 8 | + OperationResult<int> Run(string fileName, string arguments, out string stdOut, out string stdErr); |
| 9 | + OperationResult<int> Run(string fileName, string arguments); |
| 10 | + Task<OperationResult<int>> RunWithProgressAsync(string fileName, string arguments, IProgress<int>? progress = null); |
| 11 | + Task<OperationResult> LaunchAndStopAsync(string fileName, string arguments, TimeSpan duration); |
| 12 | +} |
| 13 | + |
| 14 | +public sealed class LinuxProcessService : ILinuxProcessService |
| 15 | +{ |
| 16 | + public OperationResult<int> Run(string fileName, string arguments, out string stdOut, out string stdErr) |
| 17 | + { |
| 18 | + var localStdOut = ""; |
| 19 | + var localStdErr = ""; |
| 20 | + var result = TryCatch( |
| 21 | + () => |
| 22 | + { |
| 23 | + var processInfo = new ProcessStartInfo |
| 24 | + { |
| 25 | + FileName = fileName, |
| 26 | + Arguments = arguments, |
| 27 | + RedirectStandardOutput = true, |
| 28 | + RedirectStandardError = true, |
| 29 | + UseShellExecute = false, |
| 30 | + CreateNoWindow = true, |
| 31 | + }; |
| 32 | + |
| 33 | + using var process = Process.Start(processInfo); |
| 34 | + if (process == null) |
| 35 | + return -1; |
| 36 | + |
| 37 | + localStdOut = process.StandardOutput.ReadToEnd(); |
| 38 | + localStdErr = process.StandardError.ReadToEnd(); |
| 39 | + process.WaitForExit(); |
| 40 | + return process.ExitCode; |
| 41 | + }, |
| 42 | + $"Failed to run process: {fileName} {arguments}" |
| 43 | + ); |
| 44 | + |
| 45 | + stdOut = localStdOut; |
| 46 | + stdErr = localStdErr; |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + public OperationResult<int> Run(string fileName, string arguments) |
| 51 | + { |
| 52 | + return Run(fileName, arguments, out _, out _); |
| 53 | + } |
| 54 | + |
| 55 | + public async Task<OperationResult<int>> RunWithProgressAsync(string fileName, string arguments, IProgress<int>? progress = null) |
| 56 | + { |
| 57 | + return await TryCatch( |
| 58 | + async () => |
| 59 | + { |
| 60 | + var processInfo = new ProcessStartInfo |
| 61 | + { |
| 62 | + FileName = fileName, |
| 63 | + Arguments = arguments, |
| 64 | + RedirectStandardOutput = true, |
| 65 | + RedirectStandardError = true, |
| 66 | + UseShellExecute = false, |
| 67 | + CreateNoWindow = true, |
| 68 | + }; |
| 69 | + |
| 70 | + using var process = Process.Start(processInfo); |
| 71 | + if (process == null) |
| 72 | + return -1; |
| 73 | + |
| 74 | + process.OutputDataReceived += (_, eventArgs) => ReportProgress(eventArgs.Data, progress); |
| 75 | + process.ErrorDataReceived += (_, eventArgs) => ReportProgress(eventArgs.Data, progress); |
| 76 | + |
| 77 | + process.BeginOutputReadLine(); |
| 78 | + process.BeginErrorReadLine(); |
| 79 | + await process.WaitForExitAsync(); |
| 80 | + return process.ExitCode; |
| 81 | + }, |
| 82 | + $"Failed to run process: {fileName} {arguments}" |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + public async Task<OperationResult> LaunchAndStopAsync(string fileName, string arguments, TimeSpan duration) |
| 87 | + { |
| 88 | + return await TryCatch( |
| 89 | + async () => |
| 90 | + { |
| 91 | + using var process = new Process |
| 92 | + { |
| 93 | + StartInfo = new() |
| 94 | + { |
| 95 | + FileName = fileName, |
| 96 | + Arguments = arguments, |
| 97 | + RedirectStandardOutput = true, |
| 98 | + RedirectStandardError = true, |
| 99 | + UseShellExecute = false, |
| 100 | + CreateNoWindow = true, |
| 101 | + }, |
| 102 | + }; |
| 103 | + |
| 104 | + process.Start(); |
| 105 | + await Task.Delay(duration); |
| 106 | + |
| 107 | + if (!process.HasExited) |
| 108 | + process.Kill(); |
| 109 | + }, |
| 110 | + $"Failed to run process: {fileName} {arguments}" |
| 111 | + ); |
| 112 | + } |
| 113 | + |
| 114 | + private static void ReportProgress(string? output, IProgress<int>? progress) |
| 115 | + { |
| 116 | + if (string.IsNullOrWhiteSpace(output)) |
| 117 | + return; |
| 118 | + |
| 119 | + var match = Regex.Match(output, @"(\d+)%"); |
| 120 | + if (match.Success && int.TryParse(match.Groups[1].Value, out var percent)) |
| 121 | + progress?.Report(percent); |
| 122 | + } |
| 123 | +} |
0 commit comments