Skip to content

Commit bf352f2

Browse files
committed
refactor: extract inline python to a .py file
1 parent f7232a8 commit bf352f2

File tree

2 files changed

+45
-32
lines changed

2 files changed

+45
-32
lines changed

monorepo-migration/migrate.sh

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ TARGET_DIR="$WORKING_DIR/$MONOREPO_NAME-target"
3434
TRANSFORM_SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
3535
TRANSFORM_SCRIPT="$TRANSFORM_SCRIPT_DIR/transform_workflow.py"
3636
MODERNIZE_POM_SCRIPT="$TRANSFORM_SCRIPT_DIR/modernize_pom.py"
37+
UPDATE_ROOT_POM_SCRIPT="$TRANSFORM_SCRIPT_DIR/update_root_pom.py"
3738

3839
echo "Starting migration using git read-tree with isolated clones..."
3940

@@ -140,38 +141,7 @@ fi
140141

141142
# 7.2 Update root pom.xml modules
142143
echo "Updating root pom.xml modules..."
143-
python3 -c "
144-
import sys
145-
pom_path = sys.argv[1]
146-
module_name = sys.argv[2]
147-
new_module = f' <module>{module_name}</module>\n'
148-
with open(pom_path, 'r') as f:
149-
content = f.read()
150-
start_tag = '<modules>'
151-
end_tag = '</modules>'
152-
start_idx = content.find(start_tag)
153-
end_idx = content.find(end_tag)
154-
if start_idx != -1 and end_idx != -1:
155-
modules_section = content[start_idx + len(start_tag):end_idx]
156-
lines = [l for l in modules_section.splitlines(keepends=True) if l.strip()]
157-
158-
java_indices = [i for i, l in enumerate(lines) if '<module>java-' in l]
159-
if java_indices:
160-
start_java = java_indices[0]
161-
end_java = java_indices[-1] + 1
162-
java_lines = lines[start_java:end_java]
163-
if not any(f'<module>{module_name}</module>' in l for l in java_lines):
164-
java_lines.append(new_module)
165-
java_lines.sort()
166-
lines = lines[:start_java] + java_lines + lines[end_java:]
167-
else:
168-
if not any(f'<module>{module_name}</module>' in l for l in lines):
169-
lines.append(new_module)
170-
171-
new_content = content[:start_idx + len(start_tag)] + '\n' + ''.join(lines) + ' ' + content[end_idx:]
172-
with open(pom_path, 'w') as f:
173-
f.write(new_content)
174-
" "pom.xml" "$SOURCE_REPO_NAME"
144+
python3 "$UPDATE_ROOT_POM_SCRIPT" "pom.xml" "$SOURCE_REPO_NAME"
175145

176146
echo "Committing root pom.xml modules update..."
177147
git add pom.xml
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
5+
def update_root_pom(pom_path, module_name):
6+
new_module = f' <module>{module_name}</module>\n'
7+
with open(pom_path, 'r') as f:
8+
content = f.read()
9+
10+
start_tag = '<modules>'
11+
end_tag = '</modules>'
12+
start_idx = content.find(start_tag)
13+
end_idx = content.find(end_tag)
14+
15+
if start_idx == -1 or end_idx == -1:
16+
print(f"Error: {start_tag} or {end_tag} not found in {pom_path}")
17+
sys.exit(1)
18+
19+
modules_section = content[start_idx + len(start_tag):end_idx]
20+
lines = [l for l in modules_section.splitlines(keepends=True) if l.strip()]
21+
22+
java_indices = [i for i, l in enumerate(lines) if '<module>java-' in l]
23+
if java_indices:
24+
start_java = java_indices[0]
25+
end_java = java_indices[-1] + 1
26+
java_lines = lines[start_java:end_java]
27+
if not any(f'<module>{module_name}</module>' in l for l in java_lines):
28+
java_lines.append(new_module)
29+
java_lines.sort()
30+
lines = lines[:start_java] + java_lines + lines[end_java:]
31+
else:
32+
if not any(f'<module>{module_name}</module>' in l for l in lines):
33+
lines.append(new_module)
34+
35+
new_content = content[:start_idx + len(start_tag)] + '\n' + ''.join(lines) + ' ' + content[end_idx:]
36+
with open(pom_path, 'w') as f:
37+
f.write(new_content)
38+
39+
if __name__ == "__main__":
40+
if len(sys.argv) != 3:
41+
print("Usage: update_root_pom.py <pom_path> <module_name>")
42+
sys.exit(1)
43+
update_root_pom(sys.argv[1], sys.argv[2])

0 commit comments

Comments
 (0)