Skip to content

Commit 060ae03

Browse files
committed
add templater from gojo
1 parent 01d8fdd commit 060ae03

File tree

6 files changed

+133
-35
lines changed

6 files changed

+133
-35
lines changed

mojoproject.toml

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,36 @@
11
[project]
22
authors = ["saviorand"]
3-
channels = ["conda-forge", "https://conda.modular.com/max-nightly", "https://repo.prefix.dev/mojo-community-nightly", "https://repo.prefix.dev/mojo-community"]
3+
channels = ["conda-forge", "https://conda.modular.com/max-nightly", "https://repo.prefix.dev/mojo-community"]
44
description = "Simple and fast HTTP framework for Mojo!"
55
name = "lightbug_http"
66
platforms = ["osx-arm64", "linux-64"]
77
version = "0.1.4"
8+
license = "MIT"
9+
license-file = "LICENSE"
10+
homepage = "https://github.com/saviorand/lightbug_http"
11+
repository = "https://github.com/saviorand/lightbug_http"
812

913
[tasks]
10-
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"} }
11-
publish = { cmd = "bash scripts/publish.sh", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } }
14+
build = { cmd = "bash scripts/build.sh nightly", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} }
15+
publish = { cmd = "bash scripts/publish.sh mojo-community-nightly", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } }
16+
bp = { depends_on=["build", "publish"] }
1217
test = { cmd = "magic run mojo run_tests.mojo" }
18+
template = { cmd = "magic run python scripts/templater.py" }
1319
bench = { cmd = "magic run mojo bench.mojo" }
1420
bench_server = { cmd = "magic run mojo build bench_server.mojo && ./bench_server ; rm bench_server" }
1521

1622
[dependencies]
1723
max = ">=24.5.0,<25"
1824
gojo = ">=0.1.12"
19-
small_time = ">=0.1.4.nightly"
25+
small_time = ">=0.1.4.nightly"
26+
27+
[feature.nightly]
28+
channels = ["conda-forge", "https://conda.modular.com/max-nightly", "https://repo.prefix.dev/mojo-community-nightly", "https://repo.prefix.dev/mojo-community"]
29+
30+
[feature.nightly.dependencies]
31+
max = ">=24.5.0,<25"
32+
gojo = ">=0.1.12"
33+
small_time = ">=0.1.4.nightly"
34+
35+
[environments]
36+
nightly = ["nightly"]

recipes/recipe.tmpl

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# yaml-language-server: $schema=https://raw.githubusercontent.com/prefix-dev/recipe-format/main/schema.json
2+
3+
context:
4+
version: "13.4.2"
5+
6+
package:
7+
name: {{NAME}}
8+
version: {{VERSION}}
9+
10+
source:
11+
- path: .
12+
- path: ../{{LICENSE_FILE}}
13+
14+
build:
15+
script:
16+
- mkdir -p ${PREFIX}/lib/mojo
17+
- magic run mojo package {{NAME}} -o ${PREFIX}/lib/mojo/{{NAME}}.mojopkg
18+
19+
requirements:
20+
run:
21+
{{DEPENDENCIES}}
22+
23+
about:
24+
homepage: {{HOMEPAGE}}
25+
license: {{LICENSE}}
26+
license_file: {{LICENSE_FILE}}
27+
summary: {{DESCRIPTION}}
28+
repository: {{REPOSITORY}}

recipes/recipe.yaml

Lines changed: 0 additions & 30 deletions
This file was deleted.

scripts/build.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/bin/bash
2+
set -e
3+
# The environment to build the package for. Usually "default", but might be "nightly" or others.
4+
ENVIRONMENT="${1-default}"
5+
if [[ "${ENVIRONMENT}" == "--help" ]]; then
6+
echo "Usage: ENVIRONMENT - Argument 1 corresponds with the environment you wish to build the package for."
7+
exit 0
8+
fi
9+
magic run template -m "${ENVIRONMENT}"
10+
rattler-build build -r src -c https://conda.modular.com/max -c conda-forge --skip-existing=all
11+
rm recipes/recipe.yaml

scripts/publish.sh

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
#!/bin/bash
22

3+
CHANNEL=${1-'mojo-community'}
4+
echo "Publishing packages to: $CHANNEL"
35
# ignore errors because we want to ignore duplicate packages
46
for file in $CONDA_BLD_PATH/**/*.conda; do
5-
magic run rattler-build upload prefix -c "mojo-community" "$file" || true
7+
magic run rattler-build upload prefix -c "$CHANNEL" "$file" || true
68
done
79

810
rm $CONDA_BLD_PATH/**/*.conda

scripts/templater.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import tomllib
2+
import argparse
3+
from typing import Any
4+
5+
6+
def build_dependency_list(dependencies: dict[str, str]) -> list[str]:
7+
deps: list[str] = []
8+
for name, version in dependencies.items():
9+
start = 0
10+
operator = "=="
11+
if version[0] in {'<', '>'}:
12+
if version[1] != "=":
13+
operator = version[0]
14+
start = 1
15+
else:
16+
operator = version[:2]
17+
start = 2
18+
19+
deps.append(f"- {name} {operator} {version[start:]}")
20+
21+
return deps
22+
23+
24+
def main():
25+
# Configure the parser to receive the mode argument.
26+
parser = argparse.ArgumentParser(description='Generate a recipe for the project.')
27+
parser.add_argument('-m',
28+
'--mode',
29+
default="default",
30+
help="The environment to generate the recipe for. Defaults to 'default' for the standard vers"
31+
)
32+
args = parser.parse_args()
33+
34+
# Load the project configuration and recipe template.
35+
config: dict[str, Any]
36+
with open('mojoproject.toml', 'rb') as f:
37+
config = tomllib.load(f)
38+
39+
recipe: str
40+
with open('recipes/template.yaml', 'r') as f:
41+
recipe = f.read()
42+
43+
# Replace the placeholders in the recipe with the project configuration.
44+
recipe = recipe \
45+
.replace("{{NAME}}", config["project"]["name"]) \
46+
.replace("{{DESCRIPTION}}", config["project"]["description"]) \
47+
.replace("{{LICENSE}}", config["project"]["license"]) \
48+
.replace("{{LICENSE_FILE}}", config["project"]["license-file"]) \
49+
.replace("{{HOMEPAGE}}", config["project"]["homepage"]) \
50+
.replace("{{REPOSITORY}}", config["project"]["repository"]) \
51+
.replace("{{VERSION}}", config["project"]["version"])
52+
53+
# Dependencies are the only notable field that changes between environments.
54+
dependencies: dict[str, str]
55+
match args.mode:
56+
case "default":
57+
dependencies = config["dependencies"]
58+
case _:
59+
dependencies = config["feature"][args.mode]["dependencies"]
60+
61+
deps = build_dependency_list(dependencies)
62+
recipe = recipe.replace("{{DEPENDENCIES}}", "\n".join(deps))
63+
64+
# Write the final recipe.
65+
with open('recipes/recipe.yaml', 'w+') as f:
66+
recipe = f.write(recipe)
67+
68+
69+
if __name__ == '__main__':
70+
main()

0 commit comments

Comments
 (0)