Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #33

Merged
merged 3 commits into from
Feb 5, 2023
Merged

Dev #33

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 14 additions & 3 deletions managetemplates/cli/cli_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import managetemplates
from managetemplates import __version__, constants
from managetemplates.utilities.reverse import reverse_test_project
from managetemplates.utilities.template_var_syntax import filesystem_template_var_syntax
from managetemplates.utilities.template_var_syntax import content_template_var_syntax, filesystem_template_var_syntax


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -92,7 +92,7 @@ def update():

############################################################################
# Update 'piptools-python' template:
# piptools-python/{{cookiecutter.package_name}}/requirements*.txt
# piptools-python/{{ cookiecutter.package_name }}/requirements*.txt

package_path = constants.PACKAGE_ROOT / 'piptools-python' / '{{ cookiecutter.package_name }}'
assert_is_dir(package_path)
Expand Down Expand Up @@ -163,12 +163,23 @@ def publish():
@app.command()
def fix_filesystem():
"""
Unify cookiecutter variables in the filesystem. e.g.: "/{{foo}}/{{bar}}.txt" -> "/{{ foo }}/{{ bar }}.txt"
Unify cookiecutter variables in the file/directory paths.
e.g.: "/{{foo}}/{{bar}}.txt" -> "/{{ foo }}/{{ bar }}.txt" # fmt: skip
"""
rename_count = filesystem_template_var_syntax(path=PACKAGE_ROOT)
sys.exit(rename_count)


@app.command()
def fix_file_content():
"""
Unify cookiecutter variables in file content.
e.g.: "{{foo}}" -> "{{ foo }}" # fmt: skip
"""
fixed_files = content_template_var_syntax(path=PACKAGE_ROOT)
sys.exit(fixed_files)


def _call_darker(*args):
# Work-a-round for:
#
Expand Down
16 changes: 15 additions & 1 deletion managetemplates/tests/test_project_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@
from manageprojects.utilities.subprocess_utils import verbose_check_output

from managetemplates import __version__
from managetemplates.cli.cli_app import check_code_style, fix_code_style, fix_filesystem, install, publish, update
from managetemplates.cli.cli_app import (
check_code_style,
fix_code_style,
fix_file_content,
fix_filesystem,
install,
publish,
update,
)
from managetemplates.constants import PACKAGE_ROOT, REQ_DEV_TXT_PATH, REQ_TXT_PATH
from managetemplates.tests.base import BaseTestCase
from managetemplates.utilities.test_project_utils import Call, SubprocessCallMock
Expand Down Expand Up @@ -223,3 +231,9 @@ def test_filesystem_var_syntax(self):
fix_filesystem()
except SystemExit as err:
self.assertEqual(err.code, 0)

def test_file_content_var_syntax(self):
try:
fix_file_content()
except SystemExit as err:
self.assertEqual(err.code, 0)
30 changes: 29 additions & 1 deletion managetemplates/utilities/template_var_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def filesystem_template_var_syntax(path: Path) -> int:
assert_is_file(path)

origin_path_str = str(path)
new_path_str = re.sub(r'{{(\S+?)}}', r'{{ \1 }}', origin_path_str)
new_path_str = re.sub(r'{{(\S+?)}}', r'{{ \1 }}', origin_path_str) # fmt: skip
if new_path_str != origin_path_str:
new_path = Path(new_path_str)
rel_new_path = new_path.relative_to(git_root_path)
Expand Down Expand Up @@ -53,3 +53,31 @@ def filesystem_template_var_syntax(path: Path) -> int:
print(f'{rename_count} files/directories renamed')

return rename_count


def content_template_var_syntax(path: Path) -> int:
print(f'Fix Cookiecutter variable name syntax in file content of: {path}')
git = Git(cwd=path, detect_root=True)
git_root_path = git.cwd
fixed_files = 0
for path in git.ls_files(verbose=False):
assert_is_file(path)
origin_content = path.read_text(encoding='UTF-8')
buffer = []
for line in origin_content.splitlines(keepends=True):
if 'fmt: skip' not in line:
line = re.sub(r'{{(\S+?)}}', r'{{ \1 }}', line) # fmt: skip
buffer.append(line)

new_content = ''.join(buffer)
if origin_content != new_content:
print(f'Fix content of: {path.relative_to(git_root_path)}')
path.write_text(new_content, encoding='UTF-8')
fixed_files += 1

if not fixed_files:
print('Nothing to fixed, ok.')
else:
print(f'{fixed_files} file content fixed.')

