|
| 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: |
| 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 | + |
| 32 | + tag_regex = re.compile("^\d+[.]\d+[.]\d+$") |
| 33 | + tag_match = tag_regex.match(new_tag) |
| 34 | + if tag_match is None: |
| 35 | + print('Tag must adhere to x.x.x major/minor/patch format.', file=sys.stderr) |
| 36 | + |
| 37 | + settings = [ |
| 38 | + {'variable_name': 'checksum', 'value': new_checksum}, |
| 39 | + {'variable_name': 'tag', 'value': new_tag}, |
| 40 | + ] |
| 41 | + |
| 42 | + package_file_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '../Package.swift')) |
| 43 | + print(package_file_path) |
| 44 | + |
| 45 | + |
| 46 | + |
| 47 | + original_package_file = None |
| 48 | + try: |
| 49 | + with open(package_file_path, 'r') as package_file_handle: |
| 50 | + original_package_file = package_file_handle.read() |
| 51 | + except: |
| 52 | + print('Failed to read Package.swift file.', file=sys.stderr) |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + package_file = original_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(f'(let[\s]+{current_variable_name}[\s]*=[\s]*)(.*)') |
| 66 | + |
| 67 | + previous_value = regex.search(package_file).group(2) |
| 68 | + # new_package_file = checksum_regex.sub(f'\g<1>"{new_checksum}"', package_file) |
| 69 | + package_file = package_file.replace(previous_value, f'"{new_value}"') |
| 70 | + |
| 71 | + with open(package_file_path, "w") as f: |
| 72 | + f.write(package_file) |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + parser = argparse.ArgumentParser(description='Process some integers.') |
| 78 | + parser.add_argument('--checksum', type=str, help='new checksum of LDKNode.xcframework.zip', required=False, default=None) |
| 79 | + parser.add_argument('--tag', type=str, help='new release tag', required=False, default=None) |
| 80 | + args = parser.parse_args() |
| 81 | + run(new_checksum=args.checksum, new_tag=args.tag) |
0 commit comments