@@ -41,34 +41,43 @@ for easier development, testing and pre-release deployment?!
4141
4242Search no more!
4343
44- As long as you mark your releases using git tags, instead of hardcoding:
44+ As long as you use git and you mark your releases using git tags, version-query will do the rest.
4545
46- .. code :: python
46+ It's 21st century, stop hardcoding version numbers!
4747
48- __version__ = ' 1.5.0.dev2'
48+ .. contents ::
49+ :backlinks: none
4950
50- You can do:
5151
52- .. code :: python
52+ Overview
53+ ========
5354
54- from version_query import predict_version_str
55+ There are two main ways of using version-query - at runtime or at build time.
5556
56- __version__ = predict_version_str()
57+ Using at runtime
58+ ----------------
5759
58- It's 21st century, stop hardcoding version numbers!
60+ When using this way, version-query is needed as a runtime dependency of your package.
5961
60- This will set the version to release version when you really release a new version,
61- and it will automatically generate a suitable development version at development/pre-release phase.
62+ If, for example, you have a constant storing the version of your package in some Python module:
6263
64+ .. code :: python
6365
64- .. contents ::
65- :backlinks: none
66+ VERSION = ' 1.5.0.dev2'
6667
68+ You can do the following instead of hardcoding it:
6769
68- Overview
69- ========
70+ .. code :: python
7071
71- At development time, the current version number is automatically generated based on:
72+ from version_query import predict_version_str
73+
74+ VERSION = predict_version_str()
75+
76+ This will set the version to release version when you really release a new version,
77+ and it will automatically generate a suitable development version at development/pre-release phase.
78+
79+ When your package is imported while running from inside the repository, such as during development,
80+ the current version number is automatically generated based on:
7281
7382* tags
7483* current commit SHA
@@ -81,16 +90,88 @@ or at runtime) then the script relies on metadata generated at packaging time.
8190That's why, regardless if package is installed from PyPI (from source or wheel distribution)
8291or cloned from GitHub, the version query will work.
8392
84- Additionally, version numbers in version-query are mutable objects and they can be conveniently
85- incremented, compared with each other, as well as converted to/from other popular
86- versioning formats.
93+ Using at build time
94+ -------------------
95+
96+ This is the way to go if you want to use version-query only as a dependency when building
97+ the package, in such case it's not necessary to to add it to runtime dependencies.
98+
99+ There are many build systems available for Python, and version-query may not be compatible
100+ with all of them. Below are some examples.
101+
102+ setuptools with ``setup.py `` script
103+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
104+
105+ This is a legacy way of building Python packages, but it is still widely used.
106+
107+ In such setup, you just need to add the following to your ``setup.py `` file:
108+
109+ .. code :: python
110+
111+ import setuptools
112+ from version_query import predict_version_str
113+
114+
115+ setuptools.setup(
116+ ... ,
117+ version = predict_version_str()
118+ )
119+
120+ If you are already using version-query at runtime in your package to set a constant
121+ (for example ``my_package.VERSION ``, please see "Using at runtime" section above for details),
122+ you may instead reuse the same constant in your ``setup.py `` file:
123+
124+ .. code :: python
125+
126+ import setuptools
127+
128+ from my_package import VERSION
129+
130+
131+ setuptools.setup(
132+ ... ,
133+ version = VERSION
134+ )
135+
136+ dynamic version attribute in ``pyproject.toml ``
137+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
138+
139+ A more modern approach in Python is to use ``pyproject.toml `` for building packages.
140+
141+ Some build systems that use ``pyproject.toml `` support setting the version dynamically
142+ by allowing users to point to an attribute of some module in order to get the version.
143+
144+ One of such packages is setuptools. When using it, one can do the following:
145+
146+ .. code :: toml
147+
148+ [project]
149+ dynamic = ["version"]
150+ ...
151+
152+ [build-system]
153+ requires = ["setuptools >= 70.0"]
154+ build-backend = "setuptools.build_meta"
155+
156+ [tool.setuptools.dynamic]
157+ version = {attr = "version_query.local_git_version.PREDICTED"}
158+
159+ Depending on how you build your package, the build system may copy files to a temporary folder
160+ when building. In such case, you need to set ``PROJECT_FOLDER `` environment variable
161+ to the current working directory before running the build command.
162+
163+ For example, when using ``build `` package:
164+
165+ .. code :: bash
166+
167+ PROJECT_FOLDER=$( pwd) python3 -m build
87168
88169 Versioning scheme
89170=================
90171
91172Version scheme used by version-query is a relaxed mixture of:
92173
93- * `Semantic Versioning 2.0.0 <http://semver.org/ >`_ and
174+ * `Semantic Versioning 2.0.0 <http://semver.org/ >`__ and
94175
95176* `PEP 440 -- Version Identification and Dependency Specification <https://www.python.org/dev/peps/pep-0440/ >`_.
96177
@@ -275,7 +356,7 @@ The base specification of the comparison scheme is:
275356
276357* `PEP 508 -- Dependency specification for Python Software Packages <https://www.python.org/dev/peps/pep-0508/ >`_ as well as
277358
278- * `Semantic Versioning 2.0.0 <http://semver.org/ >`_ .
359+ * `Semantic Versioning 2.0.0 <http://semver.org/ >`__ .
279360
280361With the notable difference to both that all version components are taken into account when
281362establishing version precedence.
@@ -373,7 +454,7 @@ The Version objects are mutable, hashable and comparable.
373454 version = version_query.query_caller(stack_level = 1 )
374455 version = version_query.predict_caller(2 )
375456
376- Version object can be obtained for any supported path, as well as for any python code
457+ Version object can be obtained for any supported path, as well as for any Python code
377458currently being executed -- as long as it is located in a supported location.
378459
379460
0 commit comments