Skip to content

Commit 01208d6

Browse files
committed
version support in built app
1 parent 93aa986 commit 01208d6

File tree

4 files changed

+52
-12
lines changed

4 files changed

+52
-12
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,6 @@ slcli/__pycache__/
7777
scripts/__pycache__/
7878
docs/__pycache__/
7979
tests/unit/__pycache__/
80+
slcli/_version.py
8081

8182
# End of .gitignore

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ authors = ["Fred Visser <fred.visser@emerson.com>"]
77

88
[tool.poetry.scripts]
99
slcli = "slcli.__main__:cli"
10+
build-pyinstaller = "scripts.build_pyinstaller:main"
1011

1112
[tool.poetry.dependencies]
1213
python = ">=3.11.1,<3.14"

scripts/build_pyinstaller.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,45 @@
33
import os
44
import subprocess
55
import sys
6+
from pathlib import Path
7+
8+
import tomllib
69

710
# This script is intended to be run as a Poetry script:
811
# > poetry run build-pyinstaller
912

1013

14+
def generate_version_file():
15+
"""Generate _version.py file from pyproject.toml."""
16+
project_root = Path(__file__).parent.parent
17+
pyproject_path = project_root / "pyproject.toml"
18+
version_file_path = project_root / "slcli" / "_version.py"
19+
20+
# Read version from pyproject.toml
21+
with open(pyproject_path, "rb") as f:
22+
pyproject_data = tomllib.load(f)
23+
24+
version = pyproject_data["tool"]["poetry"]["version"]
25+
26+
# Write _version.py file
27+
version_content = f'''"""Version information for slcli."""
28+
29+
# This file is auto-generated during build. Do not edit manually.
30+
__version__ = "{version}"
31+
'''
32+
33+
with open(version_file_path, "w") as f:
34+
f.write(version_content)
35+
36+
print(f"Generated _version.py with version {version}")
37+
return version
38+
39+
1140
def main():
1241
"""Build the slcli binary using PyInstaller."""
42+
# Generate version file first
43+
version = generate_version_file()
44+
1345
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
1446
entry_point = os.path.join(project_root, "slcli", "__main__.py")
1547
cmd = [
@@ -26,7 +58,7 @@ def main():
2658
if result.returncode != 0:
2759
print("PyInstaller build failed.")
2860
sys.exit(result.returncode)
29-
print("PyInstaller build completed.")
61+
print(f"PyInstaller build completed for version {version}.")
3062

3163

3264
if __name__ == "__main__":

slcli/main.py

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@
1414

1515

1616
def get_version() -> str:
17-
"""Get version from pyproject.toml."""
17+
"""Get version from _version.py (built binary) or pyproject.toml (development)."""
1818
try:
19-
# Get the path to pyproject.toml relative to this file
20-
current_dir = Path(__file__).parent
21-
pyproject_path = current_dir.parent / "pyproject.toml"
22-
23-
with open(pyproject_path, "rb") as f:
24-
pyproject_data = tomllib.load(f)
25-
26-
return pyproject_data["tool"]["poetry"]["version"]
27-
except Exception:
28-
return "unknown"
19+
# Try to import from _version.py first (works in built binary)
20+
from ._version import __version__
21+
22+
return __version__
23+
except ImportError:
24+
# Fall back to reading pyproject.toml (works in development)
25+
try:
26+
current_dir = Path(__file__).parent
27+
pyproject_path = current_dir.parent / "pyproject.toml"
28+
29+
with open(pyproject_path, "rb") as f:
30+
pyproject_data = tomllib.load(f)
31+
32+
return pyproject_data["tool"]["poetry"]["version"]
33+
except Exception:
34+
return "unknown"
2935

3036

3137
CONTEXT_SETTINGS = dict(help_option_names=["-h", "--help"])

0 commit comments

Comments
 (0)