Skip to content

Commit 384ea89

Browse files
committed
Add option to edit/keep fragments while building
1 parent 6459a7a commit 384ea89

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

scripts/news/__main__.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@
4040
"doesn't exist please create it and run this command again :) Happy change-logging!"
4141
)
4242

43-
4443
CONFIG = load_toml_config()
4544
SECTIONS = [_type for _type, _ in CONFIG.get("types").items()]
4645

4746

4847
def save_news_fragment(ctx: click.Context, gh_pr: int, nonce: str, news_entry: str, news_type: str) -> None:
4948
"""Save received changelog data to a news file."""
5049
date = datetime.now(timezone.utc).strftime("%Y-%m-%d")
51-
path = Path(
52-
Path.cwd(),
53-
f"news/next/{news_type}/{date}.pr-{str(gh_pr)}.{nonce}.md",
54-
)
50+
path = Path(Path.cwd(), f"news/next/{news_type}/{date}.pr-{gh_pr}.{nonce}.md")
5551
if not path.parents[1].exists():
5652
err(NO_NEWS_PATH_ERROR, fg="blue")
5753
ctx.exit(1)
@@ -158,11 +154,11 @@ def cli_add_news(ctx: click.Context, message: str, editor: str, type: str, pr_nu
158154
)
159155

160156
if not content:
161-
message_notes = "# ERROR: No content found previously"
157+
message_notes = ["# ERROR: No content found previously"]
162158
continue
163159

164160
message = "\n".join(
165-
[line.rstrip() for line in content.split("\n") if not line.lstrip().startswith("#")]
161+
line.rstrip() for line in content.split("\n") if not line.lstrip().startswith("#")
166162
)
167163

168164
if message is None:
@@ -175,8 +171,14 @@ def cli_add_news(ctx: click.Context, message: str, editor: str, type: str, pr_nu
175171

176172

177173
@cli_main.command("build")
174+
@click.option(
175+
"--edit/--no-edit",
176+
default=None,
177+
help="Open the changelog file in your text editor.",
178+
)
179+
@click.option("--keep", is_flag=True, help="Keep the fragment files that are collected.")
178180
@click.pass_context
179-
def cli_build_news(ctx: click.Context) -> None:
181+
def cli_build_news(ctx: click.Context, edit: Optional[bool], keep: bool) -> None:
180182
"""Build a combined news file 📜 from news fragments."""
181183
filenames = glob_fragments("next", SECTIONS)
182184
_file_metadata = {}
@@ -222,16 +224,19 @@ def cli_build_news(ctx: click.Context) -> None:
222224
)
223225
news_path = Path(Path.cwd(), f"news/{version}.md")
224226

225-
with open(news_path, mode="w") as filename:
226-
filename.write(version_news)
227+
with open(news_path, mode="w") as file:
228+
file.write(version_news)
227229

228230
out(f"All done! ✨ 🍰 ✨ Created {name}-v{version} news at {news_path}")
229231

230-
files = Path(Path.cwd(), "scripts/news/next")
231-
for news_fragment in files.glob("*.md"):
232-
os.remove(news_fragment)
232+
if edit:
233+
click.edit(filename=str(news_path))
233234

234-
out("🍰 Cleared existing `scripts/news/next` news fragments!")
235+
if not keep:
236+
files = Path(Path.cwd(), "scripts/news/next")
237+
for news_fragment in files.glob("*.md"):
238+
os.remove(news_fragment)
239+
out("🍰 Cleared existing `scripts/news/next` news fragments!")
235240

236241

237242
if __name__ == "__main__":

scripts/news/utils.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def handle_parse_result(
7070
self, ctx: Context, opts: Mapping[str, Any], args: List[str]
7171
) -> Tuple[Any, List[str]]:
7272
"""Check if option is mutually exclusive with another, if yes print error and exist."""
73-
we_are_present = self.name in opts
7473
other_present = self.not_required_if in opts
7574

7675
if other_present:
76+
we_are_present = self.name in opts
7777
if we_are_present:
7878
err(
7979
f"{ERROR_MSG_PREFIX} Illegal usage. `%s` is mutually exclusive with `%s`"
@@ -121,14 +121,13 @@ def get_metadata_from_file(path: Path) -> dict:
121121
with open(path, "r", encoding="utf-8") as file:
122122
news_entry = file.read()
123123

124-
metadata = {
124+
return {
125125
"date": date,
126126
"gh_pr": gh_pr,
127127
"news_type": news_type,
128128
"nonce": nonce,
129129
"news_entry": news_entry,
130130
}
131-
return metadata
132131

133132

134133
def get_project_meta() -> Tuple[str, str]:

0 commit comments

Comments
 (0)