Skip to content

Commit

Permalink
updated automatic versioning
Browse files Browse the repository at this point in the history
  • Loading branch information
ksiller committed Feb 25, 2025
1 parent 1d41f84 commit dc8edc5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,4 @@ venv/
.DS_Store

# build metadata written by hatch
src/mitoanalyzer/_version.p
src/mitosisanalyzer/_version.py
15 changes: 13 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ build-backend = "hatchling.build"
[tool.hatch.version]
source = "versioningit"

[tool.versioningit]
default-version = "0.0.0+unknown"
[tool.versioningit.vcs]
match = ["[0-9]*.[0-9]*.[0-9]*", "[0-9]*.[0-9]*.[0-9]*.dev[0-9]*"]
default-tag = "0.0.0"

[tool.versioningit.next-version]
method = "smallest-release"

[tool.versioningit.write]
method = { module = "write_version_info", value = "write_version_info"}
path = "src/mitosisanalyzer/_version.py"

[tool.versioningit.format]
distance = "{base_version}.post{distance}+{vcs}{rev}"
Expand All @@ -62,3 +70,6 @@ distance-dirty = "{base_version}.post{distance}+{vcs}{rev}.d{build_date:%Y%m%d}"
# Replace the path below with the path to the file created by the
# `write` step:
artifacts = ["src/mitoanalyzer/_version.py"]

[tool.hatch.build.targets.sdist]
include = ["/src/mitosisanalyzer", "/README.md", "/LICENSE", "/pyproject.toml"]
3 changes: 3 additions & 0 deletions src/mitosisanalyzer/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import _version

__version__ = _version.__version__
40 changes: 40 additions & 0 deletions write_version_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from __future__ import annotations

import textwrap
from datetime import datetime, timezone
from pathlib import Path
from subprocess import CalledProcessError, check_output
from typing import Any


def write_version_info(
project_dir: str | Path, template_fields: dict[str, Any], params: dict[str, Any]
) -> None:
"""
Write the build info to the project directory.
"""
path = Path(project_dir) / params.get("path", "src/mitosisanalyzer/_version.py")

try:
git_hash = check_output(["git", "rev-parse", "HEAD"]).decode().strip()
except CalledProcessError:
git_hash = "unknown"

build_dt_str = template_fields.get(
"build_date", datetime.now(timezone.utc).isoformat()
)
version = template_fields.get("version", "unknown")
dirty = "dirty" in version

build_info = textwrap.dedent(
f"""\
# Generated by versioningit
__version__ = "{version}"
__build_date__ = "{build_dt_str}"
__git_commit__ = "{git_hash}"
__dirty__ = {dirty}
"""
)

with open(path, "w") as f:
f.write(build_info)

0 comments on commit dc8edc5

Please sign in to comment.