48 lines
1.2 KiB
Python
48 lines
1.2 KiB
Python
|
|
"""
|
||
|
|
Shared version utilities for PlatformIO build scripts.
|
||
|
|
|
||
|
|
Provides functions to get version info including git hash for dev builds.
|
||
|
|
"""
|
||
|
|
|
||
|
|
import configparser
|
||
|
|
import subprocess
|
||
|
|
|
||
|
|
|
||
|
|
def get_git_short_hash():
|
||
|
|
"""Get the short hash of the current HEAD commit."""
|
||
|
|
try:
|
||
|
|
result = subprocess.run(
|
||
|
|
["git", "rev-parse", "--short", "HEAD"],
|
||
|
|
capture_output=True,
|
||
|
|
text=True,
|
||
|
|
check=True,
|
||
|
|
)
|
||
|
|
return result.stdout.strip()
|
||
|
|
except Exception:
|
||
|
|
return None
|
||
|
|
|
||
|
|
|
||
|
|
def get_version(env):
|
||
|
|
"""
|
||
|
|
Get version from platformio.ini, adding -dev@hash suffix for dev builds.
|
||
|
|
|
||
|
|
Args:
|
||
|
|
env: PlatformIO environment object
|
||
|
|
|
||
|
|
Returns:
|
||
|
|
Version string, e.g., "0.15.1" or "0.15.1-dev@7349fbb"
|
||
|
|
"""
|
||
|
|
config = configparser.ConfigParser()
|
||
|
|
config.read("platformio.ini")
|
||
|
|
version = config.get("crosspoint", "version", fallback="unknown")
|
||
|
|
|
||
|
|
# Add -dev@hash suffix for non-release environments (matches platformio.ini build_flags)
|
||
|
|
pio_env = env.get("PIOENV", "default")
|
||
|
|
if pio_env != "gh_release":
|
||
|
|
version += "-dev"
|
||
|
|
git_hash = get_git_short_hash()
|
||
|
|
if git_hash:
|
||
|
|
version += f"@{git_hash}"
|
||
|
|
|
||
|
|
return version
|