Skip to content

Commit

Permalink
Add support for custom template versions for dependency update and sync
Browse files Browse the repository at this point in the history
We make use of the native support for specifying a template version in
Cruft to pass through a custom version in dependency update and sync.

We introduce flag `--template-version` for the component and package
`update` and `sync` commands.

Resolves #618
  • Loading branch information
simu committed Dec 30, 2024
1 parent 0d03e44 commit 14b0a66
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 13 deletions.
14 changes: 8 additions & 6 deletions commodore/cli/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,7 @@ def component_group(config: Config, verbose):
show_default=True,
help="The URL of the component cookiecutter template.",
)
@click.option(
"--template-version",
default="main",
show_default=True,
help="The component template version (Git tree-ish) to use.",
)
@options.template_version("main")
@new_update_options(new_cmd=True)
@options.verbosity
@options.pass_config
Expand Down Expand Up @@ -430,13 +425,15 @@ def component_new(
default=True,
help="Whether to commit the rendered template changes.",
)
@options.template_version(None)
@options.verbosity
@options.pass_config
def component_update(
config: Config,
verbose: int,
component_path: str,
copyright_holder: str,
template_version: Optional[str],
golden_tests: Optional[bool],
matrix_tests: Optional[bool],
lib: Optional[bool],
Expand Down Expand Up @@ -492,6 +489,8 @@ def component_update(
t.automerge_patch_v0 = automerge_patch_v0
if autorelease is not None:
t.autorelease = autorelease
if template_version is not None:
t.template_version = template_version

test_cases = t.test_cases
test_cases.extend(additional_test_case)
Expand Down Expand Up @@ -625,6 +624,7 @@ def component_compile(
@options.pr_batch_size
@options.github_pause
@options.dependency_filter
@options.template_version(None)
def component_sync(
config: Config,
verbose: int,
Expand All @@ -636,6 +636,7 @@ def component_sync(
pr_batch_size: int,
github_pause: int,
filter: str,
template_version: Optional[str],
):
"""This command processes all components listed in the provided `COMPONENT_LIST`
YAML file.
Expand Down Expand Up @@ -672,4 +673,5 @@ def component_sync(
pr_batch_size,
timedelta(seconds=github_pause),
filter,
template_version,
)
18 changes: 18 additions & 0 deletions commodore/cli/options.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
"""Click options which are reused for multiple commands"""

from typing import Optional

import click

from commodore.config import Config
Expand Down Expand Up @@ -125,6 +127,22 @@
)


def template_version(default: Optional[str]):
help_str = "The component template version (Git tree-ish) to use."
if default is None:
help_str = (
help_str
+ " If not provided, the currently active template version will be used."
)

return click.option(
"--template-version",
default=default,
show_default=default is not None,
help=help_str,
)


def local(help: str):
return click.option(
"--local",
Expand Down
15 changes: 9 additions & 6 deletions commodore/cli/package.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ def package_group(config: Config, verbose: int):
show_default=True,
help="The URL of the package cookiecutter template.",
)
@click.option(
"--template-version",
default="main",
show_default=True,
help="The package template version (Git tree-ish) to use.",
)
@options.template_version("main")
@click.option(
"--output-dir",
default="",
Expand Down Expand Up @@ -167,6 +162,7 @@ def package_new(
default=True,
help="Whether to commit the rendered template changes.",
)
@options.template_version(None)
@options.verbosity
@options.pass_config
# pylint: disable=too-many-arguments
Expand All @@ -180,6 +176,7 @@ def package_update(
additional_test_case: Iterable[str],
remove_test_case: Iterable[str],
commit: bool,
template_version: Optional[str],
):
"""This command updates the package at PACKAGE_PATH to the latest version of the
template which was originally used to create it, if the template version is given as
Expand All @@ -201,6 +198,9 @@ def package_update(
t.golden_tests = golden_tests
if update_copyright_year:
t.copyright_year = None
if template_version is not None:
t.template_version = template_version

test_cases = t.test_cases
test_cases.extend(additional_test_case)
t.test_cases = [tc for tc in test_cases if tc not in remove_test_case]
Expand Down Expand Up @@ -278,6 +278,7 @@ def package_compile(
@options.pr_batch_size
@options.github_pause
@options.dependency_filter
@options.template_version(None)
def package_sync(
config: Config,
verbose: int,
Expand All @@ -289,6 +290,7 @@ def package_sync(
pr_batch_size: int,
github_pause: int,
filter: str,
template_version: Optional[str],
):
"""This command processes all packages listed in the provided `PACKAGE_LIST` YAML file.
Expand Down Expand Up @@ -324,4 +326,5 @@ def package_sync(
pr_batch_size,
timedelta(seconds=github_pause),
filter,
template_version,
)
12 changes: 11 additions & 1 deletion commodore/dependency_syncer.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from collections.abc import Iterable
from datetime import timedelta
from pathlib import Path
from typing import Union, Type
from typing import Optional, Union, Type

import click
import git
Expand Down Expand Up @@ -36,10 +36,18 @@ def sync_dependencies(
pr_batch_size: int = 10,
pause: timedelta = timedelta(seconds=120),
depfilter: str = "",
template_version: Optional[str] = None,
) -> None:
if not config.github_token:
raise click.ClickException("Can't continue, missing GitHub API token.")

if template_version is not None and not dry_run:
click.secho(
" > Custom template version provided for sync, but dry run not active. Forcing dry run",
fg="yellow",
)
dry_run = True

deptype_str = deptype.__name__.lower()

deps = read_dependency_list(dependency_list, depfilter)
Expand Down Expand Up @@ -72,6 +80,8 @@ def sync_dependencies(

# Update the dependency
t = templater.from_existing(config, d.target_dir)
if template_version is not None:
t.template_version = template_version
changed = t.update(
print_completion_message=False,
commit=not dry_run,
Expand Down

0 comments on commit 14b0a66

Please sign in to comment.