Skip to content

Commit 2526307

Browse files
committed
Add mechanism to write version information to a file
1 parent dd86d10 commit 2526307

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ the following functionality:
66

77
* Download a zip file from a specified URL
88
* Download a zip file from a maven repository using maven coordinates
9+
* Write project version information to a file (primarily useful when not
10+
already using a dynamic version source that provides this)
911

1012
See [config](src/hatch_robotpy/config.py) for `pyproject.toml` configuration.
1113

src/hatch_robotpy/config.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,17 @@ def _update_with_platform(self, platform):
156156

157157
@dataclasses.dataclass
158158
class HookConfig:
159+
"""
160+
.. code-block:: toml
161+
162+
[tool.hatch.build.hooks.robotpy]
163+
164+
"""
165+
166+
#: When set, writes a simple python file with the project version to the
167+
#: specified file (relative to the root of the project).
168+
version_file: T.Optional[str] = None
169+
159170
maven_lib_download: T.List[MavenLibDownload] = dataclasses.field(
160171
default_factory=list
161172
)

src/hatch_robotpy/plugin.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,29 @@ class DownloadHook(BuildHookInterface):
2323
PLUGIN_NAME = "robotpy"
2424

2525
def initialize(self, version: str, build_data: T.Dict[str, T.Any]) -> None:
26+
self.process_version(version, build_data)
27+
self.process_downloads(version, build_data)
28+
29+
def process_version(self, version: str, build_data: T.Dict[str, T.Any]) -> None:
30+
version_file = self.parsed_cfg.version_file
31+
if version_file is None:
32+
return
33+
34+
version = self.metadata.version
35+
version_path = pathlib.PurePosixPath(version_file)
36+
37+
with open(version_path, "w") as fp:
38+
fp.writelines(
39+
[
40+
"# This file is automatically generated, DO NOT EDIT\n",
41+
"\n",
42+
f'version = __version__ = "{version}"\n',
43+
]
44+
)
45+
46+
build_data["artifacts"].append(f"/{version_path}")
47+
48+
def process_downloads(self, version: str, build_data: T.Dict[str, T.Any]) -> None:
2649
# Don't need to generate files when creating an sdist
2750
# - this violates the idea that an sdist should be able to be used
2851
# offline, but because we're downloading the external artifacts

0 commit comments

Comments
 (0)