Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

From version file #45

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Alternatively, it can read it from a **git tag**, set with a GitHub release or w
$ git tag 0.1.0
```

Alternatively, it can read a version from a static file (which may have been dynamically generated)
```toml

[tool.poetry-version-plugin]
source = "version_file"
filename = "src/mypackage/VERSION"
```

🚨 Consider this in the alpha stage. Read the warning below.

## When to use
Expand Down Expand Up @@ -112,6 +120,32 @@ Building my-awesome-package (0.1.3)
- Built my-awesome-package-0.1.3-py3-none-any.whl
```

### Set the version in a VERSION file

With a `VERSION`` file, you can dynamically generate the file (as a step of the CI/CD pipeline for example) and read the file

```python
#__init__.py

from functools import cache

@cache
def read_version_file() -> str:
"""Simple style, read a file."""
try:
import pathlib
dir = pathlib.Path(__file__).parent.resolve()
return pathlib.Path(dir / "VERSION").read_text()
except Exception:
# Fall back in case the file was missing
return "0.0.0"

__version__: str = read_version_file()

```
This provides a happy medium of both a static, or dynamically generated `VERSION` file that can be used
in wider CI/CD build systems, and have both python, and poetry read the `VERSION` file.

## Version in `pyproject.toml`

Currently (2021-05-24) Poetry requires a `version` configuration in the `pyproject.toml`, even if you use this plugin.
Expand Down Expand Up @@ -349,6 +383,14 @@ and
source = "git-tag"
```

or

```toml
[tool.poetry-version-plugin]
source = "version_file"
filename = "src/mypackage/VERSION"
```

let me know what alternative configuration would make more sense and be more intuitive to you.

👍 The good news is, assuming you are building packages to then upload them to PyPI for your users to download and use them, the **worst that could happen** if something broke is that you wouldn't be able to build a new version until something is fixed or changed. But your users shouldn't be affected in any way.
Expand Down
17 changes: 17 additions & 0 deletions poetry_version_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,20 @@ def activate(self, poetry: Poetry, io: IO) -> None:
)
io.write_error_line(message)
raise RuntimeError(message)
elif version_source == "version_file":
version_file_config = poetry_version_config.get("filename")
version_file = Path(poetry_version_config.get("filename"))
if not version_file.is_file():
message = (
"<b>poetry-version-plugin</b>: version_file was not found at "
f"[{version_file_config}]. cannot extract dynamic version"
)
else:
version = version_file.read_text().strip()
io.write_line(
"<b>poetry-version-plugin</b>: Setting package "
f"dynamic version to value from {version_file}"
f": <b>{version}</b>"
)
poetry.package._set_version(version)
return
Loading