return fixed_files
2 changes: 1 addition & 1 deletion pipenv-python/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
"package_name": "your_cool_package",
"package_version": "0.0.1",
"package_description": "A minimal Python package",
"package_url": "https://github.com/{{ cookiecutter.github_username }}/{{cookiecutter.package_name}}",
"package_url": "https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.package_name }}",
"license": "GPL-3.0-or-later"
}
2 changes: 1 addition & 1 deletion pipenv-python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PipenvPythonTemplateTestCase(BaseTestCase):
def test_basic(self):
pkg_path: Path = run_cookiecutter(
template_name='pipenv-python',
final_name='your_cool_package', # {{cookiecutter.package_name}} replaced!
final_name='your_cool_package', # {{ cookiecutter.package_name }} replaced!
# force_recreate=True
force_recreate=False,
)
Expand Down
2 changes: 1 addition & 1 deletion piptools-python/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"package_name": "your_cool_package",
"package_version": "0.0.1",
"package_description": "A minimal Python package",
"package_url": "https://github.com/{{ cookiecutter.github_username }}/{{cookiecutter.package_name}}",
"package_url": "https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.package_name }}",
"issues_url": "{{ cookiecutter.package_url }}/issues",
"license": "GPL-3.0-or-later"
}
2 changes: 1 addition & 1 deletion piptools-python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class PiptoolsPythonTemplateTestCase(BaseTestCase):
def test_basic(self):
pkg_path: Path = run_cookiecutter(
template_name='piptools-python',
final_name='your_cool_package', # {{cookiecutter.package_name}} replaced!
final_name='your_cool_package', # {{ cookiecutter.package_name }} replaced!
# force_recreate=True
force_recreate=False,
)
Expand Down
2 changes: 1 addition & 1 deletion piptools-python/{{ cookiecutter.package_name }}/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@

# script file defined in pyproject.toml as [console_scripts]
# (Under Windows: ".exe" not added!)
PROJECT_SHELL_SCRIPT = BIN_PATH / '{{cookiecutter.package_name}}'
PROJECT_SHELL_SCRIPT = BIN_PATH / '{{ cookiecutter.package_name }}'


def get_dep_hash():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""


from {{cookiecutter.package_name}}.cli import cli_app
from {{ cookiecutter.package_name }}.cli import cli_app


def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import tomli
import {{ cookiecutter.package_name }}
from bx_py_utils.path import assert_is_file
from {{cookiecutter.package_name}} import __version__
from {{cookiecutter.package_name}}.cli.cli_app import check_code_style, fix_code_style
from {{ cookiecutter.package_name }} import __version__
from {{ cookiecutter.package_name }}.cli.cli_app import check_code_style, fix_code_style
from bx_py_utils.test_utils.redirect import RedirectOut


