This commit is contained in:
mdnapo 2024-04-22 20:25:20 +02:00
parent 7e249e12c4
commit 3a46e20d38
4 changed files with 20 additions and 51 deletions

View File

@ -1,46 +1,9 @@
using System.Diagnostics;
using System.Text;
using MycroForge.CLI.Exceptions;
namespace MycroForge.CLI;
public static class Bash
{
public static async Task RunAsync(params string[] script)
{
var info = new ProcessStartInfo
{
FileName = "bash",
UseShellExecute = false,
CreateNoWindow = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
};
using var process = Process.Start(info);
if (process is null)
throw new NullReferenceException("Could not initialize bash process.");
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.BeginOutputReadLine();
process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.BeginErrorReadLine();
await using var input = process.StandardInput;
foreach (var line in script)
await input.WriteLineAsync(line);
await input.FlushAsync();
input.Close();
await process.WaitForExitAsync();
if (process.ExitCode != 0)
Console.WriteLine($"Process exited with status code {process.ExitCode}.");
}
public static async Task ExecuteAsync(params string[] script)
{
var info = new ProcessStartInfo
@ -58,6 +21,17 @@ public static class Bash
if (process is null)
throw new NullReferenceException("Could not initialize bash process.");
process.OutputDataReceived += (sender, args) => { Console.WriteLine(args.Data); };
process.BeginOutputReadLine();
process.ErrorDataReceived += (sender, args) =>
{
// Only print error data when it's not empty to prevent noise in the shell
if (!string.IsNullOrEmpty(args.Data))
Console.WriteLine(args.Data);
};
process.BeginErrorReadLine();
await using var input = process.StandardInput;
foreach (var line in script)
await input.WriteLineAsync(line);
@ -65,16 +39,8 @@ public static class Bash
await input.FlushAsync();
input.Close();
var sb = new StringBuilder();
sb.Append(await process.StandardOutput.ReadToEndAsync());
sb.Append(await process.StandardError.ReadToEndAsync());
Console.WriteLine(sb.ToString());
await process.WaitForExitAsync();
// if (process.ExitCode != 0)
// throw new BashException($"Process exited with status code {process.ExitCode}.");
if (process.ExitCode != 0)
Console.WriteLine($"Process exited with status code {process.ExitCode}.");
}

View File

@ -24,7 +24,7 @@ public partial class MycroForge
private async Task ExecuteAsync()
{
await Bash.RunAsync([
await Bash.ExecuteAsync([
"source .venv/bin/activate",
"uvicorn main:app --reload"
]);

View File

@ -9,9 +9,7 @@ public partial class MycroForge : RootCommand
public MycroForge(IEnumerable<ISubCommandOf<MycroForge>> commands) : base("The MycroForge CLI tool.")
{
commands
.Cast<Command>()
.ToList()
.ForEach(AddCommand);
foreach (var subCommandOf in commands.Cast<Command>())
AddCommand(subCommandOf);
}
}

View File

@ -8,7 +8,12 @@ public static class ServiceCollectionExtensions
{
public static IServiceCollection AddServices(this IServiceCollection services, string[] args)
{
services.AddScoped<ArgsContext>(_ => new ArgsContext { Args = args });
services.AddScoped<ArgsContext>(_ =>
new ArgsContext
{
// Make sure to display the help text when no args are passed
Args = args.Length == 0 ? ["--help"] : args
});
services.AddScoped<ProjectContext>();
services.AddScoped<IFeature, Api>();
services.AddScoped<IFeature, Orm>();