Improved Bash class and removed Console.WriteLine statements

This commit is contained in:
Donné Napo 2024-04-22 08:57:59 +02:00
parent 0a4c5ebb8d
commit 8fed922cfe
4 changed files with 126 additions and 92 deletions

View File

@ -6,6 +6,41 @@ namespace MicroForge.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

View File

@ -26,7 +26,6 @@ public class OrmEnvInitializer : PythonSourceModifier
public override object? VisitAssignment(PythonParser.AssignmentContext context) public override object? VisitAssignment(PythonParser.AssignmentContext context)
{ {
var text = GetOriginalText(context); var text = GetOriginalText(context);
Console.WriteLine(text);
if (text == "target_metadata = None") if (text == "target_metadata = None")
{ {

View File

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

View File

@ -1,4 +1,4 @@
import asyncio import asyncio
from logging.config import fileConfig from logging.config import fileConfig
from sqlalchemy import pool from sqlalchemy import pool