Skip to content

Commit

Permalink
#210: Add 'show' command
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Jan 10, 2025
1 parent ebb3908 commit 3510a6f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## Unreleased

* Added:
* CLI: `show` command to print the version without changing any files.

## v1.5.2 (2025-01-09)

* Fixed:
Expand Down
2 changes: 2 additions & 0 deletions poetry_dynamic_versioning/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def main() -> None:
cli.apply(standalone=True)
elif args.cmd == cli.Command.enable:
cli.enable()
elif args.cmd == cli.Command.show:
cli.show()
except Exception as e:
print("Error: {}".format(e), file=sys.stderr)
sys.exit(1)
18 changes: 18 additions & 0 deletions poetry_dynamic_versioning/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@

from poetry_dynamic_versioning import (
_get_and_apply_version,
_get_config,
_get_pyproject_path,
_get_version,
_state,
_validate_config,
)
Expand All @@ -34,7 +36,9 @@ class Key:
class Command:
dv = "dynamic-versioning"
enable = "enable"
show = "show"
dv_enable = "{} {}".format(dv, enable)
dv_show = "{} {}".format(dv, show)


class Help:
Expand All @@ -47,13 +51,15 @@ class Help:
"Update pyproject.toml to enable the plugin using a typical configuration."
" The output may not be suitable for more complex use cases."
)
show = "Print the version without changing any files."


def get_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(description=Help.main)

subparsers = parser.add_subparsers(dest="cmd", title="subcommands")
subparsers.add_parser(Command.enable, help=Help.enable)
subparsers.add_parser(Command.show, help=Help.show)

return parser

Expand Down Expand Up @@ -140,3 +146,15 @@ def _enable_in_doc(doc: tomlkit.TOMLDocument) -> tomlkit.TOMLDocument:
doc[Key.tool][Key.poetry][Key.version] = "0.0.0"

return doc


def show() -> None:
pyproject_path = _get_pyproject_path()
if pyproject_path is None:
raise RuntimeError("Unable to find pyproject.toml")

pyproject = tomlkit.parse(pyproject_path.read_bytes().decode("utf-8"))
config = _get_config(pyproject)
version = _get_version(config)

print(version[0])
19 changes: 18 additions & 1 deletion poetry_dynamic_versioning/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _should_apply(command: str) -> bool:
if override is not None:
return command in override.split(",")
else:
return command not in ["run", "shell", cli.Command.dv, cli.Command.dv_enable]
return command not in ["run", "shell", cli.Command.dv, cli.Command.dv_enable, cli.Command.dv_show]


def _should_apply_with_io(command: str) -> bool:
Expand Down Expand Up @@ -127,6 +127,20 @@ def handle(self) -> int:
return 0


class DynamicVersioningShowCommand(Command):
name = cli.Command.dv_show
description = cli.Help.show

def __init__(self, application: Application):
super().__init__()
self._application = application

def handle(self) -> int:
_state.cli_mode = True
cli.show()
return 0


class DynamicVersioningPlugin(ApplicationPlugin):
def __init__(self):
self._application = None
Expand All @@ -138,6 +152,9 @@ def activate(self, application: Application) -> None:
application.command_loader.register_factory(
cli.Command.dv_enable, lambda: DynamicVersioningEnableCommand(application)
)
application.command_loader.register_factory(
cli.Command.dv_show, lambda: DynamicVersioningShowCommand(application)
)

try:
local = self._application.poetry.pyproject.data
Expand Down

0 comments on commit 3510a6f

Please sign in to comment.