From 650f7558091b737563c646d7af67aeba6fc05bed Mon Sep 17 00:00:00 2001 From: Tom Most Date: Sat, 20 Jul 2024 14:26:52 -0700 Subject: [PATCH] Hatch version source plugin --- pyproject.toml | 3 ++ src/incremental/_hatch.py | 35 +++++++++++++++++++ .../example_hatchling/__init__.py | 3 ++ .../example_hatchling/_version.py | 11 ++++++ tests/example_hatchling/pyproject.toml | 16 +++++++++ tests/test_examples.py | 17 +++++++++ 6 files changed, 85 insertions(+) create mode 100644 src/incremental/_hatch.py create mode 100644 tests/example_hatchling/example_hatchling/__init__.py create mode 100644 tests/example_hatchling/example_hatchling/_version.py create mode 100644 tests/example_hatchling/pyproject.toml diff --git a/pyproject.toml b/pyproject.toml index d80bd8d8..7da60104 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -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] diff --git a/src/incremental/_hatch.py b/src/incremental/_hatch.py new file mode 100644 index 00000000..2f3f2d92 --- /dev/null +++ b/src/incremental/_hatch.py @@ -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] diff --git a/tests/example_hatchling/example_hatchling/__init__.py b/tests/example_hatchling/example_hatchling/__init__.py new file mode 100644 index 00000000..26d23bad --- /dev/null +++ b/tests/example_hatchling/example_hatchling/__init__.py @@ -0,0 +1,3 @@ +from ._version import __version__ + +__all__ = ["__version__"] diff --git a/tests/example_hatchling/example_hatchling/_version.py b/tests/example_hatchling/example_hatchling/_version.py new file mode 100644 index 00000000..6f1e6b0b --- /dev/null +++ b/tests/example_hatchling/example_hatchling/_version.py @@ -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__"] diff --git a/tests/example_hatchling/pyproject.toml b/tests/example_hatchling/pyproject.toml new file mode 100644 index 00000000..fa017450 --- /dev/null +++ b/tests/example_hatchling/pyproject.toml @@ -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" diff --git a/tests/test_examples.py b/tests/test_examples.py index 40048507..118b7c0f 100644 --- a/tests/test_examples.py +++ b/tests/test_examples.py @@ -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__))) @@ -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")