Skip to content

Commit

Permalink
Merge pull request #49 from BrianPugh/belay-new
Browse files Browse the repository at this point in the history
"belay new" command
  • Loading branch information
BrianPugh authored Nov 27, 2022
2 parents 9c8630b + cd7e6a7 commit 84a095e
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion belay/cli/install.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
2 changes: 2 additions & 0 deletions belay/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
47 changes: 47 additions & 0 deletions belay/cli/new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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.")):
"""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"

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"])
Empty file.
10 changes: 10 additions & 0 deletions belay/cli/new_template/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import packagename
from machine import Pin


def main():
pass


if __name__ == "__main__":
main()
Empty file.
7 changes: 7 additions & 0 deletions belay/cli/new_template/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[tool.belay]
name = "packagename"

[tool.belay.dependencies]

[tool.pytest.ini_options]
pythonpath = ".belay-lib"
4 changes: 4 additions & 0 deletions docs/source/Package Manager.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 84a095e

Please sign in to comment.