|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import json |
| 5 | +import os |
| 6 | +import re |
| 7 | +import sys |
| 8 | + |
| 9 | + |
| 10 | +def run(new_checksum: str = None, new_tag: str = None): |
| 11 | + if new_checksum is None and new_tag is None: |
| 12 | + print('At least one of --checksum or --tag arguments must be provided.', file=sys.stderr) |
| 13 | + sys.exit(1) |
| 14 | + |
| 15 | + if new_checksum is not None: |
| 16 | + if not new_checksum.isalnum(): |
| 17 | + print('Checksum must be alphanumeric.', file=sys.stderr) |
| 18 | + sys.exit(1) |
| 19 | + |
| 20 | + if not new_checksum.islower(): |
| 21 | + print('Checksum must be lowercase.', file=sys.stderr) |
| 22 | + sys.exit(1) |
| 23 | + |
| 24 | + try: |
| 25 | + int(new_checksum, 16) |
| 26 | + except: |
| 27 | + print('Checksum must be hexadecimal.', file=sys.stderr) |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + if new_tag is not None: |
| 31 | + if new_tag.strip() != new_tag: |
| 32 | + print('Tag must not contain any whitespace.', file=sys.stderr) |
| 33 | + sys.exit(1) |
| 34 | + |
| 35 | + # Support both v0.3.1 and 0.3.1 formats |
| 36 | + tag_regex = re.compile("^v?\d+[.]\d+[.]\d+$") |
| 37 | + tag_match = tag_regex.match(new_tag) |
| 38 | + if tag_match is None: |
| 39 | + print('Tag must adhere to x.x.x or vx.x.x major/minor/patch format.', file=sys.stderr) |
| 40 | + sys.exit(1) |
| 41 | + |
| 42 | + settings = [ |
| 43 | + {'variable_name': 'checksum', 'value': new_checksum}, |
| 44 | + {'variable_name': 'tag', 'value': new_tag}, |
| 45 | + ] |
| 46 | + |
| 47 | + # Get the Package.swift path relative to this script |
| 48 | + script_dir = os.path.dirname(os.path.realpath(__file__)) |
| 49 | + package_file_path = os.path.join(script_dir, 'Package.swift') |
| 50 | + |
| 51 | + print(f'Updating: {package_file_path}') |
| 52 | + |
| 53 | + original_package_file = None |
| 54 | + try: |
| 55 | + with open(package_file_path, 'r') as package_file_handle: |
| 56 | + original_package_file = package_file_handle.read() |
| 57 | + except Exception as e: |
| 58 | + print(f'Failed to read Package.swift file: {e}', file=sys.stderr) |
| 59 | + sys.exit(1) |
| 60 | + |
| 61 | + package_file = original_package_file |
| 62 | + for current_setting in settings: |
| 63 | + current_variable_name = current_setting['variable_name'] |
| 64 | + new_value = current_setting['value'] |
| 65 | + if new_value is None: |
| 66 | + continue |
| 67 | + |
| 68 | + print(f'Setting {current_variable_name}: {new_value}') |
| 69 | + |
| 70 | + # Create regex pattern to match the let declaration |
| 71 | + regex = re.compile(f'(let[\s]+{current_variable_name}[\s]*=[\s]*)"([^"]*)"') |
| 72 | + |
| 73 | + # Find and replace the value |
| 74 | + match = regex.search(package_file) |
| 75 | + if match: |
| 76 | + old_value = match.group(2) |
| 77 | + package_file = regex.sub(f'\\1"{new_value}"', package_file) |
| 78 | + print(f' Changed from: {old_value}') |
| 79 | + print(f' Changed to: {new_value}') |
| 80 | + else: |
| 81 | + print(f' Warning: Could not find {current_variable_name} in Package.swift', file=sys.stderr) |
| 82 | + |
| 83 | + # Write the updated file |
| 84 | + try: |
| 85 | + with open(package_file_path, "w") as f: |
| 86 | + f.write(package_file) |
| 87 | + print('Successfully updated Package.swift') |
| 88 | + except Exception as e: |
| 89 | + print(f'Failed to write Package.swift file: {e}', file=sys.stderr) |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + |
| 93 | +if __name__ == '__main__': |
| 94 | + parser = argparse.ArgumentParser(description='Update Package.swift with new checksum and/or tag') |
| 95 | + parser.add_argument('--checksum', type=str, help='new checksum of VssRustClientFfi.xcframework.zip', required=False, default=None) |
| 96 | + parser.add_argument('--tag', type=str, help='new release tag (e.g., v0.3.1)', required=False, default=None) |
| 97 | + args = parser.parse_args() |
| 98 | + run(new_checksum=args.checksum, new_tag=args.tag) |
0 commit comments