|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Update version in cf_cli_java_plugin.go for releases. |
| 4 | +This script updates the PluginMetadata version in the Go source code and processes the changelog. |
| 5 | +""" |
| 6 | + |
| 7 | +import sys |
| 8 | +import re |
| 9 | +from pathlib import Path |
| 10 | + |
| 11 | +def update_version_in_go_file(file_path, major, minor, build): |
| 12 | + """Update the version in the Go plugin metadata.""" |
| 13 | + with open(file_path, 'r') as f: |
| 14 | + content = f.read() |
| 15 | + |
| 16 | + # Pattern to match the Version struct in PluginMetadata |
| 17 | + pattern = r'(Version: plugin\.VersionType\s*{\s*Major:\s*)\d+(\s*,\s*Minor:\s*)\d+(\s*,\s*Build:\s*)\d+(\s*,\s*})' |
| 18 | + |
| 19 | + replacement = rf'\g<1>{major}\g<2>{minor}\g<3>{build}\g<4>' |
| 20 | + |
| 21 | + new_content = re.sub(pattern, replacement, content) |
| 22 | + |
| 23 | + if new_content == content: |
| 24 | + print(f"Warning: Version pattern not found or not updated in {file_path}") |
| 25 | + return False |
| 26 | + |
| 27 | + with open(file_path, 'w') as f: |
| 28 | + f.write(new_content) |
| 29 | + |
| 30 | + print(f"✅ Updated version to {major}.{minor}.{build} in {file_path}") |
| 31 | + return True |
| 32 | + |
| 33 | +def process_readme_changelog(readme_path, version): |
| 34 | + """Process the README changelog section for the release.""" |
| 35 | + with open(readme_path, 'r') as f: |
| 36 | + content = f.read() |
| 37 | + |
| 38 | + # Look for the snapshot section |
| 39 | + snapshot_pattern = rf'## {re.escape(version)}-snapshot\s*\n' |
| 40 | + match = re.search(snapshot_pattern, content) |
| 41 | + |
| 42 | + if not match: |
| 43 | + print(f"Error: README.md does not contain a '## {version}-snapshot' section") |
| 44 | + return False, None |
| 45 | + |
| 46 | + # Find the content of the snapshot section |
| 47 | + start_pos = match.end() |
| 48 | + |
| 49 | + # Find the next ## section or end of file |
| 50 | + next_section_pattern = r'\n## ' |
| 51 | + next_match = re.search(next_section_pattern, content[start_pos:]) |
| 52 | + |
| 53 | + if next_match: |
| 54 | + end_pos = start_pos + next_match.start() |
| 55 | + section_content = content[start_pos:end_pos].strip() |
| 56 | + else: |
| 57 | + section_content = content[start_pos:].strip() |
| 58 | + |
| 59 | + # Remove the "-snapshot" from the header |
| 60 | + new_header = f"## {version}" |
| 61 | + updated_content = re.sub(snapshot_pattern, new_header + '\n\n', content) |
| 62 | + |
| 63 | + # Write the updated README |
| 64 | + with open(readme_path, 'w') as f: |
| 65 | + f.write(updated_content) |
| 66 | + |
| 67 | + print(f"✅ Updated README.md: converted '## {version}-snapshot' to '## {version}'") |
| 68 | + return True, section_content |
| 69 | + |
| 70 | +def get_base_version(version): |
| 71 | + """Return the base version (e.g., 4.0.0 from 4.0.0-rc2)""" |
| 72 | + return version.split('-')[0] |
| 73 | + |
| 74 | +def is_rc_version(version_str): |
| 75 | + """Return True if the version string ends with -rc or -rcN.""" |
| 76 | + return bool(re.match(r"^\d+\.\d+\.\d+-rc(\d+)?$", version_str)) |
| 77 | + |
| 78 | +def main(): |
| 79 | + if len(sys.argv) != 4: |
| 80 | + print("Usage: update_version.py <major> <minor> <build>") |
| 81 | + print("Example: update_version.py 4 1 0") |
| 82 | + sys.exit(1) |
| 83 | + |
| 84 | + try: |
| 85 | + major = int(sys.argv[1]) |
| 86 | + minor = int(sys.argv[2]) |
| 87 | + build = int(sys.argv[3]) |
| 88 | + except ValueError: |
| 89 | + print("Error: Version numbers must be integers") |
| 90 | + sys.exit(1) |
| 91 | + |
| 92 | + version = f"{major}.{minor}.{build}" |
| 93 | + version_arg = f"{major}.{minor}.{build}" if (major + minor + build) != 0 else sys.argv[1] |
| 94 | + # Accept any -rc suffix, e.g. 4.0.0-rc, 4.0.0-rc1, 4.0.0-rc2 |
| 95 | + if is_rc_version(sys.argv[1]): |
| 96 | + base_version = get_base_version(sys.argv[1]) |
| 97 | + go_file = Path("cf_cli_java_plugin.go") |
| 98 | + readme_file = Path("README.md") |
| 99 | + changelog_file = Path("release_changelog.txt") |
| 100 | + if not readme_file.exists(): |
| 101 | + print(f"Error: {readme_file} not found") |
| 102 | + sys.exit(1) |
| 103 | + with open(readme_file, 'r') as f: |
| 104 | + content = f.read() |
| 105 | + # Find the section for the base version |
| 106 | + base_pattern = rf'## {re.escape(base_version)}\s*\n' |
| 107 | + match = re.search(base_pattern, content) |
| 108 | + if not match: |
| 109 | + print(f"Error: README.md does not contain a '## {base_version}' section for RC release") |
| 110 | + sys.exit(1) |
| 111 | + start_pos = match.end() |
| 112 | + next_match = re.search(r'\n## ', content[start_pos:]) |
| 113 | + if next_match: |
| 114 | + end_pos = start_pos + next_match.start() |
| 115 | + section_content = content[start_pos:end_pos].strip() |
| 116 | + else: |
| 117 | + section_content = content[start_pos:].strip() |
| 118 | + with open(changelog_file, 'w') as f: |
| 119 | + f.write(section_content) |
| 120 | + print(f"✅ RC release: Changelog for {base_version} saved to {changelog_file}") |
| 121 | + sys.exit(0) |
| 122 | + |
| 123 | + go_file = Path("cf_cli_java_plugin.go") |
| 124 | + readme_file = Path("README.md") |
| 125 | + changelog_file = Path("release_changelog.txt") |
| 126 | + |
| 127 | + # Update Go version |
| 128 | + success = update_version_in_go_file(go_file, major, minor, build) |
| 129 | + if not success: |
| 130 | + sys.exit(1) |
| 131 | + |
| 132 | + # Process README changelog |
| 133 | + success, changelog_content = process_readme_changelog(readme_file, version) |
| 134 | + if not success: |
| 135 | + sys.exit(1) |
| 136 | + |
| 137 | + # Write changelog content to a file for the workflow to use |
| 138 | + with open(changelog_file, 'w') as f: |
| 139 | + f.write(changelog_content) |
| 140 | + |
| 141 | + print(f"✅ Version updated successfully to {version}") |
| 142 | + print(f"✅ Changelog content saved to {changelog_file}") |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + main() |
0 commit comments