Skip to content

Commit

Permalink
#177: Add from-file config section
Browse files Browse the repository at this point in the history
  • Loading branch information
mtkennerly committed Apr 30, 2024
1 parent eebbc26 commit 3890c4e
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## Unreleased

* Added:
* `pattern-prefix` and `ignore-untracked` options.
* `pattern-prefix` option to add a prefix to the version tag pattern.
* `ignore-untracked` option to control the detection of dirty state.
* `from-file` config section to read a version from a file instead of the VCS.
* Changed:
* Updated Dunamai to 1.21.0+ for the latest features.

Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,17 @@ In your pyproject.toml file, you may configure the following options:
__version_tuple__ = (0, 0, 0)
"""
```
* `[tool.poetry-dynamic-versioning.from-file]`:
This section lets you read the version from a file instead of the VCS.

* `source` (string):
If set, read the version from this file.
It must be a path relative to the location of pyproject.toml.
By default, the plugin will read the entire content of the file,
without leading and trailing whitespace.
* `pattern` (string):
If set, use this regular expression to extract the version from the file.
The first capture group must contain the version.

Simple example:

Expand Down
39 changes: 39 additions & 0 deletions poetry_dynamic_versioning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@
},
)

_FromFile = TypedDict(
"_FromFile",
{
"source": Optional[str],
"pattern": Optional[str],
},
)

_Config = TypedDict(
"_Config",
{
Expand All @@ -100,6 +108,7 @@
"strict": bool,
"fix-shallow-repository": bool,
"ignore-untracked": bool,
"from-file": _FromFile,
},
)
else:
Expand Down Expand Up @@ -211,6 +220,10 @@ def _default_config() -> Mapping:
"strict": False,
"fix-shallow-repository": False,
"ignore-untracked": False,
"from-file": {
"source": None,
"pattern": None,
},
}
}
}
Expand Down Expand Up @@ -415,6 +428,28 @@ def _get_override_version(name: Optional[str], env: Optional[Mapping] = None) ->
return None


def _get_version_from_file(config: _Config) -> Optional[str]:
source = config["from-file"]["source"]
pattern = config["from-file"]["pattern"]

if source is None:
return None

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

content = pyproject_path.parent.joinpath(source).read_bytes().decode("utf-8").strip()

if pattern is None:
return content

result = re.search(pattern, content, re.MULTILINE)
if result is None:
raise ValueError("File '{}' did not contain a match for '{}'".format(source, pattern))
return result.group(1)


def _get_version_from_dunamai(
vcs: Vcs, pattern: Union[str, Pattern], config: _Config, *, strict: Optional[bool] = None
) -> Version:
Expand All @@ -436,6 +471,10 @@ def _get_version(config: _Config, name: Optional[str] = None) -> Tuple[str, Vers
if override is not None:
return (override, Version.parse(override))

override = _get_version_from_file(config)
if override is not None:
return (override, Version.parse(override))

vcs = Vcs(config["vcs"])
style = Style(config["style"]) if config["style"] is not None else None

Expand Down

0 comments on commit 3890c4e

Please sign in to comment.