Skip to content

Commit

Permalink
add templater from gojo
Browse files Browse the repository at this point in the history
  • Loading branch information
saviorand committed Sep 25, 2024
1 parent 01d8fdd commit 060ae03
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 35 deletions.
25 changes: 21 additions & 4 deletions mojoproject.toml
Original file line number Diff line number Diff line change
@@ -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"
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"]
28 changes: 28 additions & 0 deletions recipes/recipe.tmpl
Original file line number Diff line number Diff line change
@@ -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}}
30 changes: 0 additions & 30 deletions recipes/recipe.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions scripts/templater.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 060ae03

Please sign in to comment.