Skip to content

Commit 1bc3347

Browse files
authored
Merge pull request #53 from jwodder/onbuild
Support "onbuild" step when using Hatch
2 parents d9bc4c5 + ba530a7 commit 1bc3347

22 files changed

+741
-114
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
v2.4.0 (in development)
1+
v3.0.0 (in development)
22
-----------------------
33
- Migrated from setuptools to hatch
4+
- Support using the `onbuild` step with Hatch
5+
- **Breaking**: The `build_dir` argument passed to `Versioningit.do_onbuild()`
6+
and `onbuild` method callables has been changed to an `OnbuildFileProvider`
7+
ABC
48

59
v2.3.0 (2023-11-19)
610
-------------------

README.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ extraction & calculation.
5252
- Can optionally write the final version and other details to a file for
5353
loading at runtime
5454

55-
- Provides custom setuptools commands for inserting the final version and other
56-
details into a source file at build time
55+
- Provides custom hooks for inserting the final version and other details into
56+
a source file at build time
5757

5858
- The individual methods for VCS querying, tag-to-version calculation, version
5959
bumping, version formatting, and writing the version to a file can all be

docs/changelog.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
Changelog
44
=========
55

6-
v2.4.0 (in development)
6+
v3.0.0 (in development)
77
-----------------------
88
- Migrated from setuptools to hatch
9+
- Support using the ``onbuild`` step with Hatch
10+
- **Breaking**: The ``build_dir`` argument passed to
11+
`Versioningit.do_onbuild()` and ``onbuild`` method callables has been changed
12+
to an `OnbuildFileProvider` ABC
913

1014

1115
v2.3.0 (2023-11-19)

docs/configuration.rst

Lines changed: 56 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -538,36 +538,30 @@ which takes the following parameters:
538538

539539
.. _onbuild:
540540

541-
The ``[tool.versioningit.onbuild]`` Subtable
542-
--------------------------------------------
543-
544-
.. attention::
545-
546-
Currently, the ``onbuild`` step is not supported when using
547-
``versioningit`` with Hatch. See `issue #54`__ for more information.
548-
549-
__ https://github.com/jwodder/versioningit/issues/54
541+
Enabling & Configuring the ``onbuild`` Step
542+
-------------------------------------------
550543

551544
.. versionadded:: 1.1.0
552545

553-
.. versionadded:: 2.2.0
546+
``versioningit`` provides custom setuptools and Hatch hooks for enabling an
547+
optional feature (called the "``onbuild`` step") in which your project's
548+
version and/or other fields are inserted into a file in sdists & wheels at
549+
build time while leaving your local project directory alone.
554550

555-
``sdist`` and ``build_py`` classes added for use in :file:`setup.cfg` and
556-
:file:`pyproject.toml`
551+
The steps for enabling the ``onbuild`` step differ depending on whether you're
552+
using setuptools or Hatch as your build backend. The configuration options for
553+
the step are the same between the backends, but where you put the configuration
554+
and how you tell the backend to enable the hooks differs.
557555

558-
The ``onbuild`` subtable configures an optional feature, inserting the project
559-
version and/or other fields into built project trees when building an sdist or
560-
wheel. Specifically, this feature allows you to create sdists & wheels in
561-
which some file has been modified to contain the line ``__version__ = "<project
562-
version>"`` or similar while leaving your repository alone.
556+
Using ``onbuild`` with setuptools
557+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
563558

564-
Enabling ``onbuild``
565-
~~~~~~~~~~~~~~~~~~~~
566-
567-
In order to use this feature, in addition to filling out the subtable, you must
568-
configure setuptools to use ``versioningit``'s custom command classes. How to
569-
do this depends on what file you've placed your project's setuptools
570-
configuration in.
559+
There are two steps to enabling the ``onbuild`` step with setuptools. First,
560+
add a ``[tool.versioningit.onbuild]`` table to your :file:`pyproject.toml`
561+
containing your desired configuration for the step (`see below
562+
<onbuild_opts_>`_). Second, you need to tell setuptools to use
563+
``versioningit``'s custom command classes. How to do this depends on what file
564+
you've placed your project's setuptools configuration in.
571565

572566
- If you're configuring setuptools via :file:`setup.cfg`, you can simply add
573567
the following field to its ``[options]`` table:
@@ -611,8 +605,44 @@ may have to manually modify or subclass your command classes and add a call to
611605
`run_onbuild()` at the appropriate location; see the function's documentation
612606
for more information, but you'll largely be on your own at this point.
613607

