Skip to content

Commit

Permalink
feat: add ability to read version from a file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ramon Buckland committed Nov 13, 2023
1 parent 336abec commit b466c55
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
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
6 changes: 3 additions & 3 deletions poetry_version_plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ def activate(self, poetry: Poetry, io: IO) -> None:
f"[{version_file_config}]. cannot extract dynamic version"
)
else:
version = version_file.read_text()
version = version_file.read_text().strip()
io.write_line(
"<b>poetry-version-plugin</b>: Setting package "
"dynamic version to __version__ "
f"variable from __init__.py: <b>{version}</b>"
f"dynamic version to value from {version_file}"
f": <b>{version}</b>"
)
poetry.package._set_version(version)
return

0 comments on commit b466c55

Please sign in to comment.