Skip to content

Commit

Permalink
Move bumpversion cfg into github template
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellweg committed Nov 19, 2024
1 parent c4460b7 commit 2d842c4
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 40 deletions.
32 changes: 18 additions & 14 deletions plugin-template
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ DEFAULT_SETTINGS = {

DEPRECATED_FILES = {
"github": [
".bumpversion.cfg",
".ci/assets/bindings/.gitkeep",
".ci/scripts/changelog.py",
".ci/scripts/calc_deps_lowerbounds.py",
Expand Down Expand Up @@ -308,8 +309,8 @@ def main():
return 2

if args.latest_release_branch:
write_new_config = True
config["latest_release_branch"] = str(args.latest_release_branch)
write_new_config = True

# Config key is used by the template_config.yml.j2 template to dump
# the config. (note: uses .copy() to avoid a self reference)
Expand Down Expand Up @@ -377,11 +378,6 @@ def append_releasing_to_manifest(plugin_root):
manifest_file.write_text(manifest_text + "exclude releasing.md\n")


def to_nice_yaml(data):
"""Implement a filter for Jinja 2 templates to render human readable YAML."""
return yaml.dump(data, indent=2, allow_unicode=True, default_flow_style=False)


def write_template_section(config, name, plugin_root_dir, verbose=False):
"""
Template or copy all files for the section.
Expand All @@ -402,7 +398,7 @@ def write_template_section(config, name, plugin_root_dir, verbose=False):
env.filters["caps"] = utils.to_caps
env.filters["dash"] = utils.to_dash
env.filters["snake"] = utils.to_snake
env.filters["to_yaml"] = to_nice_yaml
env.filters["to_yaml"] = utils.to_nice_yaml
env.filters["shquote"] = shlex.quote

files_templated = 0
Expand Down Expand Up @@ -450,12 +446,20 @@ def write_template_section(config, name, plugin_root_dir, verbose=False):
if relative_path.endswith(".j2"):
template = env.get_template(relative_path)
destination = destination_relative_path[: -len(".j2")]
write_template_to_file(
template,
plugin_root_dir,
destination,
template_vars,
)
if destination.startswith("pyproject.toml."):
utils.merge_toml(
template,
plugin_root_dir,
destination,
template_vars,
)
else:
template_to_file(
template,
plugin_root_dir,
destination,
template_vars,
)
files_templated += 1
if verbose:
print(f"Templated file: {relative_path}")
Expand Down Expand Up @@ -487,7 +491,7 @@ def generate_relative_path_set(root_dir):
return applicable_paths


def write_template_to_file(template, plugin_root_dir, relative_path, config):
def template_to_file(template, plugin_root_dir, relative_path, config):
"""
Render template with values from the config and write it to the target plugin directory.
"""
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ jinja2
pyyaml
requests~=2.32.3
requests_cache
tomlkit
20 changes: 0 additions & 20 deletions templates/bootstrap/.bumpversion.cfg.j2

This file was deleted.

2 changes: 1 addition & 1 deletion templates/bootstrap/plugin_name/app/__init__.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ class {{ plugin_name | camel }}PluginAppConfig(PulpPluginAppConfig):

name = "{{ plugin_name | snake }}.app"
label = "{{ plugin_app_label }}"
version = "0.1.0a1.dev"
version = "{{ current_version }}"
python_package_name = "{{ plugin_name }}"
domain_compatible = True
2 changes: 1 addition & 1 deletion templates/bootstrap/setup.py.j2
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ with open("requirements.txt") as requirements:

setup(
name="{{ plugin_name | dash }}",
version="0.1.0a1.dev",
version="{{ current_version }}",
description="{{ plugin_name | dash }} plugin for the Pulp Project",
long_description="{{ plugin_name | dash }} plugin for the Pulp Project",
long_description_content_type="text/markdown",
Expand Down
42 changes: 42 additions & 0 deletions templates/github/pyproject.toml.tool.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[tool.bumpversion]
# This section is managed by the plugin template. Do not edit manually.

current_version = "{{ 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]
# This section is managed by the plugin template. Do not edit manually.

optional_value = "prod"
values = [
"dev",
"prod",
]
{%- for plugin in plugins %}

[[tool.bumpversion.files]]
# This section is managed by the plugin template. Do not edit manually.

filename = "./{{ plugin.name }}/app/{% if plugin.name == "pulpcore" %}apps{% else %}__init__{% endif %}.py"
search = "version = \"{current_version}\""
replace = "version = \"{new_version}\""
{%- endfor %}
{%- if setup_py %}

[[tool.bumpversion.files]]
filename = "./setup.py"
{%- else %}

[[tool.bumpversion.files]]
# This section is managed by the plugin template. Do not edit manually.

filename = "./pyproject.toml"
search = "version = \"{current_version}\""
replace = "version = \"{new_version}\""
{%- endif %}
4 changes: 1 addition & 3 deletions test_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
-r requirements.txt
black
check-manifest
flake8
jinja2
mock
git+https://github.com/pulp/pulp-smash.git#egg=pulp-smash
pytest
pyyaml
requests_cache
21 changes: 20 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pathlib
import re
import requests_cache
import tomlkit
import tomllib
import yaml

Expand All @@ -21,7 +22,7 @@ def current_version(plugin_root_dir):
current_version = line[18:].strip()
break
except Exception:
current_version = "0.1.0a1.dev"
current_version = "0.0.0.dev"
return current_version


Expand Down Expand Up @@ -64,6 +65,24 @@ def to_snake(name):
return name.replace("-", "_")


def to_nice_yaml(data):
"""Implement a filter for Jinja 2 templates to render human readable YAML."""
return yaml.dump(data, indent=2, allow_unicode=True, default_flow_style=False)


def merge_toml(template, plugin_root_dir, relative_path, config):
basename, merge_key = relative_path.split(".toml.", maxsplit=1)
data = tomlkit.loads(template.render(**config))
if merge_key in data:
path = pathlib.Path(plugin_root_dir) / f"{basename}.toml"
old_toml = tomlkit.load(path.open())
if merge_key not in old_toml:
old_toml[merge_key] = data[merge_key]
else:
old_toml[merge_key].update(data[merge_key])
tomlkit.dump(old_toml, path.open("w"))


def get_pulpdocs_members() -> list[str]:
"""
Get repositories which are members of the Pulp managed documentation.
Expand Down

0 comments on commit 2d842c4

Please sign in to comment.