614-
Configuring ``onbuild``
615-
~~~~~~~~~~~~~~~~~~~~~~~
608+
.. versionadded:: 2.2.0
609+
610+
``sdist`` and ``build_py`` classes added for use in :file:`setup.cfg` and
611+
:file:`pyproject.toml`
612+
613+
Using ``onbuild`` with Hatch
614+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
615+
616+
.. versionadded:: 3.0.0
617+
618+
Support for using the ``onbuild`` step with Hatch
619+
620+
In order to enable & configure the ``onbuild`` step when using ``versioningit``
621+
with Hatch, simply place all of your desired configuration for the step under a
622+
``[tool.hatch.build.hooks.versioningit-onbuild]`` table. Do not use the
623+
``[tool.versioningit.onbuild]`` table with Hatch; it will be ignored, and its
624+
presence will generate a warning.
625+
626+
.. note::
627+
628+
The ``versioningit-onbuild`` build hook is only usable when also using
629+
``versioningit`` as a Hatch version source. Trying to use the build hook
630+
with a different version source will result in an error.
631+
632+
.. note::
633+
634+
The ``versioningit-onbuild`` build hook is only supported when building an
635+
sdist or wheel. Using other Hatch builders (such as `the application
636+
builder`__) with ``versioningit-onbuild`` is not supported or endorsed in
637+
any way.
638+
639+
__ https://hatch.pypa.io/latest/plugins/builder/app/
640+
641+
642+
.. _onbuild_opts:
643+
644+
``onbuild`` Configuration Options
645+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
616646

617647
``versioningit`` provides one ``onbuild`` method, ``"replace-version"`` (the
618648
default). It scans a given file for a line matching a given regex and inserts

docs/hatch.rst

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ way as for setuptools:
3737
[tool.hatch.version]
3838
source = "versioningit"
3939
40-
- Configure ``versioningit`` as normal. However, with Hatch, you have two
41-
possible locations to put ``versioningit``'s configuration in: either a
42-
``[tool.versioningit]`` table as used with setuptools or under the
43-
``[tool.hatch.version]`` table. Moreover, unlike when using setuptools, you
44-
don't even need the ``[tool.versioningit]`` table if it's just going to be
45-
empty.
40+
- Configure ``versioningit`` as normal (mostly; see the note about ``onbuild``
41+
below). However, with Hatch, you have two possible locations to put
42+
``versioningit``'s configuration in: either the ``[tool.versioningit]`` table
43+
as used with setuptools or under the ``[tool.hatch.version]`` table.
44+
Moreover, unlike when using setuptools, you don't even need the
45+
``[tool.versioningit]`` table if it's just going to be empty.
4646

4747
For example, the following configurations are equivalent:
4848

@@ -91,12 +91,11 @@ way as for setuptools:
9191
# `write` step:
9292
artifacts = ["src/mypackage/_version.py"]
9393
94-
.. attention::
95-
96-
Currently, :ref:`the onbuild step <onbuild>` is not supported when using
97-
``versioningit`` with Hatch. See `issue #54`__ for more information.
98-
99-
__ https://github.com/jwodder/versioningit/issues/54
94+
- The configuration for the ``onbuild`` step is placed in the
95+
``[tool.hatch.build.hooks.versioningit-onbuild]`` table (not in
96+
``[tool.versioningit.onbuild]`` or ``[tool.hatch.version.onbuild]``). In
97+
addition, filling out this table is all you need to do to enable the
98+
``onbuild`` step — no fiddling with command classes necessary!
10099

101100
.. note::
102101

docs/how.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ creates a file at a specified path containing the project's version.
5757
``onbuild`` Step
5858
^^^^^^^^^^^^^^^^
5959

60-
When a project is built that uses ``versioningit``'s custom setuptools
61-
commands, the ``onbuild`` step becomes added to the build process. The default
62-
``onbuild`` method updates one of the files in the built distribution to
63-
contain the project version while leaving the source files in the actual
64-
project alone. See ":ref:`onbuild`" for more information.
60+
When a project is built that uses ``versioningit``'s custom setuptools commands
61+
or Hatch build hook, the ``onbuild`` step becomes added to the build process.
62+
The default ``onbuild`` method updates one of the files in the built
63+
distribution to contain the project version while leaving the source files in
64+
the actual project alone. See ":ref:`onbuild`" for more information.

docs/index.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ extraction & calculation.
4646
- Can optionally write the final version and other details to a file for
4747
loading at runtime
4848

49-
- Provides custom setuptools commands for inserting the final version and other
50-
details into a source file at build time
49+
- Provides custom hooks for inserting the final version and other details into
50+
a source file at build time
5151

5252
- The individual methods for VCS querying, tag-to-version calculation, version
5353
bumping, version formatting, and writing the version to a file can all be

docs/runtime-version.rst

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ Automatically setting your project's version is all well and good, but you
55
usually also want to expose that version at runtime, usually via a
66
``__version__`` variable. There are three options for doing this:
77

