From 3176d5cea1c8878f80258b80300c7e8816aeaafe Mon Sep 17 00:00:00 2001 From: Noortheen Raja Date: Sun, 17 Mar 2024 23:52:53 +0530 Subject: [PATCH] refactor: remove usage of cookiecutter with copier-template-extensions --- README.md | 2 +- copier.yml | 8 +++- extensions.py | 46 +++++++++++++++++++ .../{{ project_repo_name }}/LICENSE | 2 +- pyproject.toml | 3 +- 5 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 extensions.py diff --git a/README.md b/README.md index 2a49593..babfa51 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/copier.yml b/copier.yml index 506cdf9..b36070b 100644 --- a/copier.yml +++ b/copier.yml @@ -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 ### @@ -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. diff --git a/extensions.py b/extensions.py new file mode 100644 index 0000000..e73881e --- /dev/null +++ b/extensions.py @@ -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 diff --git a/project-template/{{ project_repo_name }}/LICENSE b/project-template/{{ project_repo_name }}/LICENSE index 8941657..aa67b81 100644 --- a/project-template/{{ project_repo_name }}/LICENSE +++ b/project-template/{{ project_repo_name }}/LICENSE @@ -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 diff --git a/pyproject.toml b/pyproject.toml index bb455d0..55471d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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]