Clean up
This commit is contained in:
parent
7e249e12c4
commit
3a46e20d38
@ -1,46 +1,9 @@
|
|||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Text;
|
|
||||||
using MycroForge.CLI.Exceptions;
|
|
||||||
|
|
||||||
namespace MycroForge.CLI;
|
namespace MycroForge.CLI;
|
||||||
|
|
||||||
public static class Bash
|
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)
|
public static async Task ExecuteAsync(params string[] script)
|
||||||
{
|
{
|
||||||
var info = new ProcessStartInfo
|
var info = new ProcessStartInfo
|
||||||
@ -58,6 +21,17 @@ public static class Bash
|
|||||||
if (process is null)
|
if (process is null)
|
||||||
throw new NullReferenceException("Could not initialize bash process.");
|
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;
|
await using var input = process.StandardInput;
|
||||||
foreach (var line in script)
|
foreach (var line in script)
|
||||||
await input.WriteLineAsync(line);
|
await input.WriteLineAsync(line);
|
||||||
@ -65,16 +39,8 @@ public static class Bash
|
|||||||
await input.FlushAsync();
|
await input.FlushAsync();
|
||||||
input.Close();
|
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();
|
await process.WaitForExitAsync();
|
||||||
|
|
||||||
// if (process.ExitCode != 0)
|
|
||||||
// throw new BashException($"Process exited with status code {process.ExitCode}.");
|
|
||||||
|
|
||||||
if (process.ExitCode != 0)
|
if (process.ExitCode != 0)
|
||||||
Console.WriteLine($"Process exited with status code {process.ExitCode}.");
|
Console.WriteLine($"Process exited with status code {process.ExitCode}.");
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ public partial class MycroForge
|
|||||||
|
|
||||||
private async Task ExecuteAsync()
|
private async Task ExecuteAsync()
|
||||||
{
|
{
|
||||||
await Bash.RunAsync([
|
await Bash.ExecuteAsync([
|
||||||
"source .venv/bin/activate",
|
"source .venv/bin/activate",
|
||||||
"uvicorn main:app --reload"
|
"uvicorn main:app --reload"
|
||||||
]);
|
]);
|
||||||
|
@ -9,9 +9,7 @@ public partial class MycroForge : RootCommand
|
|||||||
|
|
||||||
public MycroForge(IEnumerable<ISubCommandOf<MycroForge>> commands) : base("The MycroForge CLI tool.")
|
public MycroForge(IEnumerable<ISubCommandOf<MycroForge>> commands) : base("The MycroForge CLI tool.")
|
||||||
{
|
{
|
||||||
commands
|
foreach (var subCommandOf in commands.Cast<Command>())
|
||||||
.Cast<Command>()
|
AddCommand(subCommandOf);
|
||||||
.ToList()
|
|
||||||
.ForEach(AddCommand);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -8,7 +8,12 @@ public static class ServiceCollectionExtensions
|
|||||||
{
|
{
|
||||||
public static IServiceCollection AddServices(this IServiceCollection services, string[] args)
|
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<ProjectContext>();
|
||||||
services.AddScoped<IFeature, Api>();
|
services.AddScoped<IFeature, Api>();
|
||||||
services.AddScoped<IFeature, Orm>();
|
services.AddScoped<IFeature, Orm>();
|
||||||
|
Loading…
Reference in New Issue
Block a user