Skip to content

Commit

Permalink
add templater (#52)
Browse files Browse the repository at this point in the history
* add templater

* fix benchmark script
  • Loading branch information
thatstoasty authored Sep 25, 2024
1 parent 3073d59 commit c43459f
Show file tree
Hide file tree
Showing 11 changed files with 4,687 additions and 236 deletions.
12 changes: 6 additions & 6 deletions benchmarks/string_builder.mojo
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ fn main() raises:
report = benchmark.run[benchmark_string_builder[100]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_concat - 1000 batches")
report = benchmark.run[benchmark_concat[1000]](max_iters=20)
report.print(benchmark.Unit.ms)
# print("Running benchmark_concat - 1000 batches")
# report = benchmark.run[benchmark_concat[1000]](max_iters=20)
# report.print(benchmark.Unit.ms)

print("Running benchmark_string_builder - 1000 batches")
report = benchmark.run[benchmark_string_builder[1000]](max_iters=20)
report.print(benchmark.Unit.ms)

print("Running benchmark_concat - 10000 batches")
report = benchmark.run[benchmark_concat[10000]](max_iters=2)
report.print(benchmark.Unit.ms)
# print("Running benchmark_concat - 10000 batches")
# report = benchmark.run[benchmark_concat[10000]](max_iters=2)
# report.print(benchmark.Unit.ms)

print("Running benchmark_string_builder - 10000 batches")
report = benchmark.run[benchmark_string_builder[10000]](max_iters=20)
Expand Down
4,702 changes: 4,524 additions & 178 deletions magic.lock

Large diffs are not rendered by default.

18 changes: 17 additions & 1 deletion mojoproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,28 @@ description = "Experiments in porting over Golang stdlib into Mojo."
name = "gojo"
platforms = ["osx-arm64", "linux-64"]
version = "0.1.12"
license = "MIT"
license-file = "LICENSE"
homepage = "https://github.com/thatstoasty/gojo"
repository = "https://github.com/thatstoasty/gojo"

[tasks]
run_file = "bash scripts/run_file.sh"
tests = "bash scripts/tests.sh"
benchmarks = "bash scripts/benchmarks.sh"
build = { cmd = "rattler-build build -r src -c https://conda.modular.com/max -c conda-forge --skip-existing=all", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} }
template = "magic run python scripts/templater.py"
build = { cmd = "bash scripts/build.sh", env = {MODULAR_MOJO_IMPORT_PATH = "$CONDA_PREFIX/lib/mojo"} }
publish = { cmd = "bash scripts/publish.sh", env = { PREFIX_API_KEY = "$PREFIX_API_KEY" } }
bp = { depends_on=["build", "publish"] }

[dependencies]
max = ">=24.5.0,<25"

[feature.nightly]
channels = ["conda-forge", "https://conda.modular.com/max-nightly"]

[feature.nightly.dependencies]
max = ">=24.6.0.dev2024092405"

[environments]
nightly = ["nightly"]
5 changes: 4 additions & 1 deletion scripts/benchmarks.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
#!/bin/bash

set -e

TEMP_DIR=~/tmp
PACKAGE_NAME=gojo
mkdir -p $TEMP_DIR

echo "[INFO] Building $PACKAGE_NAME package and running benchmarks."
cp -R benchmarks/ $TEMP_DIR
cp -a benchmarks/. $TEMP_DIR
magic run mojo package src/$PACKAGE_NAME -o $TEMP_DIR/$PACKAGE_NAME.mojopkg

echo "[INFO] Running benchmarks..."
magic run mojo $TEMP_DIR/scanner.mojo
magic run mojo $TEMP_DIR/string_builder.mojo
magic run mojo $TEMP_DIR/buffer.mojo

echo "[INFO] Cleaning up the benchmarks directory."
rm -R $TEMP_DIR
14 changes: 14 additions & 0 deletions scripts/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/bin/bash

set -e

# The environment to build the package for. Usually "default", but might be "nightly" or others.
ENVRIONMENT="${1-default}"
if [[ "${ENVRIONMENT}" == "--help" ]]; then
echo "Usage: ENVRIONMENT - Argument 1 corresponds with the environment you wish to build the package for."
exit 0
fi

magic run template -m "${ENVRIONMENT}"
rattler-build build -r src -c https://conda.modular.com/max -c conda-forge --skip-existing=all
rm src/recipe.yaml
5 changes: 4 additions & 1 deletion scripts/publish.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#!/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
18 changes: 18 additions & 0 deletions scripts/run_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#!/bin/bash

set -e

FILE=${1}
TEMP_DIR=~/tmp
PACKAGE_NAME=gojo
mkdir -p $TEMP_DIR

echo "[INFO] Building $PACKAGE_NAME package and copying file."
cp -R $FILE $TEMP_DIR
magic run mojo package src/$PACKAGE_NAME -o $TEMP_DIR/$PACKAGE_NAME.mojopkg

echo "[INFO] Running file..."
readlink -f $TEMP_DIR/*.mojo | xargs -I {} magic run mojo {}

echo "[INFO] Cleaning up the tmp directory."
rm -R $TEMP_DIR
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('src/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('src/recipe.yaml', 'w+') as f:
recipe = f.write(recipe)


if __name__ == '__main__':
main()
2 changes: 2 additions & 0 deletions scripts/tests.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#!/bin/bash

set -e

TEMP_DIR=~/tmp
PACKAGE_NAME=gojo
mkdir -p $TEMP_DIR
Expand Down
28 changes: 28 additions & 0 deletions src/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}}
49 changes: 0 additions & 49 deletions src/recipe.yaml

This file was deleted.

0 comments on commit c43459f

Please sign in to comment.