|
| 1 | +import sys |
| 2 | +import re |
| 3 | + |
| 4 | +def modernize_pom(file_path, parent_version): |
| 5 | + with open(file_path, 'r') as f: |
| 6 | + lines = f.readlines() |
| 7 | + |
| 8 | + new_lines = [] |
| 9 | + in_parent = False |
| 10 | + in_dep_mgmt = False |
| 11 | + in_dependencies = False |
| 12 | + in_dependency = False |
| 13 | + in_reporting = False |
| 14 | + current_dependency_lines = [] |
| 15 | + has_x_version_update = False |
| 16 | + |
| 17 | + for line in lines: |
| 18 | + # Parent section modernization |
| 19 | + if '<parent>' in line and not in_parent: |
| 20 | + in_parent = True |
| 21 | + indent = line[:line.find('<')] |
| 22 | + new_lines.append(f"{indent}<parent>\n") |
| 23 | + new_lines.append(f"{indent} <groupId>com.google.cloud</groupId>\n") |
| 24 | + new_lines.append(f"{indent} <artifactId>google-cloud-jar-parent</artifactId>\n") |
| 25 | + new_lines.append(f"{indent} <version>{parent_version}</version><!-- {{x-version-update:google-cloud-java:current}} -->\n") |
| 26 | + new_lines.append(f"{indent} <relativePath>../google-cloud-jar-parent/pom.xml</relativePath>\n") |
| 27 | + continue |
| 28 | + if '</parent>' in line and in_parent: |
| 29 | + in_parent = False |
| 30 | + new_lines.append(line) |
| 31 | + continue |
| 32 | + if in_parent: |
| 33 | + continue # skip original parent content |
| 34 | + |
| 35 | + # Dependency Management pruning |
| 36 | + if '<dependencyManagement>' in line: |
| 37 | + in_dep_mgmt = True |
| 38 | + new_lines.append(line) |
| 39 | + continue |
| 40 | + if '</dependencyManagement>' in line: |
| 41 | + in_dep_mgmt = False |
| 42 | + new_lines.append(line) |
| 43 | + continue |
| 44 | + |
| 45 | + if in_dep_mgmt: |
| 46 | + if '<dependencies>' in line: |
| 47 | + in_dependencies = True |
| 48 | + new_lines.append(line) |
| 49 | + continue |
| 50 | + if '</dependencies>' in line: |
| 51 | + in_dependencies = False |
| 52 | + new_lines.append(line) |
| 53 | + continue |
| 54 | + |
| 55 | + if in_dependencies: |
| 56 | + if '<dependency>' in line: |
| 57 | + in_dependency = True |
| 58 | + current_dependency_lines = [line] |
| 59 | + has_x_version_update = False |
| 60 | + continue |
| 61 | + if '</dependency>' in line: |
| 62 | + in_dependency = False |
| 63 | + current_dependency_lines.append(line) |
| 64 | + if has_x_version_update: |
| 65 | + new_lines.extend(current_dependency_lines) |
| 66 | + continue |
| 67 | + |
| 68 | + if in_dependency: |
| 69 | + current_dependency_lines.append(line) |
| 70 | + if '{x-version-update:' in line: |
| 71 | + has_x_version_update = True |
| 72 | + continue |
| 73 | + |
| 74 | + # Prune comments and extra whitespace in depMgmt for a cleaner result |
| 75 | + if not line.strip(): |
| 76 | + new_lines.append(line) |
| 77 | + continue |
| 78 | + |
| 79 | + # Reporting section removal |
| 80 | + if '<reporting>' in line: |
| 81 | + in_reporting = True |
| 82 | + continue |
| 83 | + if '</reporting>' in line: |
| 84 | + in_reporting = False |
| 85 | + continue |
| 86 | + if in_reporting: |
| 87 | + continue |
| 88 | + |
| 89 | + new_lines.append(line) |
| 90 | + |
| 91 | + with open(file_path, 'w') as f: |
| 92 | + # Clean up double empty lines potentially introduced by pruning |
| 93 | + content = "".join(new_lines) |
| 94 | + content = re.sub(r'\n\s*\n\s*\n', '\n\n', content) |
| 95 | + f.write(content) |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + if len(sys.argv) > 2: |
| 99 | + modernize_pom(sys.argv[1], sys.argv[2]) |
| 100 | + else: |
| 101 | + print("Usage: python3 modernize_pom.py <file_path> <parent_version>") |
| 102 | + sys.exit(1) |
0 commit comments