Skip to content

Commit

Permalink
Add option to edit/keep fragments while building
Browse files Browse the repository at this point in the history
  • Loading branch information
Shivansh-007 committed Nov 3, 2021
1 parent 6459a7a commit 384ea89
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 17 deletions.
33 changes: 19 additions & 14 deletions scripts/news/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@
"doesn't exist please create it and run this command again :) Happy change-logging!"
)


CONFIG = load_toml_config()
SECTIONS = [_type for _type, _ in CONFIG.get("types").items()]


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

if not content:
message_notes = "# ERROR: No content found previously"
message_notes = ["# ERROR: No content found previously"]
continue

message = "\n".join(
[line.rstrip() for line in content.split("\n") if not line.lstrip().startswith("#")]
line.rstrip() for line in content.split("\n") if not line.lstrip().startswith("#")
)

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


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

with open(news_path, mode="w") as filename:
filename.write(version_news)
with open(news_path, mode="w") as file:
file.write(version_news)

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

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

out("🍰 Cleared existing `scripts/news/next` news fragments!")
if not keep:
files = Path(Path.cwd(), "scripts/news/next")
for news_fragment in files.glob("*.md"):
os.remove(news_fragment)
out("🍰 Cleared existing `scripts/news/next` news fragments!")


if __name__ == "__main__":
Expand Down
5 changes: 2 additions & 3 deletions scripts/news/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ def handle_parse_result(
self, ctx: Context, opts: Mapping[str, Any], args: List[str]
) -> Tuple[Any, List[str]]:
"""Check if option is mutually exclusive with another, if yes print error and exist."""
we_are_present = self.name in opts
other_present = self.not_required_if in opts

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

metadata = {
return {
"date": date,
"gh_pr": gh_pr,
"news_type": news_type,
"nonce": nonce,
"news_entry": news_entry,
}
return metadata


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

0 comments on commit 384ea89

Please sign in to comment.