diff --git a/CHANGELOG.md b/CHANGELOG.md index 0608a20..32c33a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. diff --git a/README.md b/README.md index 9a0ac19..d85781b 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/poetry_dynamic_versioning/__init__.py b/poetry_dynamic_versioning/__init__.py index 888d050..0a36832 100644 --- a/poetry_dynamic_versioning/__init__.py +++ b/poetry_dynamic_versioning/__init__.py @@ -76,6 +76,14 @@ }, ) + _FromFile = TypedDict( + "_FromFile", + { + "source": Optional[str], + "pattern": Optional[str], + }, + ) + _Config = TypedDict( "_Config", { @@ -100,6 +108,7 @@ "strict": bool, "fix-shallow-repository": bool, "ignore-untracked": bool, + "from-file": _FromFile, }, ) else: @@ -211,6 +220,10 @@ def _default_config() -> Mapping: "strict": False, "fix-shallow-repository": False, "ignore-untracked": False, + "from-file": { + "source": None, + "pattern": None, + }, } } } @@ -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: @@ -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