|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +import sys |
| 17 | +import os |
| 18 | +import re |
| 19 | + |
| 20 | +def update_dependency_management(pom_path, group_id, artifact_id, version): |
| 21 | + with open(pom_path, 'r') as f: |
| 22 | + content = f.read() |
| 23 | + |
| 24 | + # Find <dependencyManagement> |
| 25 | + dm_match = re.search(r'<dependencyManagement>.*?</dependencyManagement>', content, re.DOTALL) |
| 26 | + if not dm_match: |
| 27 | + print(f"Error: <dependencyManagement> section not found in {pom_path}") |
| 28 | + sys.exit(1) |
| 29 | + |
| 30 | + dm_content = dm_match.group(0) |
| 31 | + |
| 32 | + # Find <dependencies> inside <dependencyManagement> |
| 33 | + deps_match = re.search(r'<dependencies>(.*?)</dependencies>', dm_content, re.DOTALL) |
| 34 | + if not deps_match: |
| 35 | + print(f"Error: <dependencies> section not found inside <dependencyManagement> in {pom_path}") |
| 36 | + sys.exit(1) |
| 37 | + |
| 38 | + inner_deps_content = deps_match.group(1) |
| 39 | + |
| 40 | + # Check if dependency already exists |
| 41 | + dep_pattern = fr'<artifactId>{re.escape(artifact_id)}</artifactId>' |
| 42 | + if dep_pattern in inner_deps_content: |
| 43 | + # Check if version matches |
| 44 | + if fr'<version>{re.escape(version)}</version>' in inner_deps_content: |
| 45 | + print(f"Dependency {group_id}:{artifact_id}:{version} already exists in {pom_path}") |
| 46 | + return |
| 47 | + else: |
| 48 | + # Update version? Requirement says "move it", but if it exists with different version, maybe we should update it? |
| 49 | + # For now, let's just append if it's not EXACTLY same. |
| 50 | + # Actually, usually there should be only one. |
| 51 | + # Let's replace if artifactId matches but version is different. |
| 52 | + |
| 53 | + new_inner_deps = re.sub( |
| 54 | + fr'(<dependency>\s*<groupId>{re.escape(group_id)}</groupId>\s*<artifactId>{re.escape(artifact_id)}</artifactId>\s*<version>).*?(</version>.*?</dependency>)', |
| 55 | + fr'\g<1>{version}\g<2>', |
| 56 | + inner_deps_content, |
| 57 | + flags=re.DOTALL |
| 58 | + ) |
| 59 | + if new_inner_deps != inner_deps_content: |
| 60 | + print(f"Updated {group_id}:{artifact_id} to version {version} in {pom_path}") |
| 61 | + new_dm_content = dm_content.replace(inner_deps_content, new_inner_deps) |
| 62 | + new_content = content.replace(dm_content, new_dm_content) |
| 63 | + with open(pom_path, 'w') as f: |
| 64 | + f.write(new_content) |
| 65 | + return |
| 66 | + |
| 67 | + # If not found or not updated, append it |
| 68 | + new_dep = f""" <dependency> |
| 69 | + <groupId>{group_id}</groupId> |
| 70 | + <artifactId>{artifact_id}</artifactId> |
| 71 | + <version>{version}</version> |
| 72 | + </dependency> |
| 73 | +""" |
| 74 | + # Find the last </dependency> and insert after it, or just before </dependencies> |
| 75 | + last_dep_end = inner_deps_content.rfind('</dependency>') |
| 76 | + if last_dep_end != -1: |
| 77 | + # Check if there is anything after last </dependency> that is not just whitespace |
| 78 | + insert_pos = last_dep_end + len('</dependency>') |
| 79 | + # Ensure we insert before </dependencies> |
| 80 | + new_inner_deps = inner_deps_content[:insert_pos] + "\n" + new_dep + inner_deps_content[insert_pos:] |
| 81 | + else: |
| 82 | + # No dependencies found, just insert at beginning |
| 83 | + new_inner_deps = "\n" + new_dep + inner_deps_content |
| 84 | + |
| 85 | + new_dm_content = dm_content.replace(inner_deps_content, new_inner_deps) |
| 86 | + new_content = content.replace(dm_content, new_dm_content) |
| 87 | + |
| 88 | + with open(pom_path, 'w') as f: |
| 89 | + f.write(new_content) |
| 90 | + print(f"Added {group_id}:{artifact_id}:{version} to {pom_path}") |
| 91 | + |
| 92 | +if __name__ == "__main__": |
| 93 | + if len(sys.argv) != 5: |
| 94 | + print("Usage: update_dependency_management.py <pom_path> <groupId> <artifactId> <version>") |
| 95 | + sys.exit(1) |
| 96 | + update_dependency_management(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4]) |
0 commit comments