Skip to content

Commit ecb30ec

Browse files
authored
Add hatchling handling (#329)
1 parent 5038e71 commit ecb30ec

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

jupyter_releaser/tests/test_functions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,16 @@ def test_get_version_pyproject_dynamic(py_package):
3838
assert util.get_version() == "0.0.1"
3939

4040

41+
def test_get_version_hatchling(py_package):
42+
py_project = py_package / "pyproject.toml"
43+
with open(py_project) as fid:
44+
data = toml.load(fid)
45+
del data["project"]["version"]
46+
data["build-system"] = {"requires": ["hatchling>=1.0"], "build-backend": "hatchling.build"}
47+
with open(py_project, "w") as fid:
48+
fid.write(str(py_project))
49+
50+
4151
def test_get_version_setuppy(py_package):
4252
assert util.get_version() == "0.0.1"
4353
util.bump_version("0.0.2a0")

jupyter_releaser/util.py

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -152,25 +152,34 @@ def get_repo():
152152

153153
def get_version():
154154
"""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+
155169
if SETUP_PY.exists():
156170
warnings.warn("Using deprecated setup.py invocation")
157171
try:
158172
return run("python setup.py --version").split("\n")[-1]
159173
except CalledProcessError as e:
160174
print(e)
161175

176+
# Build the wheel and extract the version.
162177
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
174183

175184
if PACKAGE_JSON.exists():
176185
return json.loads(PACKAGE_JSON.read_text(encoding="utf-8")).get("version", "")

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ dependencies = [
2727
"click",
2828
"ghapi",
2929
"github-activity~=0.2",
30+
"hatchling>=1.0",
3031
"importlib_resources",
3132
"jsonschema>=3.0.1",
3233
"packaging",

0 commit comments

Comments
 (0)