Skip to content

Commit 7c638bf

Browse files
committed
build: add CLI option 'filename'
1 parent d4fdd1f commit 7c638bf

File tree

6 files changed

+62
-40
lines changed

6 files changed

+62
-40
lines changed

docs/cli.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ Build the combined news file from news fragments.
2525

2626
.. option:: --draft
2727

28-
Only render news fragments to standard output.
29-
Don't write to files, don't check versions.
30-
Only renders the news fragments **without** the surrounding template.
28+
Don't stage changes nor remove fragments.
29+
If option ``--filename`` is provided, write the news there;
30+
otherwise, render the fragments **without** the surrounding template to ``stdout``.
3131

3232
.. option:: --name NAME
3333

@@ -45,6 +45,11 @@ Build the combined news file from news fragments.
4545

4646
Default: today's date
4747

48+
.. option:: --filename FILENAME
49+
50+
Use `FILENAME` to override field ``filename`` from the configuration.
51+
If used together with ``--draft``, write to ``FILENAME`` instead of ``stdout``.
52+
4853
.. option:: --yes
4954

5055
Do not ask for confirmations.

docs/configuration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Top level keys
1717
~~~~~~~~~~~~~~
1818

1919
- ``directory`` -- If you are not storing your news fragments in your Python package, or aren't using Python, this is the path to where your newsfragments will be put.
20-
- ``filename`` -- The filename of your news file.
20+
- ``filename`` -- The filename (or pattern) of your news file.
2121
``NEWS.rst`` by default.
2222
- ``package`` -- The package name of your project.
2323
(Python projects only)

docs/tutorial.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ You should get an output similar to this::
114114
Loading template...
115115
Finding news fragments...
116116
Rendering news fragments...
117-
Draft only -- nothing has been written.
117+
Print draft to stdout only -- nothing has been written.
118118
What is seen below is what would be written.
119119

120120
myproject 1.0.2 (2015-12-27)

