Skip to content

Commit

Permalink
Adjust the cookiecutters
Browse files Browse the repository at this point in the history
This will help with the transition to bump-my-version and the new
staging docs.

[noissue]
  • Loading branch information
mdellweg committed Apr 15, 2024
1 parent 9d47c0b commit 7ea839f
Show file tree
Hide file tree
Showing 19 changed files with 129 additions and 5 deletions.
10 changes: 7 additions & 3 deletions cookiecutter/apply_templates.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/bin/env python3

import tomllib
from pathlib import Path

import toml
import yaml

from cookiecutter.main import cookiecutter
Expand All @@ -11,11 +11,15 @@
sections = []
overwrite = True

with open("pyproject.toml") as fp:
config = toml.load(fp)["tool"]["pulp_cli_template"]
with open("pyproject.toml", "rb") as fp:
pyproject_toml = tomllib.load(fp)

config = pyproject_toml["tool"]["pulp_cli_template"]
config.setdefault("main_package", config["app_label"])

# CI
sections.append("ci")
config["current_version"] = pyproject_toml["project"]["version"]
with open(".github/workflows/test.yml") as fp:
config["test_matrix"] = yaml.safe_load(fp)["jobs"]["test"]["strategy"]["matrix"]

Expand Down
4 changes: 3 additions & 1 deletion cookiecutter/ci/cookiecutter.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{
"app_label": "noname",
"main_package": null,
"glue": true,
"docs": false,
"translations": false,
"test_matrix": "- python: \"3.11\"\nimage_tag: \"nightly\"\npulp_api_root: \"/relocated/djnd/\"\n- python: \"3.6\"\nimage_tag: \"latest\"\nlower_bounds: true",
"current_version": "0.0.1.dev",
"test_matrix": null,
"__app_label_suffix": "{{ cookiecutter.app_label and '-' + cookiecutter.app_label }}",
"__project_name": "pulp-cli{{ cookiecutter.__app_label_suffix }}",
"__project_slug": "{{ cookiecutter.__project_name | lower | replace(' ', '_') }}",
Expand Down
25 changes: 24 additions & 1 deletion cookiecutter/ci/hooks/post_gen_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,31 @@
import shutil
import sys

import tomlkit

# Merge pyproject.toml.update into pyproject.toml
with open("pyproject.toml", "r") as fp:
pyproject_toml = tomlkit.load(fp)

with open("pyproject.toml.update", "r") as fp:
pyproject_toml_update = tomlkit.load(fp)

# TODO How deep is your merge?
# Is merging on the tool level appropriate?
pyproject_toml["tool"].update(pyproject_toml_update["tool"])

with open("pyproject.toml", "w") as fp:
tomlkit.dump(pyproject_toml, fp)

# Remove unwanted files

REMOVE_PATHS = [
{% if not cookiecutter.glue -%} "CHANGES/pulp-glue{{ cookiecutter.__app_label_suffix }}", {%- endif %}
"pyproject.toml.update",
".bumpversion.cfg",
{% if not cookiecutter.glue -%}
"pulp-glue{{ cookiecutter.__app_label_suffix }}",
"CHANGES/pulp-glue{{ cookiecutter.__app_label_suffix }}",
{%- endif %}
]

for path in REMOVE_PATHS:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "{{ cookiecutter.current_version }}"
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{#
This file is not templated for itself, but it is used to update sections in pyproject.toml.
#}

[tool.pulp_cli_template]
# This section is co-managed by the cookiecutter templates.
# Changes to existing keys should be preserved.
app_label = {{ cookiecutter.app_label | to_toml_value }}
glue = {{ cookiecutter.glue | to_toml_value }}
docs = {{ cookiecutter.docs | to_toml_value }}
translations = {{ cookiecutter.translations | to_toml_value }}
main_package = {{ cookiecutter.main_package | to_toml_value }}

[tool.bumpversion]
# This section is managed by the cookiecutter templates.
current_version = "{{ cookiecutter.current_version }}"
commit = false
tag = false
parse = "(?P<major>\\d+)\\.(?P<minor>\\d+)\\.(?P<patch>\\d+)(\\.(?P<release>[a-z]+))?"
serialize = [
"{major}.{minor}.{patch}.{release}",
"{major}.{minor}.{patch}",
]

[tool.bumpversion.parts.release]
optional_value = "prod"
values = [
"dev",
"prod",
]
{%- if cookiecutter.app_label == "" %}

[[tool.bumpversion.files]]
filename = "./pulp_cli/__init__.py"
search = "__version__ = \"{current_version}\""
replace = "__version__ = \"{new_version}\""
{%- endif %}
{%- if cookiecutter.glue %}

[[tool.bumpversion.files]]
filename = "./pulp-glue{{ cookiecutter.__app_label_suffix }}/pulp_glue/{{ cookiecutter.main_package }}/__init__.py"
search = "__version__ = \"{current_version}\""
replace = "__version__ = \"{new_version}\""
{%- endif %}

[[tool.bumpversion.files]]
filename = "./pulpcore/cli/{{ cookiecutter.main_package }}/__init__.py"
search = "__version__ = \"{current_version}\""
replace = "__version__ = \"{new_version}\""
{%- if cookiecutter.glue %}

[[tool.bumpversion.files]]
filename = "./pulp-glue{{ cookiecutter.__app_label_suffix }}/pyproject.toml"
search = "version = \"{current_version}\""
replace = "version = \"{new_version}\""
{%- endif %}

[[tool.bumpversion.files]]
filename = "./pyproject.toml"
search = "version = \"{current_version}\""
replace = "version = \"{new_version}\""
{%- if cookiecutter.glue %}

[[tool.bumpversion.files]]
filename = "./pyproject.toml"
search = "\"pulp-glue{{ cookiecutter.__app_label_suffix }}=={current_version}\""
replace = "\"pulp-glue{{ cookiecutter.__app_label_suffix }}=={new_version}\""
{%- endif %}
{%- if cookiecutter.docs %}

[[tool.bumpversion.files]]
filename = "./mkdocs.yml"
search = "version: \"{current_version}\""
replace = "version: \"{new_version}\""
{%- endif %}
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
14 changes: 14 additions & 0 deletions cookiecutter/pulp_filter_extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,19 @@ def to_nice_yaml(data: t.Any, level: int = 0, embed_in: str = "") -> str:
return result


def to_toml_value(value: t.Any) -> str:
if isinstance(value, str):
return '"' + value + '"'
elif value is True:
return "true"
elif value is False:
return "false"
elif isinstance(value, int):
return str(value)
else:
raise NotImplementedError("Not an atomic value.")


def to_camel(name: str) -> str:
return name.title().replace("_", "")

Expand All @@ -75,6 +88,7 @@ def __init__(self, environment: Environment):
environment.filters["dash"] = to_dash
environment.filters["snake"] = to_snake
environment.filters["to_nice_yaml"] = to_nice_yaml
environment.filters["to_toml_value"] = to_toml_value
environment.filters["shquote"] = shlex.quote


Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ namespaces = true
"*" = ["py.typed", "locale/*/LC_MESSAGES/*.mo"]

[tool.pulp_cli_template]
# This section is co-managed by the cookiecutter templates.
# Changes to existing keys should be preserved.
app_label = ""
glue = true
docs = true
translations = true
main_package = "common"

[tool.bumpversion]
# This section is managed by the cookiecutter templates.
current_version = "0.25.0.dev"
commit = false
tag = false
Expand Down

0 comments on commit 7ea839f

Please sign in to comment.