generated from BrianPugh/python-template
-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from BrianPugh/belay-new
"belay new" command
- Loading branch information
Showing
8 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters