From e3237bdabe6f33b8d9755e999d9aa7eb8d15a1b4 Mon Sep 17 00:00:00 2001 From: mdnapo Date: Sun, 2 Jun 2024 11:57:25 +0200 Subject: [PATCH] Added additional scripts and cleaned up --- .dockerignore | 34 -------- Dockerfile | 32 -------- MycroForge.CLI/MycroForge.CLI.csproj | 13 +-- MycroForge.CLI/scripts/build-executable.sh | 11 +++ MycroForge.CLI/scripts/build-executables.sh | 7 ++ MycroForge.CLI/scripts/env.py | 90 --------------------- MycroForge.CLI/scripts/install.sh | 15 ++++ MycroForge.CLI/scripts/user.py | 12 --- README.Docker.md | 46 ----------- README.md | 19 +++++ 10 files changed, 56 insertions(+), 223 deletions(-) delete mode 100644 .dockerignore delete mode 100644 Dockerfile create mode 100644 MycroForge.CLI/scripts/build-executable.sh create mode 100644 MycroForge.CLI/scripts/build-executables.sh delete mode 100644 MycroForge.CLI/scripts/env.py create mode 100644 MycroForge.CLI/scripts/install.sh delete mode 100644 MycroForge.CLI/scripts/user.py delete mode 100644 README.Docker.md create mode 100644 README.md diff --git a/.dockerignore b/.dockerignore deleted file mode 100644 index d2ac015..0000000 --- a/.dockerignore +++ /dev/null @@ -1,34 +0,0 @@ -# Include any files or directories that you don't want to be copied to your -# container here (e.g., local build artifacts, temporary files, etc.). -# -# For more help, visit the .dockerignore file reference guide at -# https://docs.docker.com/go/build-context-dockerignore/ - -**/.DS_Store -**/.classpath -**/.dockerignore -**/.env -**/.git -**/.gitignore -**/.project -**/.settings -**/.toolstarget -**/.vs -**/.vscode -**/*.*proj.user -**/*.dbmdl -**/*.jfm -**/bin -**/charts -**/docker-compose* -**/compose* -**/Dockerfile* -**/node_modules -**/npm-debug.log -**/obj -**/secrets.dev.yaml -**/values.dev.yaml -LICENSE -README.md - -**/venv diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 795e802..0000000 --- a/Dockerfile +++ /dev/null @@ -1,32 +0,0 @@ -FROM --platform=$BUILDPLATFORM mcr.microsoft.com/dotnet/sdk:8.0-alpine AS build -COPY . /source -WORKDIR /source/MycroForge.CLI -ARG TARGETARCH -# Leverage a cache mount to /root/.nuget/packages so that subsequent builds don't have to re-download packages. -# If TARGETARCH is "amd64", replace it with "x64" - "x64" is .NET's canonical name for this and "amd64" doesn't -# work in .NET 6.0. -RUN --mount=type=cache,id=nuget,target=/root/.nuget/packages \ - dotnet publish -a ${TARGETARCH/amd64/x64} --use-current-runtime --self-contained false -o /app - -FROM mcr.microsoft.com/dotnet/aspnet:8.0-bookworm-slim AS final -RUN apt update -y && \ - apt upgrade -y && \ - apt install -y git && \ - apt install -y bash && \ - apt install -y python3 && \ - apt install -y python3-pip && \ - apt install -y python3-venv - -# The Docker approach doesn't work for now, because the venv setup depends on absolute paths. -# This means that the would need to recreate the full path to the actual working directory in the Docker container, -# which should be pretty doable, but it's a concern for later. - -ENV PYTHONUNBUFFERED=1 -WORKDIR /app -COPY --from=build /app . - -WORKDIR /project -COPY MycroForge.CLI/scripts /scripts -USER root - -ENTRYPOINT ["dotnet", "/app/MycroForge.CLI.dll"] CMD ["-?"] diff --git a/MycroForge.CLI/MycroForge.CLI.csproj b/MycroForge.CLI/MycroForge.CLI.csproj index 4db7db8..2102a68 100644 --- a/MycroForge.CLI/MycroForge.CLI.csproj +++ b/MycroForge.CLI/MycroForge.CLI.csproj @@ -18,19 +18,14 @@ - - - PreserveNewest - - - PreserveNewest - - - bin\Debug\net8.0\MycroForge.Parsing.dll + + + + diff --git a/MycroForge.CLI/scripts/build-executable.sh b/MycroForge.CLI/scripts/build-executable.sh new file mode 100644 index 0000000..7b5198c --- /dev/null +++ b/MycroForge.CLI/scripts/build-executable.sh @@ -0,0 +1,11 @@ +#!/usr/bin/bash + +TARGET=$1 + +if [ -z "$TARGET" ]; then + echo "The target platform was not provided." + exit 1 +fi + +dotnet publish --self-contained -r "$TARGET" +zip -vr "releases/m4g-$TARGET.zip" "bin/Release/net8.0/$TARGET/" diff --git a/MycroForge.CLI/scripts/build-executables.sh b/MycroForge.CLI/scripts/build-executables.sh new file mode 100644 index 0000000..fa47569 --- /dev/null +++ b/MycroForge.CLI/scripts/build-executables.sh @@ -0,0 +1,7 @@ +#!/usr/bin/bash + +./scripts/build-executable.sh linux-x64 +./scripts/build-executable.sh linux-arm +./scripts/build-executable.sh linux-arm64 +./scripts/build-executable.sh osx-x64 +./scripts/build-executable.sh osx-arm64 diff --git a/MycroForge.CLI/scripts/env.py b/MycroForge.CLI/scripts/env.py deleted file mode 100644 index 446e932..0000000 --- a/MycroForge.CLI/scripts/env.py +++ /dev/null @@ -1,90 +0,0 @@ -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() diff --git a/MycroForge.CLI/scripts/install.sh b/MycroForge.CLI/scripts/install.sh new file mode 100644 index 0000000..e2c6663 --- /dev/null +++ b/MycroForge.CLI/scripts/install.sh @@ -0,0 +1,15 @@ +#!/usr/bin/bash + +TARGET=$1 + +if [ -z "$TARGET" ]; then + echo "The target platform was not provided." + exit 1 +fi + +dotnet publish --self-contained -r linux-x64 +zip -vr releases/m4g-linux.zip bin/Release/net8.0/linux-x64/ +#sudo rm -rf /usr/share/m4g +#sudo cp -r bin/Release/net8.0/linux-x64 /usr/share/m4g +#sudo unlink /usr/local/bin/m4g +#sudo ln -s /usr/share/m4g/MycroForge.CLI /usr/local/bin/m4g diff --git a/MycroForge.CLI/scripts/user.py b/MycroForge.CLI/scripts/user.py deleted file mode 100644 index ca87f76..0000000 --- a/MycroForge.CLI/scripts/user.py +++ /dev/null @@ -1,12 +0,0 @@ -from sqlalchemy import String -from sqlalchemy.orm import Mapped, mapped_column -from orm.entities.entity_base import EntityBase - -class User(EntityBase): - __tablename__ = "users" - id: Mapped[int] = mapped_column(primary_key=True) - firstname: Mapped[str] = mapped_column(String(255)) - lastname: Mapped[str] = mapped_column(String(255)) - - def __repr__(self) -> str: - return f"User(id={self.id!r}, firstname={self.firstname!r}, lastname={self.lastname!r})" \ No newline at end of file diff --git a/README.Docker.md b/README.Docker.md deleted file mode 100644 index 606397b..0000000 --- a/README.Docker.md +++ /dev/null @@ -1,46 +0,0 @@ -### Building and running your application - -When you're ready, start your application by running: -`docker compose up --build`. - -Your application will be available at http://localhost:8080. - -### Deploying your application to the cloud - -First, build your image, e.g.: `docker build -t myapp .`. -If your cloud uses a different CPU architecture than your development -machine (e.g., you are on a Mac M1 and your cloud provider is amd64), -you'll want to build the image for that platform, e.g.: -`docker build --platform=linux/amd64 -t myapp .`. - -Then, push it to your registry, e.g. `docker push myregistry.com/myapp`. - -Consult Docker's [getting started](https://docs.docker.com/go/get-started-sharing/) -docs for more detail on building and pushing. - -### References -* [Docker's .NET guide](https://docs.docker.com/language/dotnet/) -* The [dotnet-docker](https://github.com/dotnet/dotnet-docker/tree/main/samples) - repository has many relevant samples and docs. - -### Dependencies - -bash (/usr/bin/bash) - -Python 3.10.2 (/usr/bin/python3) - - python3-pip - - python3-venv - -pkg-config (sudo apt update && sudo apt install pkg-config) - -### Notes - -Try Running NuGet restore when ANTLR doesn't generate Lexer or Parser - -### TODO -- Make entrypoint, i.e. main.py, customizable or fixed? -- Figure out why BashException cannot be caught, can it be due to the differences in scoping? -Because the `Bash` class is static and the services calling `Bash.ExecuteAsync` are in the container. -Maybe this in combination with the async nature of the whole thing? -- Make it so that `Bash.ExecuteAsync` can run "directly" in the calling terminal also. -- This will improve the usability of the "m4g run" command. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..691076f --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +### Dependencies + +bash (/usr/bin/bash) + +Python 3.10.2 (/usr/bin/python3) +- python3-pip +- python3-venv + +#### Note +The MycroForge CLI assumes a linux compatible environment, so on Windows you'll have to use WSL. +`Ubuntu-22.04` is the recommended WSL version to use. + +### TODO +- Figure out why BashException cannot be caught, can it be due to the differences in scoping? + Because the `Bash` class is static and the services calling `Bash.ExecuteAsync` are in the container. + Maybe this in combination with the async nature of the whole thing? + +### Install +