91 lines
2.5 KiB
C#
91 lines
2.5 KiB
C#
using System.Diagnostics;
|
|
using Core.Events;
|
|
using Core.Exceptions;
|
|
|
|
namespace Generator.Infrastructure;
|
|
|
|
public class CommandExecutor
|
|
{
|
|
|
|
private readonly Queue<string> _commands;
|
|
private string _invite;
|
|
private DisplayEmitter _emitter;
|
|
|
|
public CommandExecutor(string invite, DisplayEmitter emitter)
|
|
{
|
|
_commands = new Queue<string>();
|
|
_invite = invite;
|
|
_emitter = emitter;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Runs a single asynchronous command
|
|
/// </summary>
|
|
/// <param name="command">Command to run</param>
|
|
/// <exception cref="CommandExecutionException">When the command hasn't been terminated successfully</exception>
|
|
public async Task RunAsync(string command)
|
|
{
|
|
await Task.Run(() =>
|
|
{
|
|
using var process = new Process();
|
|
process.StartInfo = new ProcessStartInfo
|
|
{
|
|
FileName = _invite,
|
|
Arguments = $"{command}",
|
|
RedirectStandardOutput = true,
|
|
RedirectStandardError = true,
|
|
WindowStyle = ProcessWindowStyle.Normal
|
|
};
|
|
process.Start();
|
|
ReadOutput(process);
|
|
ReadError(process);
|
|
process.WaitForExit();
|
|
|
|
if(process.ExitCode == 1)
|
|
throw new CommandExecutionException($"The following task has failed : \n '{command}'");
|
|
});
|
|
}
|
|
|
|
private void ReadError(Process process)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
using var output = process.StandardError;
|
|
while (!output.EndOfStream)
|
|
{
|
|
var line = await output.ReadLineAsync();
|
|
_emitter.Warn(this, line.ToString());
|
|
}
|
|
});
|
|
}
|
|
|
|
private void ReadOutput(Process process)
|
|
{
|
|
Task.Run(async () =>
|
|
{
|
|
using var output = process.StandardOutput;
|
|
while (!output.EndOfStream)
|
|
{
|
|
var line = await output.ReadLineAsync();
|
|
_emitter.Say(this, line);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// <summary>
|
|
/// Registers a command to execute later
|
|
/// </summary>
|
|
/// <param name="command"></param>
|
|
public void Register(string command) => _commands.Enqueue(command);
|
|
|
|
/// <summary>
|
|
/// Runs all previously registered command
|
|
/// </summary>
|
|
public async Task RunRegistered()
|
|
{
|
|
while (_commands.TryDequeue(out var c))
|
|
{
|
|
await RunAsync(c);
|
|
}
|
|
}
|
|
} |