-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsync_version.py
More file actions
236 lines (207 loc) · 7.16 KB
/
sync_version.py
File metadata and controls
236 lines (207 loc) · 7.16 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/env python3
"""
OpenCut Version Sync Script
Reads __version__ from opencut/__init__.py and updates all other files
that contain version strings to match.
Usage:
python scripts/sync_version.py # Sync current version to all files
python scripts/sync_version.py --set 1.3.0 # Set new version, then sync
"""
import argparse
import re
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
INIT_PY = ROOT / "opencut" / "__init__.py"
# Each entry: (relative path, regex pattern, replacement template)
# The replacement template uses {v} for the version string.
TARGETS = [
(
"pyproject.toml",
r'^(version\s*=\s*")[^"]+(")',
r'\g<1>{v}\g<2>',
),
(
"opencut/server.py",
r'(OpenCut Backend Server v)[0-9]+\.[0-9]+\.[0-9]+',
r'\g<1>{v}',
),
(
"extension/com.opencut.panel/client/index.html",
r'(<span class="settings-value">)[0-9]+\.[0-9]+\.[0-9]+(</span>)',
r'\g<1>{v}\g<2>',
),
(
"extension/com.opencut.panel/client/main.js",
r'(OpenCut CEP Panel - Main Controller v)[0-9]+\.[0-9]+\.[0-9]+',
r'\g<1>{v}',
),
(
"extension/com.opencut.panel/client/style.css",
r'(OpenCut CEP Panel v)[0-9]+\.[0-9]+\.[0-9]+',
r'\g<1>{v}',
),
# CEP manifest (both bundle and extension version)
(
"extension/com.opencut.panel/CSXS/manifest.xml",
r'(ExtensionBundleVersion=")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
(
"extension/com.opencut.panel/CSXS/manifest.xml",
r'(<Extension Id="com\.opencut\.panel\.main" Version=")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# Installer C# project
(
"installer/src/OpenCut.Installer/OpenCut.Installer.csproj",
r'(<Version>)[0-9]+\.[0-9]+\.[0-9]+(</Version>)',
r'\g<1>{v}\g<2>',
),
(
"installer/src/OpenCut.Installer/OpenCut.Installer.csproj",
r'(<FileVersion>)[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(</FileVersion>)',
r'\g<1>{v}.0\g<2>',
),
# Installer AppConstants.cs
(
"installer/src/OpenCut.Installer/Models/AppConstants.cs",
r'(Version\s*=\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# Installer app.manifest
(
"installer/src/OpenCut.Installer/Properties/app.manifest",
r'(assemblyIdentity version=")[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}.0\g<2>',
),
# Install.ps1 dev installer banner
(
"Install.ps1",
r'(Installer v)[0-9]+\.[0-9]+\.[0-9]+',
r'\g<1>{v}',
),
# install.py dev installer
(
"install.py",
r'(VERS\s*=\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# requirements.txt header comment
(
"requirements.txt",
r'(# OpenCut v)[0-9]+\.[0-9]+\.[0-9]+',
r'\g<1>{v}',
),
# Inno Setup
(
"OpenCut.iss",
r'(#define MyAppVersion\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# CEP package.json
(
"extension/com.opencut.panel/package.json",
r'("version":\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# UXP manifest.json
(
"extension/com.opencut.uxp/manifest.json",
r'("version":\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# UXP main.js VERSION constant
(
"extension/com.opencut.uxp/main.js",
r'(const VERSION\s*=\s*")[0-9]+\.[0-9]+\.[0-9]+(")',
r'\g<1>{v}\g<2>',
),
# UXP index.html version display
(
"extension/com.opencut.uxp/index.html",
r'(<span class="oc-version">v)[0-9]+\.[0-9]+\.[0-9]+(</span>)',
r'\g<1>{v}\g<2>',
),
]
def read_version() -> str:
"""Read __version__ from opencut/__init__.py."""
text = INIT_PY.read_text(encoding="utf-8")
m = re.search(r'__version__\s*=\s*"([^"]+)"', text)
if not m:
print(f"ERROR: Could not find __version__ in {INIT_PY}")
sys.exit(1)
return m.group(1)
def set_version(new_ver: str) -> None:
"""Write __version__ into opencut/__init__.py."""
text = INIT_PY.read_text(encoding="utf-8")
updated = re.sub(
r'(__version__\s*=\s*")[^"]+(")',
rf'\g<1>{new_ver}\g<2>',
text,
)
INIT_PY.write_text(updated, encoding="utf-8")
print(f" SET {INIT_PY.relative_to(ROOT)} -> {new_ver}")
def check_file(rel_path: str, pattern: str, version: str) -> bool:
"""Check if a file's version matches. Returns True if in sync."""
fpath = ROOT / rel_path
if not fpath.exists():
return True # Missing files are OK (optional targets)
text = fpath.read_text(encoding="utf-8")
m = re.search(pattern, text, flags=re.MULTILINE)
if not m:
return True # Pattern not found — nothing to check
# Extract current version from the matched text
matched = m.group(0)
if version in matched:
return True
print(f" MISMATCH {rel_path} (expected {version})")
return False
def sync_file(rel_path: str, pattern: str, replacement: str, version: str) -> bool:
"""Update a single version occurrence in a file. Returns True if changed."""
fpath = ROOT / rel_path
if not fpath.exists():
print(f" SKIP {rel_path} (file not found)")
return False
text = fpath.read_text(encoding="utf-8")
repl = replacement.replace("{v}", version)
updated, count = re.subn(pattern, repl, text, count=1, flags=re.MULTILINE)
if count == 0:
print(f" SKIP {rel_path} (pattern not matched)")
return False
fpath.write_text(updated, encoding="utf-8")
print(f" SYNC {rel_path} -> {version}")
return True
def main() -> None:
parser = argparse.ArgumentParser(description="Sync OpenCut version across all files")
parser.add_argument("--set", dest="new_version", metavar="X.Y.Z",
help="Set a new version in __init__.py before syncing")
parser.add_argument("--check", action="store_true",
help="Check all files are in sync (exit 1 if not). Does not modify files.")
args = parser.parse_args()
if args.new_version:
if not re.match(r'^\d+\.\d+\.\d+$', args.new_version):
print(f"ERROR: Invalid version format '{args.new_version}' (expected X.Y.Z)")
sys.exit(1)
set_version(args.new_version)
version = read_version()
if args.check:
print(f"\nChecking version {version} across project files:\n")
all_ok = True
for rel_path, pattern, _replacement in TARGETS:
if not check_file(rel_path, pattern, version):
all_ok = False
if all_ok:
print(f"\nAll files in sync at v{version}.")
else:
print(f"\nVersion mismatch detected! Run: python scripts/sync_version.py")
sys.exit(1)
return
print(f"\nSyncing version {version} across project files:\n")
changed = 0
for rel_path, pattern, replacement in TARGETS:
if sync_file(rel_path, pattern, replacement, version):
changed += 1
print(f"\nDone. {changed} file(s) updated to v{version}.")
if __name__ == "__main__":
main()