Skip to content

Commit

Permalink
feat: allow template directories to contain a '.' at the top-level (#762
Browse files Browse the repository at this point in the history
)
  • Loading branch information
bernardcooke53 committed Dec 7, 2023
1 parent aa9b33b commit 07b232a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ If ``--no-commit`` is supplied, it may disable other options derivatively; pleas
************************

Whether or not to perform a ``git tag`` to apply a tag of the corresponding to the new version during this
command invocation. This option manages the tag application separate from the commit handled by the `--commit`
command invocation. This option manages the tag application separate from the commit handled by the ``--commit``
option.

If ``--no-tag`` is supplied, it may disable other options derivatively; please see below.
Expand Down
22 changes: 14 additions & 8 deletions semantic_release/changelog/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,28 @@ def recursive_render(
) -> list[str]:
rendered_paths: list[str] = []
for root, file in (
(root, file)
(Path(root), file)
for root, _, files in os.walk(template_dir)
for file in files
if not any(elem.startswith(".") for elem in root.split(os.sep))
# we slice relpath.parts[1:] to allow the top-level
# template folder to have a dot prefix.
# e.g. to permit ".github/psr-templates" to contain the templates,
# rather than enforcing a top-level, non-hidden directory
if not any(
elem.startswith(".")
for elem in Path(root).relative_to(template_dir).parts[1:]
)
and not file.startswith(".")
):
src_path = Path(root)
output_path = (_root_dir / src_path.relative_to(template_dir)).resolve()
log.info("Rendering templates from %s to %s", src_path, output_path)
os.makedirs(str(output_path), exist_ok=True)
output_path = (_root_dir / root.relative_to(template_dir)).resolve()
log.info("Rendering templates from %s to %s", root, output_path)
output_path.mkdir(parents=True, exist_ok=True)
if file.endswith(".j2"):
# We know the file ends with .j2 by the filter in the for-loop
output_filename = file[:-3]
# Strip off the template directory from the front of the root path -
# that's the output location relative to the repo root
src_file_path = str((src_path / file).relative_to(template_dir))
src_file_path = str((root / file).relative_to(template_dir))
output_file_path = str((output_path / output_filename).resolve())

log.debug("rendering %s to %s", src_file_path, output_file_path)
Expand All @@ -107,7 +113,7 @@ def recursive_render(

rendered_paths.append(output_file_path)
else:
src_file = str((src_path / file).resolve())
src_file = str((root / file).resolve())
target_file = str((output_path / file).resolve())
log.debug(
"source file %s is not a template, copying to %s", src_file, target_file
Expand Down
14 changes: 6 additions & 8 deletions semantic_release/cli/commands/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,13 @@ def version( # noqa: C901
translator.prerelease_token = prerelease_token

# Only push if we're committing changes
if push_changes and not commit_changes:
if not create_tag:
log.info("changes will not be pushed because --no-commit disables pushing")
push_changes &= commit_changes
if push_changes and not commit_changes and not create_tag:
log.info("changes will not be pushed because --no-commit disables pushing")
push_changes &= commit_changes
# Only push if we're creating a tag
if push_changes and not create_tag:
if not commit_changes:
log.info("new tag will not be pushed because --no-tag disables pushing")
push_changes &= create_tag
if push_changes and not create_tag and not commit_changes:
log.info("new tag will not be pushed because --no-tag disables pushing")
push_changes &= create_tag
# Only make a release if we're pushing the changes
if make_vcs_release and not push_changes:
log.info("No vcs release will be created because pushing changes is disabled")
Expand Down
36 changes: 35 additions & 1 deletion tests/scenario/test_template_render.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def test_recursive_render(
preexisting_paths = set(example_project.rglob("**/*"))

recursive_render(
template_dir=str(tmpl_dir),
template_dir=example_project_template_dir.resolve(),
environment=env,
_root_dir=str(example_project.resolve()),
)
Expand Down Expand Up @@ -128,3 +128,37 @@ def test_recursive_render(
t.relative_to(example_project).parts, func=lambda *a: os.sep.join(a)
)
)


@pytest.fixture
def dotfolder_template_dir(example_project: Path):
newpath = example_project / ".templates/.psr-templates"
newpath.mkdir(parents=True, exist_ok=True)
return newpath


@pytest.fixture
def dotfolder_template(dotfolder_template_dir: Path):
tmpl = dotfolder_template_dir / "template.txt"
tmpl.write_text("I am a template")
return tmpl


def test_recursive_render_with_top_level_dotfolder(
example_project, dotfolder_template, dotfolder_template_dir
):
preexisting_paths = set(example_project.rglob("**/*"))
env = environment(template_dir=dotfolder_template_dir.resolve())

recursive_render(
template_dir=dotfolder_template_dir.resolve(),
environment=env,
_root_dir=example_project.resolve(),
)

rendered_template = example_project / dotfolder_template.name
assert rendered_template.exists()

assert set(example_project.rglob("**/*")) == preexisting_paths.union(
{example_project / rendered_template}
)

0 comments on commit 07b232a

Please sign in to comment.