diff --git a/MicroForge.CLI/Bash.cs b/MicroForge.CLI/Bash.cs index b188f1b..cccb155 100644 --- a/MicroForge.CLI/Bash.cs +++ b/MicroForge.CLI/Bash.cs @@ -6,6 +6,41 @@ namespace MicroForge.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 diff --git a/MicroForge.CLI/CodeGen/OrmEnvInitializer.cs b/MicroForge.CLI/CodeGen/OrmEnvInitializer.cs index 3ee9cd8..021b75b 100644 --- a/MicroForge.CLI/CodeGen/OrmEnvInitializer.cs +++ b/MicroForge.CLI/CodeGen/OrmEnvInitializer.cs @@ -26,7 +26,6 @@ public class OrmEnvInitializer : PythonSourceModifier public override object? VisitAssignment(PythonParser.AssignmentContext context) { var text = GetOriginalText(context); - Console.WriteLine(text); if (text == "target_metadata = None") { diff --git a/MicroForge.CLI/Commands/MicroForge.Add.cs b/MicroForge.CLI/Commands/MicroForge.Add.cs index 7403a68..d9d1d08 100644 --- a/MicroForge.CLI/Commands/MicroForge.Add.cs +++ b/MicroForge.CLI/Commands/MicroForge.Add.cs @@ -24,7 +24,7 @@ public partial class MicroForge private async Task ExecuteAsync() { - await Bash.ExecuteAsync([ + await Bash.RunAsync([ "source .venv/bin/activate", "uvicorn main:app --reload" ]); diff --git a/MicroForge.CLI/scripts/env.py b/MicroForge.CLI/scripts/env.py index 5acaf4f..446e932 100644 --- a/MicroForge.CLI/scripts/env.py +++ b/MicroForge.CLI/scripts/env.py @@ -1,90 +1,90 @@ -import asyncio -from logging.config import fileConfig - -from sqlalchemy import pool -from sqlalchemy.engine import Connection -from sqlalchemy.ext.asyncio import async_engine_from_config - -from alembic import context - - -# this is the Alembic Config object, which provides -# access to the values within the .ini file in use. -config = context.config - -# Interpret the config file for Python logging. -# This line sets up loggers basically. -if config.config_file_name is not None: - fileConfig(config.config_file_name) - -# add your model's MetaData object here -# for 'autogenerate' support -# from myapp import mymodel -# target_metadata = mymodel.Base.metadata -target_metadata = None - -# other values from the config, defined by the needs of env.py, -# can be acquired: -# my_important_option = config.get_main_option("my_important_option") -# ... etc. - - -def run_migrations_offline() -> None: - """Run migrations in 'offline' mode. - - This configures the context with just a URL - and not an Engine, though an Engine is acceptable - here as well. By skipping the Engine creation - we don't even need a DBAPI to be available. - - Calls to context.execute() here emit the given string to the - script output. - - """ - url = config.get_main_option("sqlalchemy.url") - context.configure( - url=url, - target_metadata=target_metadata, - literal_binds=True, - dialect_opts={"paramstyle": "named"}, - ) - - with context.begin_transaction(): - context.run_migrations() - - -def do_run_migrations(connection: Connection) -> None: - context.configure(connection=connection, target_metadata=target_metadata) - - with context.begin_transaction(): - context.run_migrations() - - -async def run_async_migrations() -> None: - """In this scenario we need to create an Engine - and associate a connection with the context. - - """ - - connectable = async_engine_from_config( - config.get_section(config.config_ini_section, {}), - prefix="sqlalchemy.", - poolclass=pool.NullPool, - ) - - async with connectable.connect() as connection: - await connection.run_sync(do_run_migrations) - - await connectable.dispose() - - -def run_migrations_online() -> None: - """Run migrations in 'online' mode.""" - - asyncio.run(run_async_migrations()) - - -if context.is_offline_mode(): - run_migrations_offline() -else: - run_migrations_online() +import asyncio +from logging.config import fileConfig + +from sqlalchemy import pool +from sqlalchemy.engine import Connection +from sqlalchemy.ext.asyncio import async_engine_from_config + +from alembic import context + + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +if config.config_file_name is not None: + fileConfig(config.config_file_name) + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +target_metadata = None + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline() -> None: + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, + target_metadata=target_metadata, + literal_binds=True, + dialect_opts={"paramstyle": "named"}, + ) + + with context.begin_transaction(): + context.run_migrations() + + +def do_run_migrations(connection: Connection) -> None: + context.configure(connection=connection, target_metadata=target_metadata) + + with context.begin_transaction(): + context.run_migrations() + + +async def run_async_migrations() -> None: + """In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + connectable = async_engine_from_config( + config.get_section(config.config_ini_section, {}), + prefix="sqlalchemy.", + poolclass=pool.NullPool, + ) + + async with connectable.connect() as connection: + await connection.run_sync(do_run_migrations) + + await connectable.dispose() + + +def run_migrations_online() -> None: + """Run migrations in 'online' mode.""" + + asyncio.run(run_async_migrations()) + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online()