Skip to content

Commit

Permalink
Hatch version source plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
twm committed Jul 20, 2024
1 parent 7294bac commit 650f755
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ maintainers = [
classifiers = [
"Intended Audience :: Developers",
"License :: OSI Approved :: MIT License",
"Framework :: Hatch",
"Framework :: Setuptools Plugin",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.8",
Expand Down Expand Up @@ -50,6 +51,8 @@ Changelog = "https://github.com/twisted/incremental/blob/trunk/NEWS.rst"
use_incremental = "incremental:_get_distutils_version"
[project.entry-points."setuptools.finalize_distribution_options"]
incremental = "incremental:_get_setuptools_version"
[project.entry-points.hatch]
incremental = "incremental._hatch"

[tool.incremental]

Expand Down
35 changes: 35 additions & 0 deletions src/incremental/_hatch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright (c) Twisted Matrix Laboratories.
# See LICENSE for details.

import os
import shlex
from typing import TypedDict

from hatchling.version.source.plugin.interface import VersionSourceInterface
from hatchling.plugin import hookimpl

from incremental import _load_pyproject_toml, _existing_version


class _VersionData(TypedDict):
version: str


class IncrementalVersionSource(VersionSourceInterface):
PLUGIN_NAME = "incremental"

def get_version_data(self) -> _VersionData:
config = _load_pyproject_toml(os.path.join(self.root, "./pyproject.toml"))
return {"version": _existing_version(config.path).public()}

def set_version(self, version: str, version_data: dict):
raise NotImplementedError(
f"Run `python -m incremental.version --newversion"
f" {shlex.quote(version)}` to set the version.\n\n"
f" See `python -m incremental.version --help` for more options."
)


@hookimpl
def hatch_register_version_source():
return [IncrementalVersionSource]
3 changes: 3 additions & 0 deletions tests/example_hatchling/example_hatchling/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from ._version import __version__

__all__ = ["__version__"]
11 changes: 11 additions & 0 deletions tests/example_hatchling/example_hatchling/_version.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"""
Provides example_hatchling version information.
"""

# This file is auto-generated! Do not edit!
# Use `python -m incremental.update example_hatchling` to change this file.

from incremental import Version

__version__ = Version("example_hatchling", 24, 7, 0)
__all__ = ["__version__"]
16 changes: 16 additions & 0 deletions tests/example_hatchling/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[build-system]
requires = [
"hatchling",
"incremental",
]
build-backend = "hatchling.build"

[project]
name = "example_hatchling"
dependencies = [
"incremental",
]
dynamic = ["version"]

[tool.hatch.version]
source = "incremental"
17 changes: 17 additions & 0 deletions tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
from twisted.python.filepath import FilePath
from twisted.trial.unittest import TestCase

from incremental import Version


TEST_DIR = FilePath(os.path.abspath(os.path.dirname(__file__)))

Expand Down Expand Up @@ -47,3 +49,18 @@ def test_setuptools_version(self):

self.assertEqual(example_setuptools.__version__.base(), "2.3.4")
self.assertEqual(metadata.version("example_setuptools"), "2.3.4")

def test_hatchling_get_version(self):
"""
example_hatchling has a version of 24.7.0, which may be retrieved
by the ``hatch version`` command.
"""
build_and_install(TEST_DIR.child("example_hatchling"))

import example_hatchling

self.assertEqual(
example_hatchling.__version__,
Version("example_hatchling", 24, 7, 0),
)
self.assertEqual(metadata.version("example_hatchling"), "24.7.0")

0 comments on commit 650f755

Please sign in to comment.