Skip to content

Commit

Permalink
add --dry-run option to print latest snippet content instead of updat…
Browse files Browse the repository at this point in the history
…ing the changelog, closes #27
  • Loading branch information
brainelectronics committed Oct 12, 2024
1 parent e56b641 commit ab2bcfc
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 8 additions & 0 deletions .snippets/27.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## Dry run option to print latest changelog entry
<!--
type: feature
scope: all
affected: all
-->

Use `--dry-run` with the `changelog` subparser to print the latest changelog entry as JSON instead of updating the changelog file.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,27 @@ version entries and version bumps multiple times otherwise.*
changelog-generator changelog changelog.md --snippets=.snippets [--in-place]
```

To just get the latest changelog entry without updating or generating the
changelog, use `--dry-run` to print the latest snippet content in JSON format.

```json
{
"version": "1.5.0",
"timestamp": "2024-10-12T13:36:46+02:00",
"meta": {
"type": "feature",
"scope": [
"all"
],
"affected": [
"all"
]
},
"content": "\n\nUse `--dry-run` with the `changelog` subparser to print the latest changelog entry as JSON instead of updating the changelog file.\n",
"version_reference": "https://github.com/brainelectronics/snippets2changelog/tree/1.5.0"
}
```

### Parse

Parse an existing snippet file and return the data as JSON without indentation
Expand Down
7 changes: 6 additions & 1 deletion snippets2changelog/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def parse_args(argv: Union[Sequence[str], None] = None) -> Args:
action='store_true',
help="Skip snippets with scope set as 'internal'",
)
parser_changelog.add_argument(
"--dry-run",
action='store_true',
help="Print latest changelog entry as JSON instead of updating the changelog file",
)

parser_create = subparsers.add_parser(
"create",
Expand Down Expand Up @@ -125,7 +130,7 @@ def fn_info(_args: Args) -> None:

def fn_changelog(args: Args) -> None:
cc = ChangelogCreator(changelog=args.changelog, snippets_folder=args.snippets, update_in_place=args.in_place, skip_internal=args.no_internal, verbosity=args.verbose)
cc.update_changelog()
cc.update_changelog(dry_run=args.dry_run)


def fn_create(args: Args) -> None:
Expand Down
11 changes: 10 additions & 1 deletion snippets2changelog/creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""Snippets generator"""

import json
import logging
from pathlib import Path
from typing import Dict, Iterator
Expand Down Expand Up @@ -89,11 +90,13 @@ def __init__(self, changelog: Path, snippets_folder: Path, update_in_place: boo

self._skip_internal = skip_internal

def update_changelog(self) -> None:
def update_changelog(self, dry_run: bool = False) -> None:
new_changelog_content = ""
# create a "prolog" and an "epilog", with the new content in between
existing_changelog_content = read_file(path=self._changelog, parse="read").split(self._version_line)

latest_changelog_entry = {}

for commit, file_name in self.snippets():
self._logger.debug(f"Parsing {file_name}")
self.parse(file_name=file_name)
Expand Down Expand Up @@ -123,12 +126,18 @@ def update_changelog(self) -> None:
"content": snippet_content["details"],
"version_reference": f"https://github.com/brainelectronics/snippets2changelog/tree/{self.semver_data}",
}
latest_changelog_entry = changelog_entry_content
self._logger.debug(f"changelog_entry_content: {changelog_entry_content}")

changelog_entry = self._env.get_template("changelog_part.md.template").render(changelog_entry_content)
self._logger.debug(f"rendered changelog_entry: \n{changelog_entry}")
new_changelog_content = changelog_entry + new_changelog_content

if dry_run:
latest_changelog_entry['version'] = str(latest_changelog_entry['version'])
print(json.dumps(latest_changelog_entry))
return

rendered_changelog = self._env.get_template("changelog.md.template").render({"prolog": existing_changelog_content[0], "new": new_changelog_content, "existing": self._version_line + existing_changelog_content[1]})
rendered_changelog_path = Path(f"{self._changelog}")
if not self._update_in_place:
Expand Down

0 comments on commit ab2bcfc

Please sign in to comment.