|
| 1 | +import subprocess |
| 2 | +import os |
| 3 | +import re |
| 4 | +import sys |
| 5 | +from datetime import datetime, timezone |
| 6 | + |
| 7 | +Import("env") |
| 8 | + |
| 9 | + |
| 10 | +def do_main(): |
| 11 | + # hash |
| 12 | + ret = subprocess.run(["git", "rev-parse", "HEAD"], stdout=subprocess.PIPE, text=True, check=False) # Uses any tags |
| 13 | + full_hash = ret.stdout.strip() |
| 14 | + short_hash = full_hash[:7] |
| 15 | + |
| 16 | + # branch |
| 17 | + ref_name = os.environ.get("REF_NAME") |
| 18 | + if ref_name: |
| 19 | + branch = ref_name |
| 20 | + else: |
| 21 | + ret = subprocess.run( |
| 22 | + ["git", "symbolic-ref", "--short", "HEAD"], |
| 23 | + stdout=subprocess.PIPE, |
| 24 | + text=True, |
| 25 | + check=False, |
| 26 | + ) # retrieve branch name |
| 27 | + branch = ret.stdout.strip() |
| 28 | + branch = branch.replace("/", "") |
| 29 | + branch = branch.replace("-", "") |
| 30 | + branch = branch.replace("_", "") |
| 31 | + |
| 32 | + if branch == "": |
| 33 | + raise Exception("No branch name found") |
| 34 | + |
| 35 | + # is_tag ? |
| 36 | + tagPattern = re.compile("^v[0-9]+.[0-9]+.[0-9]+([_-][a-zA-Z0-9]+)?$") |
| 37 | + is_tag = branch.startswith("v") and len(branch) >= 6 and tagPattern.match(branch) |
| 38 | + |
| 39 | + version = branch |
| 40 | + if not is_tag: |
| 41 | + version += "_" + short_hash |
| 42 | + |
| 43 | + # local modifications ? |
| 44 | + has_local_modifications = False |
| 45 | + if not ref_name: |
| 46 | + # Check if the source has been modified since the last commit |
| 47 | + ret = subprocess.run( |
| 48 | + ["git", "diff-index", "--quiet", "HEAD", "--"], |
| 49 | + stdout=subprocess.PIPE, |
| 50 | + text=True, |
| 51 | + check=False, |
| 52 | + ) |
| 53 | + has_local_modifications = ret.returncode != 0 |
| 54 | + |
| 55 | + if has_local_modifications: |
| 56 | + version += "_modified" |
| 57 | + |
| 58 | + # version = "v2.40.2-rc1" |
| 59 | + constantFile = os.path.join(env.subst("$BUILD_DIR"), "__compiled_constants.c") |
| 60 | + with open(constantFile, "w") as f: |
| 61 | + f.write( |
| 62 | + f'const char* __COMPILED_APP_VERSION__ = "{version[1:] if tagPattern.match(version) else version}";\n' |
| 63 | + f'const char* __COMPILED_BUILD_BRANCH__ = "{branch}";\n' |
| 64 | + f'const char* __COMPILED_BUILD_HASH__ = "{short_hash}";\n' |
| 65 | + f'const char* __COMPILED_BUILD_NAME__ = "{env["PIOENV"]}";\n' |
| 66 | + f'const char* __COMPILED_BUILD_TIMESTAMP__ = "{datetime.now(timezone.utc).isoformat()}";\n' |
| 67 | + f'const char* __COMPILED_BUILD_BOARD__ = "{env.get("BOARD")}";\n' |
| 68 | + ) |
| 69 | + sys.stderr.write(f"version.py: APP_VERSION: {version[1:] if tagPattern.match(version) else version}\n") |
| 70 | + sys.stderr.write(f"version.py: BUILD_BRANCH: {branch}\n") |
| 71 | + sys.stderr.write(f"version.py: BUILD_HASH: {short_hash}\n") |
| 72 | + sys.stderr.write(f"version.py: BUILD_NAME: {env['PIOENV']}\n") |
| 73 | + sys.stderr.write(f"version.py: BUILD_TIMESTAMP: {datetime.now(timezone.utc).isoformat()}\n") |
| 74 | + sys.stderr.write(f"version.py: BUILD_BOARD: {env.get('BOARD')}\n") |
| 75 | + |
| 76 | + env.AppendUnique(PIOBUILDFILES=[constantFile]) |
| 77 | + |
| 78 | + |
| 79 | +do_main() |
0 commit comments