8-
1. Use the `~importlib.metadata.version()` function in `importlib.metadata` to
9-
get your package's version, like so:
8+
1. Use the `~importlib.metadata.version()` function from `importlib.metadata`
9+
to get your package's version, like so:
1010

1111
.. code:: python
1212
@@ -68,12 +68,10 @@ usually also want to expose that version at runtime, usually via a
6868
from pathlib import Path
6969
__version__ = Path(__file__).with_name("VERSION").read_text().strip()
7070
71-
3. *(New in version 1.1.0)* Fill out the ``[tool.versioningit.onbuild]``
72-
subtable in :file:`pyproject.toml` and configure setuptools to use
73-
``versioningit``'s custom build commands. This will allow you to create
74-
sdists & wheels in which some file has been modified to contain the line
75-
``__version__ = "<project version>"`` or similar while leaving your
76-
repository alone. See ":ref:`onbuild`" for more information.
71+
3. *(New in version 1.1.0)* Use the :ref:`onbuild step <onbuild>` and its
72+
custom hooks to create sdists & wheels in which some file has been modified
73+
to contain the line ``__version__ = "<project version>"`` or similar while
74+
leaving your repository's contents alone.
7775

7876
.. tip::
7977

@@ -90,9 +88,7 @@ usually also want to expose that version at runtime, usually via a
9088
Should affected file be under version control? **No** **Yes**
9189
Affected file must already exist? **No** **Yes**
9290
Modifies working tree? [#f1]_ **Yes** **No**
93-
Requires configuration in ``setup.{py,cfg}``? **No** **Yes**
9491
Run when installing in editable mode? **Yes** **No**
95-
Usable with Hatch? **Yes** **No**
9692
============================================== ========= ===========
9793

9894
.. [#f1] That is, the ``write`` method causes a file to be present (though

docs/writing-methods.rst

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,13 +168,13 @@ A custom ``write`` method is a callable with the following synopsis:
168168

169169
A custom ``onbuild`` method is a callable with the following synopsis:
170170

171-
.. function:: funcname(*, build_dir: str | pathlib.Path, is_source: bool, template_fields: dict[str, Any], params: dict[str, Any]) -> None
171+
.. function:: funcname(*, file_provider: OnbuildFileProvider, is_source: bool, template_fields: dict[str, Any], params: dict[str, Any]) -> None
172172
:noindex:
173173

174-
Modifies one or more files in ``build_dir``
174+
Modifies the files about to be included in an sdist or wheel
175175

176-
:param path build_dir:
177-
the path to the directory where the project is being built
176+
:param file_provider:
177+
an object for accessing files being built into an sdist or wheel
178178
:param bool is_source:
179179
true if an sdist or other artifact that preserves source paths is being
180180
built, false if a wheel or other artifact that uses installation paths
@@ -187,6 +187,17 @@ A custom ``onbuild`` method is a callable with the following synopsis:
187187

188188
``version`` argument replaced with ``template_fields``
189189

190+
.. versionchanged:: 3.0.0
191+
192+
``build_dir`` argument replaced with ``file_provider``
193+
194+
``onbuild`` methods are provided with instances of the following abstract base
195+
classes for operating on:
196+
197+
.. autoclass:: versioningit.OnbuildFileProvider
198+
199+
.. autoclass:: versioningit.OnbuildFile
200+
190201

191202
Distributing Your Methods in an Extension Package
192203
-------------------------------------------------

src/versioningit/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
2424
- Can optionally write the final version to a file for loading at runtime
2525
26-
- Provides custom setuptools commands for inserting the final version into a
27-
source file at build time
26+
- Provides custom hooks for inserting the final version into a source file at
27+
build time
2828
2929
- The individual methods for VCS querying, tag-to-version calculation, version
3030
bumping, version formatting, and writing the version to a file can all be
@@ -43,7 +43,7 @@
4343
<https://versioningit.rtfd.io> for more information.
4444
"""
4545

46-
__version__ = "2.4.0.dev1"
46+
__version__ = "3.0.0.dev1"
4747
__author__ = "John Thorvald Wodder II"
4848
__author_email__ = "[email protected]"
4949
__license__ = "MIT"
@@ -72,6 +72,7 @@
7272
NotVersioningitError,
7373
)
7474
from .get_cmdclasses import get_cmdclasses
75+
from .onbuild import OnbuildFile, OnbuildFileProvider
7576

7677
__all__ = [
7778
"ConfigError",
@@ -84,6 +85,8 @@
8485
"NotSdistError",
8586
"NotVCSError",
8687
"NotVersioningitError",
88+
"OnbuildFile",
89+
"OnbuildFileProvider",
8790
"Report",
8891
"VCSDescription",
8992
"Versioningit",

0 commit comments

Comments
 (0)