-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathversion_manager.dart
More file actions
342 lines (290 loc) Β· 10.3 KB
/
version_manager.dart
File metadata and controls
342 lines (290 loc) Β· 10.3 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/usr/bin/env dart
// ignore_for_file: avoid_print
import 'dart:io';
class VersionManager {
static const String pubspecPath = 'pubspec.yaml';
void run() async {
try {
print('π AzyX Version Manager\n');
final currentVersion = await getCurrentVersion();
final currentBuildNumber = await getCurrentBuildNumber();
print('π Current Status:');
print(' Version: $currentVersion');
print(' Build Number: $currentBuildNumber\n');
print('π§ What would you like to do?');
print(
' 1. Patch increment ($currentVersion β ${incrementVersion(currentVersion, 'patch')})');
print(
' 2. Minor increment ($currentVersion β ${incrementVersion(currentVersion, 'minor')})');
print(
' 3. Major increment ($currentVersion β ${incrementVersion(currentVersion, 'major')})');
print(
' 4. Release hotfix ($currentVersion β ${addHotfix(currentVersion)})');
print(
' 5. Release pre-release ($currentVersion β $currentVersion-[alpha/beta/rc])');
print(' 6. Custom version');
print(' 7. Exit\n');
stdout.write('Enter your choice (1-7): ');
final choice = stdin.readLineSync()?.trim();
String? newVersion;
String versionType = '';
switch (choice) {
case '1':
newVersion = incrementVersion(currentVersion, 'patch');
versionType = 'patch';
break;
case '2':
newVersion = incrementVersion(currentVersion, 'minor');
versionType = 'minor';
break;
case '3':
newVersion = incrementVersion(currentVersion, 'major');
versionType = 'major';
break;
case '4':
newVersion = addHotfix(currentVersion);
versionType = 'hotfix';
break;
case '5':
newVersion = await handlePreRelease(currentVersion);
versionType = 'pre-release';
break;
case '6':
newVersion = await getCustomVersion();
versionType = 'custom';
break;
case '7':
print('π Goodbye!');
return;
default:
print('β Invalid choice');
return;
}
if (newVersion == null) return;
// Confirm the change
print('\nπ Proposed Changes:');
print(' Current: $currentVersion+$currentBuildNumber');
print(' New: $newVersion+${currentBuildNumber + 1}');
stdout.write('\nβ
Proceed with this update? (y/N): ');
final confirm = stdin.readLineSync()?.trim().toLowerCase();
if (confirm != 'y' && confirm != 'yes') {
print('β Update cancelled');
return;
}
await updateVersions(newVersion, currentBuildNumber + 1);
await handleGitOperations(newVersion, currentVersion, versionType);
print('\nπ Version updated successfully!');
} catch (e) {
print('β Error: $e');
}
}
Future<String> getCurrentVersion() async {
final pubspecContent = await File(pubspecPath).readAsString();
final versionMatch =
RegExp(r'version:\s*([^\+\s]+)').firstMatch(pubspecContent);
if (versionMatch == null) {
throw Exception('Could not find version in pubspec.yaml');
}
return versionMatch.group(1)!;
}
Future<int> getCurrentBuildNumber() async {
final pubspecContent = await File(pubspecPath).readAsString();
final versionMatch =
RegExp(r'version:\s*[^\+\s]+\+(\d+)').firstMatch(pubspecContent);
if (versionMatch == null) {
throw Exception('Could not find build number in pubspec.yaml');
}
return int.parse(versionMatch.group(1)!);
}
String incrementVersion(String version, String type) {
final cleanVersion = version.split('-')[0];
final parts = cleanVersion.split('.').map(int.parse).toList();
while (parts.length < 3) {
parts.add(0);
}
switch (type) {
case 'major':
parts[0]++;
parts[1] = 0;
parts[2] = 0;
break;
case 'minor':
parts[1]++;
parts[2] = 0;
break;
case 'patch':
parts[2]++;
break;
}
return parts.join('.');
}
String addHotfix(String version) {
final cleanVersion = version.split('-')[0];
return '$cleanVersion-hotfix';
}
Future<String?> handlePreRelease(String currentVersion) async {
print('\nπ Pre-release options:');
print(' 1. Alpha');
print(' 2. Beta');
print(' 3. Release Candidate (RC)');
stdout.write('Choose pre-release type (1-3): ');
final choice = stdin.readLineSync()?.trim();
String tag;
switch (choice) {
case '1':
tag = 'alpha';
break;
case '2':
tag = 'beta';
break;
case '3':
tag = 'rc';
break;
default:
print('β Invalid choice');
return null;
}
final cleanVersion = currentVersion.split('-')[0];
return '$cleanVersion-$tag';
}
Future<String?> getCustomVersion() async {
stdout.write('\nπ Enter custom version (e.g., 3.0.0, 2.9.9-beta): ');
final customVersion = stdin.readLineSync()?.trim();
if (customVersion == null || customVersion.isEmpty) {
print('β Invalid version');
return null;
}
if (!RegExp(r'^\d+\.\d+\.\d+(-\w+)?$').hasMatch(customVersion)) {
print('β Invalid version format. Use: major.minor.patch[-tag]');
return null;
}
return customVersion;
}
Future<void> updateVersions(String newVersion, int newBuildNumber) async {
await updatePubspec(newVersion, newBuildNumber);
}
Future<void> updatePubspec(String newVersion, int newBuildNumber) async {
final content = await File(pubspecPath).readAsString();
var updatedContent = content.replaceAll(
RegExp(r'version:\s*[^\n]+'), 'version: $newVersion+$newBuildNumber');
if (updatedContent.contains('inno_bundle:')) {
updatedContent = updatedContent.replaceAllMapped(
RegExp(r'(inno_bundle:.*?)(\n\s*version:\s*)([^\n]+)', dotAll: true),
(match) {
final before = match.group(1);
final versionKey = match.group(2);
return '$before$versionKey$newVersion';
},
);
}
await File(pubspecPath).writeAsString(updatedContent);
print('β
Updated pubspec.yaml (main version and inno_bundle)');
}
String generateCommitMessage(
String newVersion, String oldVersion, String versionType) {
final messages = {
'major': 'build: bump version to v$newVersion (major release)',
'minor': 'build: bump version to v$newVersion (minor release)',
'patch': 'build: bump version to v$newVersion (patch release)',
'hotfix': 'build: bump version to v$newVersion (hotfix)',
'pre-release': 'build: bump version to v$newVersion (pre-release)',
'custom': 'build: bump version to v$newVersion',
};
return messages[versionType] ?? 'build: bump version to v$newVersion';
}
Future<void> handleGitOperations(
String newVersion, String oldVersion, String versionType) async {
print('\nπ Git Operations:');
print(' 1. Commit changes, create tag, and push');
print(' 2. Commit changes and create tag only');
print(' 3. Commit changes only');
print(' 4. Create tag only (no commit)');
print(' 5. Skip all git operations');
stdout.write('Choose option (1-5): ');
final choice = stdin.readLineSync()?.trim();
switch (choice) {
case '1':
await commitChanges(newVersion, oldVersion, versionType);
await createTag(newVersion);
await pushChanges();
await pushTag(newVersion);
break;
case '2':
await commitChanges(newVersion, oldVersion, versionType);
await createTag(newVersion);
break;
case '3':
await commitChanges(newVersion, oldVersion, versionType);
break;
case '4':
await createTag(newVersion);
break;
case '5':
print('βοΈ Skipped all git operations');
break;
default:
print('β Invalid choice, skipping git operations');
}
}
Future<void> commitChanges(
String newVersion, String oldVersion, String versionType) async {
try {
final addResult = await Process.run('git', ['add', pubspecPath]);
if (addResult.exitCode != 0) {
print('β Failed to stage pubspec.yaml: ${addResult.stderr}');
return;
}
final commitMessage =
generateCommitMessage(newVersion, oldVersion, versionType);
final commitResult =
await Process.run('git', ['commit', '-m', commitMessage]);
if (commitResult.exitCode == 0) {
print('β
Committed changes: $commitMessage');
} else {
print('β Failed to commit changes: ${commitResult.stderr}');
}
} catch (e) {
print('β Git commit failed: $e');
}
}
Future<void> createTag(String version) async {
try {
final result = await Process.run('git', ['tag', 'v$version']);
if (result.exitCode == 0) {
print('β
Created git tag: v$version');
} else {
print('β Failed to create git tag: ${result.stderr}');
}
} catch (e) {
print('β Git tag creation failed: $e');
}
}
Future<void> pushChanges() async {
try {
final result = await Process.run('git', ['push']);
if (result.exitCode == 0) {
print('β
Pushed commits to remote');
} else {
print('β Failed to push commits: ${result.stderr}');
}
} catch (e) {
print('β Git push failed: $e');
}
}
Future<void> pushTag(String version) async {
try {
final result = await Process.run('git', ['push', 'origin', 'v$version']);
if (result.exitCode == 0) {
print('β
Pushed git tag: v$version');
} else {
print('β Failed to push git tag: ${result.stderr}');
}
} catch (e) {
print('β Git tag push failed: $e');
}
}
}
void main() {
final manager = VersionManager();
manager.run();
}