Skip to content

Commit 168768a

Browse files
committed
Basic version sync script
1 parent 67967b4 commit 168768a

File tree

1 file changed

+134
-0
lines changed

1 file changed

+134
-0
lines changed

scripts/sync_versin.dart

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
import 'dart:io';
2+
import 'package:yaml/yaml.dart';
3+
4+
void main() {
5+
final pubspecFile = File('pubspec.yaml');
6+
if (!pubspecFile.existsSync()) {
7+
print('pubspec.yaml not found');
8+
exit(1);
9+
}
10+
11+
final pubspecContent = pubspecFile.readAsStringSync();
12+
final pubspec = loadYaml(pubspecContent);
13+
14+
final version = pubspec['version'] as String;
15+
if (version == null) {
16+
print('Version not found in pubspec.yaml');
17+
exit(1);
18+
}
19+
20+
final versionParts = version.split('+');
21+
if (versionParts.length != 2) {
22+
print('Invalid version format in pubspec.yaml');
23+
exit(1);
24+
}
25+
26+
final versionName = versionParts[0];
27+
final versionCode = versionParts[1];
28+
29+
updateAndroidVersion(versionName, versionCode);
30+
// updateIOSVersion(versionName, versionCode);
31+
// updateMacOSVersion(versionName, versionCode);
32+
// updateLinuxVersion(versionName, versionCode);
33+
// updateWindowsVersion(versionName, versionCode);
34+
35+
print('Version updated successfully');
36+
}
37+
38+
void updateAndroidVersion(String versionName, String versionCode) {
39+
final buildGradleFile = File('android/app/build.gradle');
40+
if (!buildGradleFile.existsSync()) {
41+
print('android/app/build.gradle not found');
42+
return;
43+
}
44+
45+
final buildGradleContent = buildGradleFile.readAsStringSync();
46+
final updatedContent = buildGradleContent
47+
.replaceAll(RegExp(r'versionName "[^"]+"'), 'versionName "$versionName"')
48+
.replaceAll(RegExp(r'versionCode \d+'), 'versionCode $versionCode');
49+
50+
buildGradleFile.writeAsStringSync(updatedContent);
51+
print('Android version updated');
52+
}
53+
54+
void updateIOSVersion(String versionName, String versionCode) {
55+
final plistFile = File('ios/Runner/Info.plist');
56+
if (!plistFile.existsSync()) {
57+
print('ios/Runner/Info.plist not found');
58+
return;
59+
}
60+
61+
final plistContent = plistFile.readAsStringSync();
62+
final updatedContent = plistContent
63+
.replaceAll(
64+
RegExp(
65+
r'<key>CFBundleShortVersionString</key>\s*<string>[^<]+</string>'),
66+
'<key>CFBundleShortVersionString</key>\n\t<string>$versionName</string>')
67+
.replaceAll(
68+
RegExp(r'<key>CFBundleVersion</key>\s*<string>[^<]+</string>'),
69+
'<key>CFBundleVersion</key>\n\t<string>$versionCode</string>');
70+
71+
plistFile.writeAsStringSync(updatedContent);
72+
print('iOS version updated');
73+
}
74+
75+
void updateMacOSVersion(String versionName, String versionCode) {
76+
final plistFile = File('macos/Runner/Info.plist');
77+
if (!plistFile.existsSync()) {
78+
print('macos/Runner/Info.plist not found');
79+
return;
80+
}
81+
82+
final plistContent = plistFile.readAsStringSync();
83+
final updatedContent = plistContent
84+
.replaceAll(
85+
RegExp(
86+
r'<key>CFBundleShortVersionString</key>\s*<string>[^<]+</string>'),
87+
'<key>CFBundleShortVersionString</key>\n\t<string>$versionName</string>')
88+
.replaceAll(
89+
RegExp(r'<key>CFBundleVersion</key>\s*<string>[^<]+</string>'),
90+
'<key>CFBundleVersion</key>\n\t<string>$versionCode</string>');
91+
92+
plistFile.writeAsStringSync(updatedContent);
93+
print('macOS version updated');
94+
}
95+
96+
void updateLinuxVersion(String versionName, String versionCode) {
97+
final cmakeFile = File('linux/CMakeLists.txt');
98+
if (!cmakeFile.existsSync()) {
99+
print('linux/CMakeLists.txt not found');
100+
return;
101+
}
102+
103+
final cmakeContent = cmakeFile.readAsStringSync();
104+
final updatedContent = cmakeContent
105+
.replaceAll(RegExp(r'set\(PROJECT_VERSION "[^"]+"\)'),
106+
'set(PROJECT_VERSION "$versionName")')
107+
.replaceAll(RegExp(r'set\(PROJECT_VERSION_CODE \d+\)'),
108+
'set(PROJECT_VERSION_CODE $versionCode)');
109+
110+
cmakeFile.writeAsStringSync(updatedContent);
111+
print('Linux version updated');
112+
}
113+
114+
void updateWindowsVersion(String versionName, String versionCode) {
115+
final rcFile = File('windows/runner/Runner.rc');
116+
if (!rcFile.existsSync()) {
117+
print('windows/runner/Runner.rc not found');
118+
return;
119+
}
120+
121+
final rcContent = rcFile.readAsStringSync();
122+
final updatedContent = rcContent
123+
.replaceAll(RegExp(r'FILEVERSION \d+,\d+,\d+,\d+'),
124+
'FILEVERSION ${versionName.replaceAll('.', ',')},$versionCode')
125+
.replaceAll(RegExp(r'PRODUCTVERSION \d+,\d+,\d+,\d+'),
126+
'PRODUCTVERSION ${versionName.replaceAll('.', ',')},$versionCode')
127+
.replaceAll(RegExp(r'VALUE "FileVersion", "[^"]+"'),
128+
'VALUE "FileVersion", "$versionName.$versionCode"')
129+
.replaceAll(RegExp(r'VALUE "ProductVersion", "[^"]+"'),
130+
'VALUE "ProductVersion", "$versionName.$versionCode"');
131+
132+
rcFile.writeAsStringSync(updatedContent);
133+
print('Windows version updated');
134+
}

0 commit comments

Comments
 (0)