From 060ae03a01b54ecf3816813fa85ba4fe0ebc0729 Mon Sep 17 00:00:00 2001 From: Val Date: Wed, 25 Sep 2024 10:44:08 +0200 Subject: [PATCH] add templater from gojo --- mojoproject.toml | 25 +++++++++++++--- recipes/recipe.tmpl | 28 ++++++++++++++++++ recipes/recipe.yaml | 30 ------------------- scripts/build.sh | 11 +++++++ scripts/publish.sh | 4 ++- scripts/templater.py | 70 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 133 insertions(+), 35 deletions(-) create mode 100644 recipes/recipe.tmpl delete mode 100644 recipes/recipe.yaml create mode 100644 scripts/build.sh create mode 100644 scripts/templater.py diff --git a/mojoproject.toml b/mojoproject.toml index d082c859..83d05a29 100644 --- a/mojoproject.toml +++ b/mojoproject.toml @@ -1,19 +1,36 @@ [project] authors = ["saviorand"] -channels = ["conda-forge", "https://conda.modular.com/max-nightly", "https://repo.prefix.dev/mojo-community-nightly", "https://repo.prefix.dev/mojo-community"] +channels = ["conda-forge", "https://conda.modular.com/max-nightly", "https://repo.prefix.dev/mojo-community"] description = "Simple and fast HTTP framework for Mojo!" name = "lightbug_http" platforms = ["osx-arm64", "linux-64"] version = "0.1.4" +license = "MIT" +license-file = "LICENSE" +homepage = "https://github.com/saviorand/lightbug_http" +repository = "https://github.com/saviorand/lightbug_http" [tasks] -build = { cmd = "rattler-build build --recipe recipes -c https://conda.modular.com/max -c conda-forge --skip-existing=all", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} } -publish = { cmd = "bash scripts/publish.sh", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } } +build = { cmd = "bash scripts/build.sh nightly", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} } +publish = { cmd = "bash scripts/publish.sh mojo-community-nightly", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } } +bp = { depends_on=["build", "publish"] } test = { cmd = "magic run mojo run_tests.mojo" } +template = { cmd = "magic run python scripts/templater.py" } bench = { cmd = "magic run mojo bench.mojo" } bench_server = { cmd = "magic run mojo build bench_server.mojo && ./bench_server ; rm bench_server" } [dependencies] max = ">=24.5.0,<25" gojo = ">=0.1.12" -small_time = ">=0.1.4.nightly" \ No newline at end of file +small_time = ">=0.1.4.nightly" + +[feature.nightly] +channels = ["conda-forge", "https://conda.modular.com/max-nightly", "https://repo.prefix.dev/mojo-community-nightly", "https://repo.prefix.dev/mojo-community"] + +[feature.nightly.dependencies] +max = ">=24.5.0,<25" +gojo = ">=0.1.12" +small_time = ">=0.1.4.nightly" + +[environments] +nightly = ["nightly"] \ No newline at end of file diff --git a/recipes/recipe.tmpl b/recipes/recipe.tmpl new file mode 100644 index 00000000..f860e47c --- /dev/null +++ b/recipes/recipe.tmpl @@ -0,0 +1,28 @@ +# yaml-language-server: $schema=https://raw.githubusercontent.com/prefix-dev/recipe-format/main/schema.json + +context: + version: "13.4.2" + +package: + name: {{NAME}} + version: {{VERSION}} + +source: + - path: . + - path: ../{{LICENSE_FILE}} + +build: + script: + - mkdir -p ${PREFIX}/lib/mojo + - magic run mojo package {{NAME}} -o ${PREFIX}/lib/mojo/{{NAME}}.mojopkg + +requirements: + run: + {{DEPENDENCIES}} + +about: + homepage: {{HOMEPAGE}} + license: {{LICENSE}} + license_file: {{LICENSE_FILE}} + summary: {{DESCRIPTION}} + repository: {{REPOSITORY}} \ No newline at end of file diff --git a/recipes/recipe.yaml b/recipes/recipe.yaml deleted file mode 100644 index 719135d2..00000000 --- a/recipes/recipe.yaml +++ /dev/null @@ -1,30 +0,0 @@ -# yaml-language-server: $schema=https://raw.githubusercontent.com/prefix-dev/recipe-format/main/schema.json - -context: - version: "13.4.2" - -package: - name: "lightbug_http" - version: 0.1.4.nightly - -source: - - path: ../lightbug_http - - path: ../LICENSE - -build: - script: - - mkdir -p ${PREFIX}/lib/mojo - - magic run mojo package . -o ${PREFIX}/lib/mojo/lightbug_http.mojopkg - -requirements: - run: - - max >=24.5.0 - - gojo == 0.1.12 - - small_time == 0.1.4.nightly - -about: - homepage: https://github.com/saviorand/lightbug_http - license: MIT - license_file: LICENSE - summary: Lightbug is a simple and sweet HTTP framework for Mojo - repository: https://github.com/saviorand/lightbug_http diff --git a/scripts/build.sh b/scripts/build.sh new file mode 100644 index 00000000..367af564 --- /dev/null +++ b/scripts/build.sh @@ -0,0 +1,11 @@ +#!/bin/bash +set -e +# The environment to build the package for. Usually "default", but might be "nightly" or others. +ENVIRONMENT="${1-default}" +if [[ "${ENVIRONMENT}" == "--help" ]]; then + echo "Usage: ENVIRONMENT - Argument 1 corresponds with the environment you wish to build the package for." + exit 0 +fi +magic run template -m "${ENVIRONMENT}" +rattler-build build -r src -c https://conda.modular.com/max -c conda-forge --skip-existing=all +rm recipes/recipe.yaml \ No newline at end of file diff --git a/scripts/publish.sh b/scripts/publish.sh index e081260c..e3b14c62 100644 --- a/scripts/publish.sh +++ b/scripts/publish.sh @@ -1,8 +1,10 @@ #!/bin/bash +CHANNEL=${1-'mojo-community'} +echo "Publishing packages to: $CHANNEL" # ignore errors because we want to ignore duplicate packages for file in $CONDA_BLD_PATH/**/*.conda; do - magic run rattler-build upload prefix -c "mojo-community" "$file" || true + magic run rattler-build upload prefix -c "$CHANNEL" "$file" || true done rm $CONDA_BLD_PATH/**/*.conda \ No newline at end of file diff --git a/scripts/templater.py b/scripts/templater.py new file mode 100644 index 00000000..ee294cbd --- /dev/null +++ b/scripts/templater.py @@ -0,0 +1,70 @@ +import tomllib +import argparse +from typing import Any + + +def build_dependency_list(dependencies: dict[str, str]) -> list[str]: + deps: list[str] = [] + for name, version in dependencies.items(): + start = 0 + operator = "==" + if version[0] in {'<', '>'}: + if version[1] != "=": + operator = version[0] + start = 1 + else: + operator = version[:2] + start = 2 + + deps.append(f"- {name} {operator} {version[start:]}") + + return deps + + +def main(): + # Configure the parser to receive the mode argument. + parser = argparse.ArgumentParser(description='Generate a recipe for the project.') + parser.add_argument('-m', + '--mode', + default="default", + help="The environment to generate the recipe for. Defaults to 'default' for the standard vers" + ) + args = parser.parse_args() + + # Load the project configuration and recipe template. + config: dict[str, Any] + with open('mojoproject.toml', 'rb') as f: + config = tomllib.load(f) + + recipe: str + with open('recipes/template.yaml', 'r') as f: + recipe = f.read() + + # Replace the placeholders in the recipe with the project configuration. + recipe = recipe \ + .replace("{{NAME}}", config["project"]["name"]) \ + .replace("{{DESCRIPTION}}", config["project"]["description"]) \ + .replace("{{LICENSE}}", config["project"]["license"]) \ + .replace("{{LICENSE_FILE}}", config["project"]["license-file"]) \ + .replace("{{HOMEPAGE}}", config["project"]["homepage"]) \ + .replace("{{REPOSITORY}}", config["project"]["repository"]) \ + .replace("{{VERSION}}", config["project"]["version"]) + + # Dependencies are the only notable field that changes between environments. + dependencies: dict[str, str] + match args.mode: + case "default": + dependencies = config["dependencies"] + case _: + dependencies = config["feature"][args.mode]["dependencies"] + + deps = build_dependency_list(dependencies) + recipe = recipe.replace("{{DEPENDENCIES}}", "\n".join(deps)) + + # Write the final recipe. + with open('recipes/recipe.yaml', 'w+') as f: + recipe = f.write(recipe) + + +if __name__ == '__main__': + main() \ No newline at end of file