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 b110735
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 37 deletions.
33 changes: 19 additions & 14 deletions plugin-template
Original file line number Diff line number Diff line change
Expand Up @@ -308,8 +308,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 +377,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 +397,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 @@ -437,6 +432,8 @@ def write_template_section(config, name, plugin_root_dir, verbose=False):
continue
if config["use_issue_template"] is False and "ISSUE_TEMPLATE" in relative_path:
continue
if not setup_py and ".bumpversion.cfg" in relative_path:
continue
destination_relative_path = relative_path.replace(
"plugin_name", utils.to_snake(config["plugin_name"])
)
Expand All @@ -450,12 +447,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 "pyproject.toml" in relative_path:
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 +492,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
28 changes: 28 additions & 0 deletions templates/github/.bumpversion.cfg.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[bumpversion]
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}

[bumpversion:part:release]
optional_value = prod
first_value = dev
values =
dev
prod
{%- for plugin in plugins %}

[bumpversion:file:./{{ plugin.name | snake }}/app/{% if plugin.name == "pulpcore" %}apps{% else %}__init__{% endif %}.py]
{%- endfor %}
{%- if setup_py %}

[bumpversion:file:./setup.py]
{%- else %}

[bumpversion:file:./pyproject.toml]
search = version = "{current_version}"
replace = version = "{new_version}"
{%- endif %}
38 changes: 38 additions & 0 deletions templates/github/pyproject.toml.tool.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{%- if not setup_py %}
[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/__init__.py"
search = "version = \"{current_version}\""
replace = "version = \"{new_version}\""
{%- endfor %}

[[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 %}
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 b110735

Please sign in to comment.