src/towncrier/build.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ def _validate_answer(ctx: Context, param: Option, value: bool) -> bool:
8484
default=None,
8585
help="Render the news fragments using the given date.",
8686
)
87+
@click.option(
88+
"--filename",
89+
"filename",
90+
default=None,
91+
help="Write the output to the given filename pattern.",
92+
)
8793
@click.option(
8894
"--yes",
8995
"answer_yes",
@@ -107,6 +113,7 @@ def _main(
107113
project_name: str | None,
108114
project_version: str | None,
109115
project_date: str | None,
116+
filename: str | None,
110117
answer_yes: bool,
111118
answer_keep: bool,
112119
) -> None:
@@ -115,14 +122,15 @@ def _main(
115122
"""
116123
try:
117124
return __main(
118-
draft,
119-
directory,
120-
config_file,
121-
project_name,
122-
project_version,
123-
project_date,
124-
answer_yes,
125-
answer_keep,
125+
draft=draft,
126+
directory=directory,
127+
config_file=config_file,
128+
project_name=project_name,
129+
project_version=project_version,
130+
project_date=project_date,
131+
filename=filename,
132+
answer_yes=answer_yes,
133+
answer_keep=answer_keep,
126134
)
127135
except ConfigError as e:
128136
print(e, file=sys.stderr)
@@ -136,6 +144,7 @@ def __main(
136144
project_name: str | None,
137145
project_version: str | None,
138146
project_date: str | None,
147+
filename: str | None,
139148
answer_yes: bool,
140149
answer_keep: bool,
141150
) -> None:
@@ -238,44 +247,51 @@ def __main(
238247
else:
239248
content = rendered
240249

241-
if draft:
250+
if draft and filename is None:
242251
click.echo(
243-
"Draft only -- nothing has been written.\n"
252+
"Print draft to stdout only -- nothing has been written.\n"
244253
"What is seen below is what would be written.\n",
245254
err=to_err,
246255
)
247256
click.echo(content)
248257
return
249258

250259
click.echo("Writing to newsfile...", err=to_err)
251-
news_file = config.filename
252260

253-
if config.single_file is False:
261+
if filename is None:
262+
filename = config.filename
263+
264+
single_file = config.single_file
265+
266+
if not single_file:
254267
# The release notes for each version are stored in a separate file.
255268
# The name of that file is generated based on the current version and project.
256-
news_file = news_file.format(
257-
name=project_name, version=project_version, project_date=project_date
269+
filename = filename.format(
270+
name=project_name,
271+
version=project_version,
272+
project_date=project_date,
258273
)
259274

260275
append_to_newsfile(
261276
base_directory,
262-
news_file,
277+
filename,
263278
config.start_string,
264279
top_line,
265280
content,
266-
single_file=config.single_file,
281+
single_file=single_file,
267282
)
268283

269-
click.echo("Staging newsfile...", err=to_err)
270-
_git.stage_newsfile(base_directory, news_file)
284+
if not draft:
285+
click.echo("Staging newsfile...", err=to_err)
286+
_git.stage_newsfile(base_directory, filename)
271287

272-
if should_remove_fragment_files(
273-
fragment_filenames,
274-
answer_yes,
275-
answer_keep,
276-
):
277-
click.echo("Removing news fragments...", err=to_err)
278-
_git.remove_files(fragment_filenames)
288+
if should_remove_fragment_files(
289+
fragment_filenames,
290+
answer_yes,
291+
answer_keep,
292+
):
293+
click.echo("Removing news fragments...", err=to_err)
294+
_git.remove_files(fragment_filenames)
279295

280296
click.echo("Done!", err=to_err)
281297

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add CLI option ``--filename FILENAME``.

src/towncrier/test/test_build.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def _test_command(self, command):
6060
Loading template...
6161
Finding news fragments...
6262
Rendering news fragments...
63-
Draft only -- nothing has been written.
63+
Print draft to stdout only -- nothing has been written.
6464
What is seen below is what would be written.
6565
6666
Foo 1.2.3 (01-01-2001)
@@ -273,7 +273,7 @@ def run_order_scenario(sections, types):
273273
self.assertEqual(
274274
result.output,
275275
"Loading template...\nFinding news fragments...\nRendering news "
276-
"fragments...\nDraft only -- nothing has been written.\nWhat is "
276+
"fragments...\nPrint draft to stdout only -- nothing has been written.\nWhat is "
277277
"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
278278
"\n======================"
279279
+ dedent(
@@ -316,7 +316,7 @@ def run_order_scenario(sections, types):
316316
self.assertEqual(
317317
result.output,
318318
"Loading template...\nFinding news fragments...\nRendering news "
319-
"fragments...\nDraft only -- nothing has been written.\nWhat is "
319+
"fragments...\nPrint draft to stdout only -- nothing has been written.\nWhat is "
320320
"seen below is what would be written.\n\nFoo 1.2.3 (01-01-2001)"
321321
"\n======================"
322322
+ dedent(
@@ -551,7 +551,7 @@ def test_projectless_changelog(self):
551551
Loading template...
552552
Finding news fragments...
553553
Rendering news fragments...
554-
Draft only -- nothing has been written.
554+
Print draft to stdout only -- nothing has been written.
555555
What is seen below is what would be written.
556556
557557
FooBarBaz 7.8.9 (01-01-2001)
@@ -592,7 +592,7 @@ def test_version_in_config(self):
592592
Loading template...
593593
Finding news fragments...
594594
Rendering news fragments...
595-
Draft only -- nothing has been written.
595+
Print draft to stdout only -- nothing has been written.
596596
What is seen below is what would be written.
597597
598598
7.8.9 (01-01-2001)
@@ -634,7 +634,7 @@ def test_project_name_in_config(self):
634634
Loading template...
635635
Finding news fragments...
636636
Rendering news fragments...
637-
Draft only -- nothing has been written.
637+
Print draft to stdout only -- nothing has been written.
638638
What is seen below is what would be written.
639639
640640
ImGoProject 7.8.9 (01-01-2001)
@@ -679,7 +679,7 @@ def test_no_package_changelog(self):
679679
Loading template...
680680
Finding news fragments...
681681
Rendering news fragments...
682-
Draft only -- nothing has been written.
682+
Print draft to stdout only -- nothing has been written.
683683
What is seen below is what would be written.
684684
685685
7.8.9 (01-01-2001)
@@ -1010,7 +1010,7 @@ def test_title_format_custom(self):
10101010
Loading template...
10111011
Finding news fragments...
10121012
Rendering news fragments...
1013-
Draft only -- nothing has been written.
1013+
Print draft to stdout only -- nothing has been written.
10141014
What is seen below is what would be written.
10151015
10161016
[20-01-2001] CUSTOM RELEASE for FooBarBaz version 7.8.9
@@ -1087,7 +1087,7 @@ def test_title_format_false(self):
10871087
Loading template...
10881088
Finding news fragments...
10891089
Rendering news fragments...
1090-
Draft only -- nothing has been written.
1090+
Print draft to stdout only -- nothing has been written.
10911091
What is seen below is what would be written.
10921092
10931093
Here's a hardcoded title added by the template
@@ -1220,7 +1220,7 @@ def test_with_topline_and_template_and_draft(self):
12201220
Loading template...
12211221
Finding news fragments...
12221222
Rendering news fragments...
1223-
Draft only -- nothing has been written.
1223+
Print draft to stdout only -- nothing has been written.
12241224
What is seen below is what would be written.
12251225
12261226
7.8.9 - 20-01-2001

0 commit comments

Comments
 (0)