Skip to content

Commit 3425cd0

Browse files
committed
Allow a single file to be expanded into a directory
This allows a file whose name is templated to be expanded into a directory under its expanded name, so we can find out what that name is, even when we are expanding just that one file.
1 parent 01cd305 commit 3425cd0

5 files changed

Lines changed: 38 additions & 11 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ Nancy then processes each file:
9999
or directory in the output, the name in the input tree is expanded, and
100100
any `.nancy` suffix is removed. There is one exception: the root directory
101101
(or file) is called `OUTPUT` (that is, the `OUTPUT` argument to Nancy).
102+
However, if `OUTPUT` already exists and is a directory, and the root is a
103+
file, then the output is written with its expanded file name to the
104+
directory `OUTPUT`.
102105

103106
Input files, which are not copied to the output in any form, can be used by
104107
commands in other files. They can also be used for documentation or other

README.nancy.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Nancy then processes each file:
7575
or directory in the output, the name in the input tree is expanded, and
7676
any `.nancy` suffix is removed. There is one exception: the root directory
7777
(or file) is called `OUTPUT` (that is, the `OUTPUT` argument to Nancy).
78+
However, if `OUTPUT` already exists and is a directory, and the root is a
79+
file, then the output is written with its expanded file name to the
80+
directory `OUTPUT`.
7881

7982
Input files, which are not copied to the output in any form, can be used by
8083
commands in other files. They can also be used for documentation or other

nancy/__init__.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -348,19 +348,29 @@ def __init__(
348348

349349
async def set_output_path(self):
350350
"""Recompute `_output_path` by expanding `path`."""
351-
output_path = self.path.relative_to(self.tree.build)
352-
if output_path.name != "":
353-
if re.search(COPY_REGEX, output_path.name):
354-
output_path = output_path.with_name(
355-
output_path.name.replace(".copy", "", 1)
356-
)
351+
# Compute expanded filename
352+
if self.path.name != "":
353+
if re.search(COPY_REGEX, self.path.name):
354+
final_path = self.path.with_name(self.path.name.replace(".copy", "", 1))
357355
else:
358-
output_path = output_path.with_name(
359-
re.sub(TEMPLATE_REGEX, "", output_path.name)
356+
final_path = self.path.with_name(
357+
re.sub(TEMPLATE_REGEX, "", self.path.name)
360358
)
361359
# Discard computed inputs when expanding filenames.
362-
output, _ = await self.expand(bytes(output_path))
363-
output_path = Path(os.fsdecode(output))
360+
expanded_final_path, _ = await self.expand(bytes(final_path))
361+
expanded_final_path = Path(os.fsdecode(expanded_final_path))
362+
else:
363+
expanded_final_path = Path("")
364+
365+
output_path = self.path.relative_to(self.tree.build)
366+
if output_path.name != "":
367+
output_path = output_path.with_name(expanded_final_path.name)
368+
elif (
369+
self.tree.output.exists()
370+
and self.tree.output.is_dir()
371+
and self.input_file().is_file()
372+
):
373+
output_path = Path(expanded_final_path.name)
364374
self._output_path = output_path
365375

366376
def input_file(self):

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "nancy"
3-
version = "13.0.2"
3+
version = "13.1.0"
44
description = "Simple templating system"
55
license = "GPL-3.0-or-later"
66
authors = [

tests/test_nancy.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,17 @@ async def test_expanding_macros_in_file_names() -> None:
401401
)
402402

403403

404+
async def test_expanding_macro_in_single_file_argument_with_directory_output() -> None:
405+
with TemporaryDirectory() as tmp_dir:
406+
with chdir(tests_dir):
407+
await passing_test(
408+
"expanding-macros-in-file-names-src",
409+
"expanding-macros-in-file-names-expected",
410+
"$include(name.in.txt).nancy.txt",
411+
tmp_dir,
412+
)
413+
414+
404415
async def test_run_with_input() -> None:
405416
with chdir(tests_dir):
406417
await passing_test(

0 commit comments

Comments
 (0)