diff --git a/.gitignore b/.gitignore index 2c0ea2b..abdff60 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ __pycache__/ .env/ .idea/ .mypy_cache/ +.poetry/ .pytest_cache/ .ruff_cache/ .tox/ diff --git a/CHANGELOG.md b/CHANGELOG.md index db8fcdb..3d087ef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,16 @@ +## Unreleased + +* Added: + * CLI: When pyproject.toml contains the `project` section, + the `enable` command will add the required fields for Poetry 2.0.0+. +* Fixed: + * For compatibility with Poetry 2.0.0, + adjusted Poetry constraint from `^1.2.0` to `>=1.2.0`. + * For compatibility with Poetry 2.0.0, + when inserting the dynamic version into pyproject.toml with PEP 621 mode activated, + the plugin ensures not to set `project.version` and `tool.poetry.version` at the same time. + * An `UnboundLocalError` could happen when pyproject.toml was misconfigured. + ## v1.4.1 (2024-09-10) * Fixed: diff --git a/README.md b/README.md index fa270af..5243713 100644 --- a/README.md +++ b/README.md @@ -31,16 +31,28 @@ be sure to uninstall it before proceeding. * Install the plugin using one of the options below: - * In most cases: `poetry self add "poetry-dynamic-versioning[plugin]"` - * If you installed Poetry with Pipx: `pipx inject poetry "poetry-dynamic-versioning[plugin]"` + * For Poetry 2.0.0+, add this to your pyproject.toml and then run `poetry install`: + ```toml + [tool.poetry.requires-plugins] + poetry-dynamic-versioning = { version = ">=1.0.0,<2.0.0", extras = ["plugin"] } + ``` + * For older Poetry versions, depending on how you installed Poetry: + * In most cases: `poetry self add "poetry-dynamic-versioning[plugin]"` + * If you installed Poetry with Pipx: `pipx inject poetry "poetry-dynamic-versioning[plugin]"` See the [Poetry plugin documentation](https://python-poetry.org/docs/plugins/#using-plugins) for more information about these options. -* Run in your project: `poetry dynamic-versioning enable` +* Run this in your project: `poetry dynamic-versioning enable` - Or you can update your pyproject.toml manually: + Or you can update your pyproject.toml manually to include these fields: ```toml + [project] + dynamic = ["version"] + + [tool.poetry] + version = "0.0.0" + [tool.poetry-dynamic-versioning] enable = true ``` diff --git a/poetry_dynamic_versioning/__init__.py b/poetry_dynamic_versioning/__init__.py index 5c05cb3..6537365 100644 --- a/poetry_dynamic_versioning/__init__.py +++ b/poetry_dynamic_versioning/__init__.py @@ -595,6 +595,7 @@ def _apply_version( elif mode == _Mode.Pep621: pyproject["project"]["dynamic"].remove("version") # type: ignore pyproject["project"]["version"] = version # type: ignore + pyproject["tool"]["poetry"].pop("version") # type: ignore # Disable the plugin in case we're building a source distribution, # which won't have access to the VCS info at install time. @@ -654,6 +655,9 @@ def _get_and_apply_version( and "dynamic" in pyproject["project"] and "version" in pyproject["project"]["dynamic"] and "version" not in pyproject["project"] + and "tool" in pyproject + and "poetry" in pyproject["tool"] + and "version" in pyproject["tool"]["poetry"] ) if classic: @@ -662,10 +666,10 @@ def _get_and_apply_version( dynamic_index = None elif pep621: name = pyproject["project"]["name"] - original = pyproject["project"].get("version") + original = pyproject["tool"]["poetry"]["version"] dynamic_index = pyproject["project"]["dynamic"].index("version") else: - return name if name in _state.projects else None + return None if name in _state.projects: return name @@ -726,6 +730,8 @@ def _revert_version(retain: bool = False) -> None: pyproject["project"]["dynamic"].insert(index, "version") # type: ignore if "version" in pyproject["project"]: # type: ignore pyproject["project"].pop("version") # type: ignore + if state.original_version is not None: + pyproject["tool"]["poetry"]["version"] = state.original_version # type: ignore if not retain and not _state.cli_mode: pyproject["tool"]["poetry-dynamic-versioning"]["enable"] = True # type: ignore diff --git a/poetry_dynamic_versioning/cli.py b/poetry_dynamic_versioning/cli.py index bf0a69f..d10cd1e 100644 --- a/poetry_dynamic_versioning/cli.py +++ b/poetry_dynamic_versioning/cli.py @@ -25,6 +25,10 @@ class Key: build_system = "build-system" requires = "requires" build_backend = "build-backend" + project = "project" + poetry = "poetry" + dynamic = "dynamic" + version = "version" class Command: @@ -120,4 +124,19 @@ def _enable_in_doc(doc: tomlkit.TOMLDocument) -> tomlkit.TOMLDocument: else: doc[Key.build_system].update(build_system_table) # type: ignore + # Poetry 2.0.0+ + if doc.get(Key.project) is not None: + if doc[Key.project].get(Key.version) is not None: + del doc[Key.project][Key.version] + + if doc[Key.project].get(Key.dynamic) is None: + doc[Key.project][Key.dynamic] = [Key.version] + else: + doc[Key.project][Key.dynamic].append(Key.version) + + if doc[Key.tool].get(Key.poetry) is None: + doc[Key.tool][Key.poetry] = tomlkit.table().add(Key.version, "0.0.0") + else: + doc[Key.tool][Key.poetry][Key.version] = "0.0.0" + return doc