@@ -152,25 +152,34 @@ def get_repo():
152
152
153
153
def get_version ():
154
154
"""Get the current package version"""
155
+ # Prefer to get a static version from pyproject.toml.
156
+ if PYPROJECT .exists ():
157
+ text = PYPROJECT .read_text (encoding = "utf-8" )
158
+ data = toml .loads (text )
159
+ project = data .get ("project" , {})
160
+ version = project .get ("version" )
161
+ if version :
162
+ return version
163
+
164
+ # If this is a hatchling project, use hatchling to get
165
+ # the dynamic version.
166
+ if data .get ("build-system" , {}).get ("build-backend" ) == "hatchling.build" :
167
+ return run ("hatchling version" ).split ("\n " )[- 1 ]
168
+
155
169
if SETUP_PY .exists ():
156
170
warnings .warn ("Using deprecated setup.py invocation" )
157
171
try :
158
172
return run ("python setup.py --version" ).split ("\n " )[- 1 ]
159
173
except CalledProcessError as e :
160
174
print (e )
161
175
176
+ # Build the wheel and extract the version.
162
177
if PYPROJECT .exists ():
163
- text = PYPROJECT .read_text (encoding = "utf-8" )
164
- data = toml .loads (text )
165
- project = data .get ("project" , {})
166
- version = project .get ("version" )
167
- if not version :
168
- with tempfile .TemporaryDirectory () as tempdir :
169
- run (f"{ sys .executable } -m build --wheel --outdir { tempdir } " )
170
- wheel_path = glob (f"{ tempdir } /*.whl" )[0 ]
171
- wheel = Wheel (wheel_path )
172
- version = wheel .version
173
- return version
178
+ with tempfile .TemporaryDirectory () as tempdir :
179
+ run (f"{ sys .executable } -m build --wheel --outdir { tempdir } " )
180
+ wheel_path = glob (f"{ tempdir } /*.whl" )[0 ]
181
+ wheel = Wheel (wheel_path )
182
+ version = wheel .version
174
183
175
184
if PACKAGE_JSON .exists ():
176
185
return json .loads (PACKAGE_JSON .read_text (encoding = "utf-8" )).get ("version" , "" )
0 commit comments