Skip to content

Commit

Permalink
refactor: remove usage of cookiecutter with copier-template-extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
jnoortheen committed Mar 17, 2024
1 parent 1ec2617 commit 3176d5c
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ xpip install copier jinja2-time cookiecutter

# OR using pipx (https://pypa.github.io/pipx/):
pipx install copier>=9
pipx inject copier jinja2-time cookiecutter
pipx inject copier copier-templates-extensions
```

Create your new xontrib:
Expand Down
8 changes: 6 additions & 2 deletions copier.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ _min_copier_version: "9.0.0"
_subdirectory: project-template
_templates_suffix: "" # empty suffix will process all files
_jinja_extensions:
- jinja2_time.TimeExtension
- cookiecutter.extensions.SlugifyExtension
- copier_templates_extensions.TemplateExtensionLoader
- extensions.py:CurrentYearExtension
- extensions.py:GitExtension
- extensions.py:SlugifyExtension

### question ###

Expand All @@ -15,10 +17,12 @@ full_name:
help: Enter your full name. This will appear in the License generated and `pyproject.toml`'s authors field.

email:
default: "{{ git_user_email }}"
type: str
help: Your E-mail address. This will appear in the License generated and `pyproject.toml`'s authors field.

github_username:
default: "{{ git_user_name }}"
type: str
help: You Github username or organization name under which the projects lives.

Expand Down
46 changes: 46 additions & 0 deletions extensions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# copied from https://github.com/pawamoy/copier-pdm/blob/main/extensions.py
# Thanks to the original author

import re
import subprocess
import unicodedata
from datetime import date

from jinja2.ext import Extension


def git_user_name() -> str:
return subprocess.getoutput("git config user.name").strip()


def git_user_email() -> str:
return subprocess.getoutput("git config user.email").strip()


def slugify(value, separator="-"):
value = (
unicodedata.normalize("NFKD", str(value))
.encode("ascii", "ignore")
.decode("ascii")
)
value = re.sub(r"[^\w\s-]", "", value.lower())
return re.sub(r"[-_\s]+", separator, value).strip("-_")


class GitExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.globals["git_user_name"] = git_user_name
environment.globals["git_user_email"] = git_user_email


class SlugifyExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.filters["slugify"] = slugify


class CurrentYearExtension(Extension):
def __init__(self, environment):
super().__init__(environment)
environment.globals["current_year"] = date.today().year
2 changes: 1 addition & 1 deletion project-template/{{ project_repo_name }}/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) {% now 'local', '%Y' %}, {{ full_name }}
Copyright (c) {{ current_year }}, {{ full_name }}

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ license = { text = "MIT" }
requires-python = ">=3.8,<4.0"
dependencies = [
"copier>=9",
"cookiecutter<2.0.0,>=1.7.3",
"jinja2-time",
"copier-templates-extensions"
]

[project.optional-dependencies]
Expand Down

0 comments on commit 3176d5c

Please sign in to comment.