Refactor processing running and merge apr2025 generator into

BaseRandomizer
This commit is contained in:
2026-01-24 18:47:33 -06:00
parent e5fd52376e
commit 96cebd2289
12 changed files with 189 additions and 290 deletions

View File

@@ -0,0 +1,41 @@
namespace ALttPRandomizer.Service {
using System.Diagnostics;
using System.Linq;
using Microsoft.Extensions.Logging;
public class ProcessService {
public ProcessService(ILogger<ProcessService> logger) {
this.Logger = logger;
}
public ILogger<ProcessService> Logger { get; }
public Process StartProcess(string logPrefix, string workingDirectory, params string[] args) {
var start = new ProcessStartInfo(args[0], args.Skip(1)) {
WorkingDirectory = workingDirectory,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
this.Logger.LogInformation("{prefix} - executing command: {command}", logPrefix, string.Join(" ", args.Select(arg => arg.Contains(' ') ? $"\"{arg}\"" : arg)));
var process = Process.Start(start) ?? throw new GenerationFailedException("{0} - Process failed to start.", logPrefix);
process.EnableRaisingEvents = true;
process.OutputDataReceived += (_, args) => {
if (args.Data != null) {
Logger.LogInformation("{prefix} - STDOUT: {output}", logPrefix, args.Data);
}
};
process.ErrorDataReceived += (_, args) => {
if (args.Data != null) {
Logger.LogInformation("{prefix} STDERR: {output}", logPrefix, args.Data);
}
};
process.BeginOutputReadLine();
process.BeginErrorReadLine();
return process;
}
}
}