|
| 1 | +import argparse |
| 2 | +import json |
| 3 | +import os |
| 4 | +import re |
| 5 | +import sys |
| 6 | + |
| 7 | + |
| 8 | +def run(new_checksum: str = None, new_tag: str = None): |
| 9 | + if new_checksum is None and new_tag is None: |
| 10 | + print("At least one of --checksum or --tag arguments must be provided.", file=sys.stderr) |
| 11 | + sys.exit(1) |
| 12 | + |
| 13 | + if new_checksum is not None: |
| 14 | + if not new_checksum.isalnum(): |
| 15 | + print("Checksum must be alphanumeric.", file=sys.stderr) |
| 16 | + sys.exit(1) |
| 17 | + |
| 18 | + if not new_checksum.islower(): |
| 19 | + print("Checksum must be lowercase.", file=sys.stderr) |
| 20 | + sys.exit(1) |
| 21 | + |
| 22 | + try: |
| 23 | + int(new_checksum, 16) |
| 24 | + except ValueError: |
| 25 | + print("Checksum must be hexadecimal.", file=sys.stderr) |
| 26 | + sys.exit(1) |
| 27 | + |
| 28 | + if new_tag is not None: |
| 29 | + if new_tag.strip() != new_tag: |
| 30 | + print("Tag must not contain any whitespace.", file=sys.stderr) |
| 31 | + sys.exit(1) |
| 32 | + |
| 33 | + tag_regex = re.compile(r"^v?\d+[.]\d+[.]\d+$") |
| 34 | + tag_match = tag_regex.match(new_tag) |
| 35 | + if tag_match is None: |
| 36 | + print("Tag must adhere to vX.Y.Z or X.Y.Z format.", file=sys.stderr) |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | + settings = [ |
| 40 | + {"variable_name": "checksum", "value": new_checksum}, |
| 41 | + {"variable_name": "tag", "value": new_tag}, |
| 42 | + ] |
| 43 | + |
| 44 | + package_file_path = os.path.realpath( |
| 45 | + os.path.join(os.path.dirname(__file__), "../Package.swift") |
| 46 | + ) |
| 47 | + |
| 48 | + try: |
| 49 | + with open(package_file_path, "r") as package_file_handle: |
| 50 | + package_file = package_file_handle.read() |
| 51 | + except OSError: |
| 52 | + print("Failed to read Package.swift file.", file=sys.stderr) |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + updated_package_file = package_file |
| 56 | + for current_setting in settings: |
| 57 | + current_variable_name = current_setting["variable_name"] |
| 58 | + new_value = current_setting["value"] |
| 59 | + if new_value is None: |
| 60 | + continue |
| 61 | + |
| 62 | + print(f"setting {current_variable_name} (JSON-serialization):") |
| 63 | + print(json.dumps(new_value)) |
| 64 | + |
| 65 | + regex = re.compile(rf"(let\s+{current_variable_name}\s*=\s*)(.*)") |
| 66 | + match = regex.search(updated_package_file) |
| 67 | + if match is None: |
| 68 | + print( |
| 69 | + f"Failed to find {current_variable_name} in Package.swift.", |
| 70 | + file=sys.stderr, |
| 71 | + ) |
| 72 | + sys.exit(1) |
| 73 | + |
| 74 | + previous_value = match.group(2) |
| 75 | + updated_package_file = updated_package_file.replace(previous_value, f'"{new_value}"') |
| 76 | + |
| 77 | + with open(package_file_path, "w") as package_file_handle: |
| 78 | + package_file_handle.write(updated_package_file) |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + parser = argparse.ArgumentParser( |
| 83 | + description="Update Swift package checksum and tag." |
| 84 | + ) |
| 85 | + parser.add_argument( |
| 86 | + "--checksum", |
| 87 | + type=str, |
| 88 | + help="new checksum of cktapFFI.xcframework.zip", |
| 89 | + required=False, |
| 90 | + default=None, |
| 91 | + ) |
| 92 | + parser.add_argument( |
| 93 | + "--tag", |
| 94 | + type=str, |
| 95 | + help="new release tag (vX.Y.Z or X.Y.Z)", |
| 96 | + required=False, |
| 97 | + default=None, |
| 98 | + ) |
| 99 | + args = parser.parse_args() |
| 100 | + run(new_checksum=args.checksum, new_tag=args.tag) |
0 commit comments