Skip to content

Commit

Permalink
Add eject-template command and fixed module_name issue (#10)
Browse files Browse the repository at this point in the history
* add eject-template command

* Fix ejecting of nested templates files

* Fix module_name/package_name usage

* Also show snippets when ejecting templates

* Fix formatting

* Remove redundant template listing in README
  • Loading branch information
fbinz authored Jun 24, 2024
1 parent 3e42e42 commit ef62e1f
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 42 deletions.
44 changes: 11 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,19 @@ METADATA = PackageMetadata(

### Customizing the templates

The default templates are located in the `ambient_package_update/templates` directory.
You can overwrite them by creating a `.ambient-package-update/templates` directory in your project
and create a new file with the same name as the template you want to overwrite.
The following templates are available:
To customize the templates, you can use the `eject-template` command.
Simply run

```bash
python -m ambient_package_update.cli eject-template
```
├── docs
│ ├── conf.py.tpl
│ ├── make.bat.tpl
│ └── Makefile.tpl
├── MANIFEST.in.tpl
├── pyproject.toml.tpl
├── README.md.tpl
├── scripts
│ ├── unix
│ │ ├── install_requirements.sh.tpl
│ │ └── publish_to_pypi.sh.tpl
│ └── windows
│ ├── install_requirements.ps1.tpl
│ └── publish_to_pypi.ps1.tpl
├── setup.cfg.tpl
└── snippets
├── badges.tpl
├── content.tpl
├── contribute.tpl
├── empty.tpl
├── installation.tpl
├── licenses
│ ├── GPL.md
│ └── MIT.md
├── links.tpl
├── maintenance.tpl
├── publish.tpl
└── tagline.tpl
```

from the root of your project and select the template you want to eject.
The chosen template will be copied to `.ambient-package-update/templates`, ready to be customized.

If you want to overwrite template manually, you can find the default templates in the `ambient_package_update/templates` directory.
You can overwrite them by creating a `.ambient-package-update/templates` directory in your project
and create a new file with the same name as the template you want to overwrite.

## Contribution

Expand Down
37 changes: 33 additions & 4 deletions ambient_package_update/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,20 @@ def create_rendered_file(*, template: Path, relative_target_path: Path | str) ->
print(f'> Successfully rendered template "{abs_path}".')


@app.command()
def render_templates():
# Collect all template files and add them to "template_list"
def get_template_list(*, include_snippets: bool = False) -> list[str]:
template_list = [
str(Path(Path(path).relative_to(TEMPLATE_PATH), file))
for path, subdirs, files in os.walk(TEMPLATE_PATH)
for file in files
if "snippets" not in path
if include_snippets or "snippets" not in path
]
return template_list


@app.command()
def render_templates():
# Collect all template files and add them to "template_list"
template_list = get_template_list()

for template in template_list:
print(f"> Found template {template!r}...")
Expand Down Expand Up @@ -127,5 +132,29 @@ def run_tests():
subprocess.call("pytest --ds settings tests", shell=True)


@app.command()
def eject_template():
template_list = get_template_list(include_snippets=True)

for i, template in enumerate(template_list, start=1):
print(f"{i:>2}) {template}")

template_no = typer.prompt("Choose template")
template_no = int(template_no)

if template_no > len(template_list):
raise typer.Abort("Invalid template number")

print(f"Ejecting template {template_list[template_no-1]}")

template = template_list[template_no - 1]

contents = Path(TEMPLATE_PATH / template).read_text()

output = Path(".ambient-package-update/templates") / template
os.makedirs(output.parent, exist_ok=True)
output.write_text(contents)


if __name__ == "__main__":
app()
4 changes: 2 additions & 2 deletions ambient_package_update/templates/pyproject.toml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["flit_core>=3.4"]
build-backend = "flit_core.buildapi"

[project]
name = "{{ package_name|replace("_", "-") }}"
name = "{{ package_name }}"
authors = [{% for author in authors %}
{'name' = '{{ author.name }}', 'email' = '{{ author.email }}'},{% endfor %}
]
Expand Down Expand Up @@ -35,7 +35,7 @@ dependencies = [{% for dependency in dependencies %}
]{% endfor %}{% endif %}

[tool.flit.module]
name = "{{ package_name }}"
name = "{{ module_name }}"

[project.urls]
'Homepage' = 'https://github.com/ambient-innovation/{{ github_package_name|replace("_", "-") }}/'
Expand Down
6 changes: 3 additions & 3 deletions ambient_package_update/templates/snippets/installation.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

- Install the package via pip:

`pip install {{ package_name|replace("_", "-") }}`
`pip install {{ package_name }}`

or via pipenv:

`pipenv install {{ package_name|replace("_", "-") }}`
`pipenv install {{ package_name }}`

- Add module to `INSTALLED_APPS` within the main django `settings.py`:

````
INSTALLED_APPS = (
...
'{{ package_name }}',
'{{ module_name }}',
)
````

Expand Down

0 comments on commit ef62e1f

Please sign in to comment.