From 1c726c355a14c40a57aed6298c34b61cc5cbef4f Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Fri, 18 Nov 2022 09:25:44 -0800 Subject: [PATCH 1/3] belay new command --- belay/cli/main.py | 2 + belay/cli/new.py | 46 +++++++++++++++++++ belay/cli/new_template/README.md | 0 .../cli/new_template/packagename/__init__.py | 0 belay/cli/new_template/pyproject.toml | 7 +++ docs/source/Package Manager.rst | 4 ++ 6 files changed, 59 insertions(+) create mode 100644 belay/cli/new.py create mode 100644 belay/cli/new_template/README.md create mode 100644 belay/cli/new_template/packagename/__init__.py create mode 100644 belay/cli/new_template/pyproject.toml diff --git a/belay/cli/main.py b/belay/cli/main.py index 3ae1f10..0cfe5d2 100644 --- a/belay/cli/main.py +++ b/belay/cli/main.py @@ -7,6 +7,7 @@ from belay.cli.identify import identify from belay.cli.info import info from belay.cli.install import install +from belay.cli.new import new from belay.cli.run import run from belay.cli.sync import sync from belay.cli.update import update @@ -17,6 +18,7 @@ app.command()(identify) app.command()(info) app.command()(install) +app.command()(new) app.command()(run) app.command()(sync) app.command()(update) diff --git a/belay/cli/new.py b/belay/cli/new.py new file mode 100644 index 0000000..ae15d74 --- /dev/null +++ b/belay/cli/new.py @@ -0,0 +1,46 @@ +import importlib.resources as pkg_resources +import re +import shutil +from pathlib import Path + +from packaging.utils import canonicalize_name +from typer import Argument, Option + + +def new(project_name: str = Argument(..., help="Project Name.")): + package_name = canonicalize_name(project_name) + dst_dir = Path() / project_name + template_dir = pkg_resources.files("belay") / "cli" / "new_template" + + shutil.copytree(str(template_dir), str(dst_dir)) + + # Find/Replace Engine + replacements: dict[str, str] = { + "packagename": package_name, + } + + paths = list(dst_dir.rglob("*")) + + def replace(string): + """Replace whole words only.""" + + def _replace(match): + return replacements[match.group(0)] + + return re.sub( + "|".join(r"\b%s\b" % re.escape(s) for s in replacements), _replace, string + ) + + for path in paths: + if path.is_dir(): + continue + + contents = path.read_text() + contents = replace(contents) + path.write_text(contents) + if path.stem in replacements: + dst = path.with_name(replacements[path.stem] + path.suffix) + path.replace(dst) + + # Move the app folder + (dst_dir / "packagename").replace(dst_dir / replacements["packagename"]) diff --git a/belay/cli/new_template/README.md b/belay/cli/new_template/README.md new file mode 100644 index 0000000..e69de29 diff --git a/belay/cli/new_template/packagename/__init__.py b/belay/cli/new_template/packagename/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/belay/cli/new_template/pyproject.toml b/belay/cli/new_template/pyproject.toml new file mode 100644 index 0000000..b56e155 --- /dev/null +++ b/belay/cli/new_template/pyproject.toml @@ -0,0 +1,7 @@ +[tool.belay] +name = "packagename" + +[tool.belay.dependencies] + +[tool.pytest.ini_options] +pythonpath = ".belay-lib" diff --git a/docs/source/Package Manager.rst b/docs/source/Package Manager.rst index b0a4e27..21c5658 100644 --- a/docs/source/Package Manager.rst +++ b/docs/source/Package Manager.rst @@ -44,6 +44,10 @@ more robust code and improve development iteration speed. Commands ^^^^^^^^ +New +--- +``belay new belay-micropython-demo`` creates a new micropython project structure. + Update ------ ``belay update`` iterates over and downloads the dependencies defined From fbbf086484f9e4e9d6d2c844e1451e07c7c8c9ac Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Sun, 27 Nov 2022 15:27:03 -0800 Subject: [PATCH 2/3] Add main.py to template --- belay/cli/new_template/main.py | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 belay/cli/new_template/main.py diff --git a/belay/cli/new_template/main.py b/belay/cli/new_template/main.py new file mode 100644 index 0000000..777787f --- /dev/null +++ b/belay/cli/new_template/main.py @@ -0,0 +1,10 @@ +import packagename +from machine import Pin + + +def main(): + pass + + +if __name__ == "__main__": + main() From cd7e6a7b87f193bfe7d81f93b70be3aadbd6b2ca Mon Sep 17 00:00:00 2001 From: Brian Pugh Date: Sun, 27 Nov 2022 15:29:03 -0800 Subject: [PATCH 3/3] new docstring --- belay/cli/install.py | 2 +- belay/cli/new.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/belay/cli/install.py b/belay/cli/install.py index 1e20cd4..889375f 100644 --- a/belay/cli/install.py +++ b/belay/cli/install.py @@ -21,7 +21,7 @@ def install( None, help="Sync script to /main.py after installing." ), ): - """Sync dependencies and project itself.""" + """Sync dependencies and project itself to device.""" if run and run.suffix != ".py": raise ValueError("Run script MUST be a python file.") if main and main.suffix != ".py": diff --git a/belay/cli/new.py b/belay/cli/new.py index ae15d74..2f920e2 100644 --- a/belay/cli/new.py +++ b/belay/cli/new.py @@ -8,6 +8,7 @@ def new(project_name: str = Argument(..., help="Project Name.")): + """Create a new micropython project structure.""" package_name = canonicalize_name(project_name) dst_dir = Path() / project_name template_dir = pkg_resources.files("belay") / "cli" / "new_template"