-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathmigrate_packages.py
More file actions
117 lines (102 loc) · 3.75 KB
/
migrate_packages.py
File metadata and controls
117 lines (102 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python3
"""
Migration script to convert old CLI attributes to new CLI attributes in Docker, Helm, and Kubernetes packages.
"""
import os
import re
import glob
def migrate_file(filepath):
"""Migrate a single file to use new CLI attributes."""
with open(filepath, 'r', encoding='utf-8-sig') as f:
content = f.read()
original = content
# 1. Replace CommandPrecedingArguments with CliCommand
# [CommandPrecedingArguments("docker", "config", "create")]
content = re.sub(
r'\[CommandPrecedingArguments\(([^)]+)\)\]',
r'[CliCommand(\1)]',
content
)
# 2. Replace BooleanCommandSwitch with CliFlag
# [BooleanCommandSwitch("--force")] -> [CliFlag("--force")]
content = re.sub(
r'\[BooleanCommandSwitch\(([^)]+)\)\]',
r'[CliFlag(\1)]',
content
)
# 3. Replace CommandEqualsSeparatorSwitch with CliOption with Format
# [CommandEqualsSeparatorSwitch("--option")] -> [CliOption("--option", Format = OptionFormat.EqualsSeparated)]
content = re.sub(
r'\[CommandEqualsSeparatorSwitch\("([^"]+)"\)\]',
r'[CliOption("\1", Format = OptionFormat.EqualsSeparated)]',
content
)
# 4. Replace CommandSwitch with CliOption
# [CommandSwitch("--option")] -> [CliOption("--option")]
content = re.sub(
r'\[CommandSwitch\(([^)]+)\)\]',
r'[CliOption(\1)]',
content
)
# 5. Replace PositionalArgument with CliArgument
# [PositionalArgument(Position = Position.AfterSwitches)] -> [CliArgument(Placement = ArgumentPlacement.AfterOptions)]
# [PositionalArgument(Position = Position.BeforeSwitches)] -> [CliArgument(Placement = ArgumentPlacement.BeforeOptions)]
# [PositionalArgument] -> [CliArgument]
content = re.sub(
r'\[PositionalArgument\(Position\s*=\s*Position\.AfterSwitches\)\]',
r'[CliArgument(Placement = ArgumentPlacement.AfterOptions)]',
content
)
content = re.sub(
r'\[PositionalArgument\(Position\s*=\s*Position\.BeforeSwitches\)\]',
r'[CliArgument(Placement = ArgumentPlacement.BeforeOptions)]',
content
)
content = re.sub(
r'\[PositionalArgument\]',
r'[CliArgument]',
content
)
# Handle PlaceholderName pattern
content = re.sub(
r'\[PositionalArgument\(PlaceholderName\s*=\s*"([^"]+)"\)\]',
r'[CliArgument(Name = "\1")]',
content
)
# 6. Add OptionFormat using if CliOption with Format is used
if 'OptionFormat.EqualsSeparated' in content and 'using ModularPipelines.Options;' not in content:
# Find the last using statement and add after it
content = re.sub(
r'(using [^;]+;)\n(\nnamespace)',
r'\1\nusing ModularPipelines.Options;\2',
content
)
if content != original:
with open(filepath, 'w', encoding='utf-8') as f:
f.write(content)
return True
return False
def migrate_package(package_path):
"""Migrate all files in a package."""
migrated = 0
for filepath in glob.glob(os.path.join(package_path, '**', '*.cs'), recursive=True):
# Skip Generated folder
if 'Generated' in filepath or 'obj' in filepath or 'bin' in filepath:
continue
if migrate_file(filepath):
print(f"Migrated: {filepath}")
migrated += 1
return migrated
def main():
packages = [
r'C:\git\ModularPipelines\src\ModularPipelines.Azure',
]
total = 0
for package in packages:
print(f"\n=== Migrating {os.path.basename(package)} ===")
count = migrate_package(package)
total += count
print(f"Migrated {count} files")
print(f"\n=== Total: {total} files migrated ===")
if __name__ == '__main__':
main()