Expand Down
2 changes: 1 addition & 1 deletion poetry-python/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"package_name": "your_cool_package",
"package_version": "0.0.1",
"package_description": "A minimal Python package using Poetry",
"package_url": "https://github.com/{{ cookiecutter.github_username }}/{{cookiecutter.package_name}}",
"package_url": "https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.package_name }}",
"issues_url": "{{ cookiecutter.package_url }}/issues",
"license": "GPL-3.0-or-later"
}
2 changes: 1 addition & 1 deletion poetry-python/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class PoetryPythonTemplateTestCase(BaseTestCase):
def test_basic(self):
pkg_path: Path = run_cookiecutter(
template_name='poetry-python',
final_name='your_cool_package', # {{cookiecutter.package_name}} replaced!
final_name='your_cool_package', # {{ cookiecutter.package_name }} replaced!
# force_recreate=True
force_recreate=False,
)
Expand Down
8 changes: 4 additions & 4 deletions yunohost_django_package/cookiecutter.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
"github_username": "{{ cookiecutter.full_name.lower()|replace(' ', '-') }}",
"author_email": "{{ cookiecutter.github_username }}@example.tld",
"upstream_pkg_name": "django_example",
"upstream_url": "https://github.com/{{ cookiecutter.github_username }}/{{cookiecutter.upstream_pkg_name}}",
"ynh_app_pkg_name": "{{cookiecutter.upstream_pkg_name}}_ynh",
"ynh_app_url": "https://github.com/YunoHost-Apps/{{cookiecutter.upstream_pkg_name}}_ynh",
"bug_tracker_url": "{{cookiecutter.upstream_url}}/issues",
"upstream_url": "https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.upstream_pkg_name }}",
"ynh_app_pkg_name": "{{ cookiecutter.upstream_pkg_name }}_ynh",
"ynh_app_url": "https://github.com/YunoHost-Apps/{{ cookiecutter.upstream_pkg_name }}_ynh",
"bug_tracker_url": "{{ cookiecutter.upstream_url }}/issues",
"upstream_version": "0.0.1",
"ynh_version": "1",
"package_description": "A example YunoHost App",
Expand Down
2 changes: 1 addition & 1 deletion yunohost_django_package/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class YunohostDjangoPackageTemplateTestCase(BaseTestCase):
def test_basic(self):
pkg_path: Path = run_cookiecutter(
template_name='yunohost_django_package',
final_name='django_example_ynh', # {{cookiecutter.ynh_app_pkg_name}} replaced!
final_name='django_example_ynh', # {{ cookiecutter.ynh_app_pkg_name }} replaced!
# force_recreate=True
force_recreate=False,
extra_context=dict(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
from django_yunohost_integration.secret_key import get_or_create_secret as __get_or_create_secret


# {{cookiecutter.upstream_url}}
from {{cookiecutter.upstream_pkg_name}}.settings.prod import * # noqa:F401,F403 isort:skip
# {{ cookiecutter.upstream_url }}
from {{ cookiecutter.upstream_pkg_name }}.settings.prod import * # noqa:F401,F403 isort:skip


from django_yunohost_integration.base_settings import LOGGING # noqa:F401 isort:skip
Expand All @@ -28,7 +28,7 @@
PUBLIC_PATH = __Path('__PUBLIC_PATH__') # /var/www/$app
assert PUBLIC_PATH.is_dir(), f'Directory not exists: {PUBLIC_PATH}'

LOG_FILE = __Path('__LOG_FILE__') # /var/log/$app/{{cookiecutter.ynh_app_pkg_name}}.log
LOG_FILE = __Path('__LOG_FILE__') # /var/log/$app/{{ cookiecutter.ynh_app_pkg_name }}.log
assert LOG_FILE.is_file(), f'File not exists: {LOG_FILE}'

PATH_URL = '__PATH_URL__' # $YNH_APP_ARG_PATH
Expand Down Expand Up @@ -160,7 +160,7 @@
LOGGING['handlers']['log_file']['filename'] = str(LOG_FILE)

# Example how to add logging to own app:
LOGGING['loggers']['{{cookiecutter.upstream_pkg_name}}'] = {
LOGGING['loggers']['{{ cookiecutter.upstream_pkg_name }}'] = {
'handlers': ['syslog', 'log_file', 'mail_admins'],
'level': 'INFO',
'propagate': False,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
# Prefix all urls with "PATH_URL":
urlpatterns = [
path('', RedirectView.as_view(url=f'{settings.PATH_URL}/')),
path(f'{settings.PATH_URL}/', include('{{cookiecutter.upstream_pkg_name}}.urls')),
path(f'{settings.PATH_URL}/', include('{{ cookiecutter.upstream_pkg_name }}.urls')),
]
else:
# Installed to domain root, without a path prefix
# Just use the default project urls.py
from {{cookiecutter.upstream_pkg_name}}.urls import urlpatterns # noqa
from {{ cookiecutter.upstream_pkg_name }}.urls import urlpatterns # noqa
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

{{ cookiecutter.package_description }}

[![Integration level](https://dash.yunohost.org/integration/{{cookiecutter.ynh_app_pkg_name}}.svg)](https://dash.yunohost.org/appci/app/{{cookiecutter.ynh_app_pkg_name}}) ![](https://ci-apps.yunohost.org/ci/badges/{{cookiecutter.ynh_app_pkg_name}}.status.svg) ![](https://ci-apps.yunohost.org/ci/badges/{{cookiecutter.ynh_app_pkg_name}}.maintain.svg)
[![Install {{cookiecutter.ynh_app_pkg_name}} with YunoHost](https://install-app.yunohost.org/install-with-yunohost.svg)](https://install-app.yunohost.org/?app={{cookiecutter.ynh_app_pkg_name}})
[![Integration level](https://dash.yunohost.org/integration/{{ cookiecutter.ynh_app_pkg_name }}.svg)](https://dash.yunohost.org/appci/app/{{ cookiecutter.ynh_app_pkg_name }}) ![](https://ci-apps.yunohost.org/ci/badges/{{ cookiecutter.ynh_app_pkg_name }}.status.svg) ![](https://ci-apps.yunohost.org/ci/badges/{{ cookiecutter.ynh_app_pkg_name }}.maintain.svg)
[![Install {{ cookiecutter.ynh_app_pkg_name }} with YunoHost](https://install-app.yunohost.org/install-with-yunohost.svg)](https://install-app.yunohost.org/?app={{ cookiecutter.ynh_app_pkg_name }})


Pull requests welcome ;)