|
| 1 | +import os |
| 2 | +import re |
| 3 | +import sys |
| 4 | +import json |
| 5 | +import argparse |
| 6 | + |
| 7 | +def update_csproj_file(path, version): |
| 8 | + with open(path, 'r', encoding='utf-8') as f: |
| 9 | + lines = f.readlines() |
| 10 | + |
| 11 | + updated_lines = [] |
| 12 | + version_prefix = '.'.join(version.split('.')[:2]) + '.*' |
| 13 | + version_tag_written = set() |
| 14 | + |
| 15 | + for i, line in enumerate(lines): |
| 16 | + # Update PackageReference for SpacetimeDB.* |
| 17 | + updated_line = re.sub( |
| 18 | + r'(<PackageReference[^>]*Include="SpacetimeDB\.[^"]*"[^>]*Version=")[^"]*(")', |
| 19 | + rf'\g<1>{version_prefix}\g<2>', |
| 20 | + line |
| 21 | + ) |
| 22 | + |
| 23 | + for tag in ('Version', 'AssemblyVersion'): |
| 24 | + if re.search(rf'<{tag}>.*</{tag}>', updated_line.strip()): |
| 25 | + updated_line = re.sub( |
| 26 | + rf'(<{tag}>).*(</{tag}>)', |
| 27 | + rf'\g<1>{version}\g<2>', |
| 28 | + updated_line |
| 29 | + ) |
| 30 | + version_tag_written.add(tag) |
| 31 | + |
| 32 | + updated_lines.append(updated_line) |
| 33 | + |
| 34 | + with open(path, 'w', encoding='utf-8') as f: |
| 35 | + f.writelines(updated_lines) |
| 36 | + print(f"Updated: {path}") |
| 37 | + |
| 38 | +def update_all_csproj_files(version): |
| 39 | + for root, _, files in os.walk('.'): |
| 40 | + for file in files: |
| 41 | + if file.endswith('.csproj'): |
| 42 | + update_csproj_file(os.path.join(root, file), version) |
| 43 | + |
| 44 | +def update_package_json(version): |
| 45 | + path = 'package.json' |
| 46 | + if os.path.exists(path): |
| 47 | + with open(path, 'r', encoding='utf-8') as f: |
| 48 | + data = json.load(f) |
| 49 | + data['version'] = version |
| 50 | + with open(path, 'w', encoding='utf-8') as f: |
| 51 | + json.dump(data, f, indent=2) |
| 52 | + f.write('\n') # ensure trailing newline |
| 53 | + print(f"Updated version in {path} to {version}") |
| 54 | + else: |
| 55 | + print("package.json not found.") |
| 56 | + |
| 57 | +def main(): |
| 58 | + parser = argparse.ArgumentParser( |
| 59 | + description='Update version numbers in .csproj files and package.json', |
| 60 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 61 | + epilog=""" |
| 62 | +Examples: |
| 63 | + python upgrade-version.py 1.2.3 |
| 64 | + python upgrade-version.py --version 1.2.3 |
| 65 | + """ |
| 66 | + ) |
| 67 | + parser.add_argument( |
| 68 | + 'version', |
| 69 | + help='Version number to set (e.g., 1.2.3)' |
| 70 | + ) |
| 71 | + |
| 72 | + args = parser.parse_args() |
| 73 | + |
| 74 | + update_all_csproj_files(args.version) |
| 75 | + update_package_json(args.version) |
| 76 | + |
| 77 | +if __name__ == '__main__': |
| 78 | + main() |
0 commit comments