|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Simple script for updating CURRENT_PROJECT_VERSION (build number) in Xcode project file. |
| 4 | +Can either increment the current build number or set it to a specific value. |
| 5 | +""" |
| 6 | + |
| 7 | +import argparse |
| 8 | +import os |
| 9 | +import re |
| 10 | +import sys |
| 11 | + |
| 12 | + |
| 13 | +def update_build_number(pbxproj_path, config_name, build_number=None): |
| 14 | + """ |
| 15 | + Update CURRENT_PROJECT_VERSION in specific build configuration. |
| 16 | +
|
| 17 | + Args: |
| 18 | + pbxproj_path: Path to project.pbxproj file |
| 19 | + config_name: Configuration name (e.g., 'Release', 'DevCI') |
| 20 | + build_number: If provided, set to this value. If None, increment current value. |
| 21 | +
|
| 22 | + Returns: |
| 23 | + Tuple of (success: bool, new_build_number: int) |
| 24 | + """ |
| 25 | + if not os.path.isfile(pbxproj_path): |
| 26 | + raise FileNotFoundError(f"Xcode project file not found at {pbxproj_path}") |
| 27 | + |
| 28 | + with open(pbxproj_path, "r", encoding="utf-8", errors="ignore") as f: |
| 29 | + content = f.read() |
| 30 | + |
| 31 | + # Find configuration block by name |
| 32 | + # Pattern: <config_id> /* <config_name> */ = { ... }; |
| 33 | + config_pattern = ( |
| 34 | + rf"(\w{{24}}\s*/\*\s*{re.escape(config_name)}\s*\*/\s*=\s*\{{[^}}]*?\}})" |
| 35 | + ) |
| 36 | + config_match = re.search(config_pattern, content, re.DOTALL) |
| 37 | + |
| 38 | + if not config_match: |
| 39 | + raise ValueError(f"Could not find configuration block for '{config_name}'") |
| 40 | + |
| 41 | + # Find current build number in the config block |
| 42 | + config_block = config_match.group(0) |
| 43 | + build_match = re.search(r"CURRENT_PROJECT_VERSION\s*=\s*(\d+);", config_block) |
| 44 | + |
| 45 | + if not build_match: |
| 46 | + raise ValueError( |
| 47 | + f"Could not find CURRENT_PROJECT_VERSION in {config_name} configuration" |
| 48 | + ) |
| 49 | + |
| 50 | + current_build = int(build_match.group(1)) |
| 51 | + |
| 52 | + if build_number is not None: |
| 53 | + # Set to specific value |
| 54 | + new_build = build_number |
| 55 | + action = "Set" |
| 56 | + else: |
| 57 | + # Increment current value |
| 58 | + new_build = current_build + 1 |
| 59 | + action = "Incremented" |
| 60 | + |
| 61 | + # Update build number in the configuration block |
| 62 | + def update_block(match): |
| 63 | + block = match.group(0) |
| 64 | + block = re.sub( |
| 65 | + r"(CURRENT_PROJECT_VERSION\s*=\s*)\d+;", rf"\g<1>{new_build};", block |
| 66 | + ) |
| 67 | + return block |
| 68 | + |
| 69 | + new_content = re.sub(config_pattern, update_block, content, flags=re.DOTALL) |
| 70 | + |
| 71 | + with open(pbxproj_path, "w", encoding="utf-8") as f: |
| 72 | + f.write(new_content) |
| 73 | + |
| 74 | + print( |
| 75 | + f"{action} CURRENT_PROJECT_VERSION: {current_build} -> {new_build} in {config_name} configuration" |
| 76 | + ) |
| 77 | + return True, new_build |
| 78 | + |
| 79 | + |
| 80 | +def main(): |
| 81 | + parser = argparse.ArgumentParser( |
| 82 | + description="Update CURRENT_PROJECT_VERSION in Xcode project" |
| 83 | + ) |
| 84 | + parser.add_argument("pbxproj", help="Path to project.pbxproj file") |
| 85 | + parser.add_argument( |
| 86 | + "--config-name", |
| 87 | + required=True, |
| 88 | + help="Configuration name (e.g., 'Release', 'DevCI')", |
| 89 | + ) |
| 90 | + parser.add_argument( |
| 91 | + "--build-number", |
| 92 | + type=int, |
| 93 | + help="Set build number to this value (if not provided, will increment current value)", |
| 94 | + ) |
| 95 | + parser.add_argument( |
| 96 | + "--output-github", |
| 97 | + action="store_true", |
| 98 | + help="Output in GitHub Actions format to GITHUB_OUTPUT", |
| 99 | + ) |
| 100 | + |
| 101 | + args = parser.parse_args() |
| 102 | + |
| 103 | + try: |
| 104 | + success, new_build = update_build_number( |
| 105 | + args.pbxproj, args.config_name, args.build_number |
| 106 | + ) |
| 107 | + |
| 108 | + if args.output_github and os.environ.get("GITHUB_OUTPUT"): |
| 109 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 110 | + f.write(f"new_build={new_build}\n") |
| 111 | + f.write(f"build_updated=true\n") |
| 112 | + |
| 113 | + except Exception as e: |
| 114 | + print(f"Error: {e}", file=sys.stderr) |
| 115 | + sys.exit(1) |
| 116 | + |
| 117 | + |
| 118 | +if __name__ == "__main__": |
| 119 | + main() |
0 commit comments