Skip to content

Commit 5d8c829

Browse files
authored
Embed the project version in the name of binaries (#851)
1 parent 283a478 commit 5d8c829

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

backend/src/hatchling/builders/app.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,21 @@ def build_bootstrap(self, directory: str, **build_data: Any) -> str:
153153

154154
self.cargo_build(install_command, cwd=context_dir, env=env)
155155

156-
exe_stem = f'{script}-{build_target}' if build_target else script
156+
exe_stem = (
157+
f'{script}-{self.metadata.version}-{build_target}'
158+
if build_target
159+
else f'{script}-{self.metadata.version}'
160+
)
157161
exe_path = os.path.join(app_dir, f'{exe_stem}.exe' if on_windows else exe_stem)
158162
shutil.move(temp_exe_path, exe_path)
159163
else:
160164
self.cargo_build(install_command, cwd=context_dir, env=base_env)
161165

162-
exe_stem = f'{self.metadata.name}-{build_target}' if build_target else self.metadata.name
166+
exe_stem = (
167+
f'{self.metadata.name}-{self.metadata.version}-{build_target}'
168+
if build_target
169+
else f'{self.metadata.name}-{self.metadata.version}'
170+
)
163171
exe_path = os.path.join(app_dir, f'{exe_stem}.exe' if on_windows else exe_stem)
164172
shutil.move(temp_exe_path, exe_path)
165173

docs/history/hatchling.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
## Unreleased
1010

11+
***Added:***
12+
13+
- The `app` build target now embeds the project version in the name of binaries
14+
1115
## [1.16.1](https://github.com/pypa/hatch/releases/tag/hatchling-v1.16.1) - 2023-05-11 ## {: #hatchling-v1.16.1 }
1216

1317
***Fixed:***

tests/backend/builders/test_app.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ def test_default(self, hatch, helpers, temp_dir, mocker):
274274
build_artifacts = list(build_path.iterdir())
275275
assert len(build_artifacts) == 1
276276
assert expected_artifact == str(build_artifacts[0])
277-
assert (build_path / 'app' / ('my-app.exe' if sys.platform == 'win32' else 'my-app')).is_file()
277+
assert (build_path / 'app' / ('my-app-0.1.0.exe' if sys.platform == 'win32' else 'my-app-0.1.0')).is_file()
278278

279279
def test_default_build_target(self, hatch, helpers, temp_dir, mocker):
280280
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -316,7 +316,9 @@ def test_default_build_target(self, hatch, helpers, temp_dir, mocker):
316316
build_artifacts = list(build_path.iterdir())
317317
assert len(build_artifacts) == 1
318318
assert expected_artifact == str(build_artifacts[0])
319-
assert (build_path / 'app' / ('my-app-target.exe' if sys.platform == 'win32' else 'my-app-target')).is_file()
319+
assert (
320+
build_path / 'app' / ('my-app-0.1.0-target.exe' if sys.platform == 'win32' else 'my-app-0.1.0-target')
321+
).is_file()
320322

321323
def test_scripts(self, hatch, helpers, temp_dir, mocker):
322324
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -360,7 +362,7 @@ def test_scripts(self, hatch, helpers, temp_dir, mocker):
360362
build_artifacts = list(build_path.iterdir())
361363
assert len(build_artifacts) == 1
362364
assert expected_artifact == str(build_artifacts[0])
363-
assert (build_path / 'app' / ('foo.exe' if sys.platform == 'win32' else 'foo')).is_file()
365+
assert (build_path / 'app' / ('foo-0.1.0.exe' if sys.platform == 'win32' else 'foo-0.1.0')).is_file()
364366

365367
def test_scripts_build_target(self, hatch, helpers, temp_dir, mocker):
366368
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -404,7 +406,9 @@ def test_scripts_build_target(self, hatch, helpers, temp_dir, mocker):
404406
build_artifacts = list(build_path.iterdir())
405407
assert len(build_artifacts) == 1
406408
assert expected_artifact == str(build_artifacts[0])
407-
assert (build_path / 'app' / ('foo-target.exe' if sys.platform == 'win32' else 'foo-target')).is_file()
409+
assert (
410+
build_path / 'app' / ('foo-0.1.0-target.exe' if sys.platform == 'win32' else 'foo-0.1.0-target')
411+
).is_file()
408412

409413
def test_custom_cargo(self, hatch, helpers, temp_dir, mocker):
410414
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -446,7 +450,7 @@ def test_custom_cargo(self, hatch, helpers, temp_dir, mocker):
446450
build_artifacts = list(build_path.iterdir())
447451
assert len(build_artifacts) == 1
448452
assert expected_artifact == str(build_artifacts[0])
449-
assert (build_path / 'app' / ('my-app.exe' if sys.platform == 'win32' else 'my-app')).is_file()
453+
assert (build_path / 'app' / ('my-app-0.1.0.exe' if sys.platform == 'win32' else 'my-app-0.1.0')).is_file()
450454

451455
def test_no_cargo(self, hatch, helpers, temp_dir, mocker):
452456
mocker.patch('shutil.which', return_value=None)
@@ -514,7 +518,7 @@ def test_python_version(self, hatch, helpers, temp_dir, mocker):
514518
build_artifacts = list(build_path.iterdir())
515519
assert len(build_artifacts) == 1
516520
assert expected_artifact == str(build_artifacts[0])
517-
assert (build_path / 'app' / ('my-app.exe' if sys.platform == 'win32' else 'my-app')).is_file()
521+
assert (build_path / 'app' / ('my-app-0.1.0.exe' if sys.platform == 'win32' else 'my-app-0.1.0')).is_file()
518522

519523
def test_pyapp_version(self, hatch, helpers, temp_dir, mocker):
520524
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -556,7 +560,7 @@ def test_pyapp_version(self, hatch, helpers, temp_dir, mocker):
556560
build_artifacts = list(build_path.iterdir())
557561
assert len(build_artifacts) == 1
558562
assert expected_artifact == str(build_artifacts[0])
559-
assert (build_path / 'app' / ('my-app.exe' if sys.platform == 'win32' else 'my-app')).is_file()
563+
assert (build_path / 'app' / ('my-app-0.1.0.exe' if sys.platform == 'win32' else 'my-app-0.1.0')).is_file()
560564

561565
def test_verbosity(self, hatch, helpers, temp_dir, mocker):
562566
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -596,7 +600,7 @@ def test_verbosity(self, hatch, helpers, temp_dir, mocker):
596600
build_artifacts = list(build_path.iterdir())
597601
assert len(build_artifacts) == 1
598602
assert expected_artifact == str(build_artifacts[0])
599-
assert (build_path / 'app' / ('my-app.exe' if sys.platform == 'win32' else 'my-app')).is_file()
603+
assert (build_path / 'app' / ('my-app-0.1.0.exe' if sys.platform == 'win32' else 'my-app-0.1.0')).is_file()
600604

601605
def test_local_build_with_build_target(self, hatch, helpers, temp_dir, mocker):
602606
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -638,7 +642,9 @@ def test_local_build_with_build_target(self, hatch, helpers, temp_dir, mocker):
638642
build_artifacts = list(build_path.iterdir())
639643
assert len(build_artifacts) == 1
640644
assert expected_artifact == str(build_artifacts[0])
641-
assert (build_path / 'app' / ('my-app-target.exe' if sys.platform == 'win32' else 'my-app-target')).is_file()
645+
assert (
646+
build_path / 'app' / ('my-app-0.1.0-target.exe' if sys.platform == 'win32' else 'my-app-0.1.0-target')
647+
).is_file()
642648

643649
def test_local_build_no_build_target(self, hatch, helpers, temp_dir, mocker):
644650
subprocess_run = mocker.patch('subprocess.run', side_effect=cargo_install)
@@ -680,4 +686,4 @@ def test_local_build_no_build_target(self, hatch, helpers, temp_dir, mocker):
680686
build_artifacts = list(build_path.iterdir())
681687
assert len(build_artifacts) == 1
682688
assert expected_artifact == str(build_artifacts[0])
683-
assert (build_path / 'app' / ('my-app.exe' if sys.platform == 'win32' else 'my-app')).is_file()
689+
assert (build_path / 'app' / ('my-app-0.1.0.exe' if sys.platform == 'win32' else 'my-app-0.1.0')).is_file()

0 commit comments

Comments
 (0)