-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathrelease.dart
342 lines (311 loc) · 9.34 KB
/
release.dart
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
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:io';
import 'package:args/args.dart';
const _packageOption = 'package';
const _versionOption = 'version';
const _resetFlag = 'reset';
const _skipStableCheckFlag = 'skipStableCheck';
/// Note: Must be run from the /tool directory.
///
/// To prepare DWDS for release:
/// `dart run release.dart -p dwds`
///
/// To prepare WebDev for release:
/// `dart run release.dart -p webdev`
///
/// To reset DWDS after a release:
/// `dart run release.dart --reset -p dwds -v [[wip version]]`
///
/// To reset WebDev after a release:
/// `dart run release.dart --reset -p webdev -v [[wip version]]`
void main(List<String> arguments) async {
final parser = ArgParser()
..addOption(
_packageOption,
abbr: 'p',
allowed: [
'webdev',
'dwds',
],
)
..addOption(_versionOption, abbr: 'v')
..addFlag(_resetFlag, abbr: 'r')
..addFlag(_skipStableCheckFlag, abbr: 's');
final argResults = parser.parse(arguments);
final package = argResults[_packageOption] as String?;
if (package == null) {
_logWarning('Please specify package with either --p=dwds or --p=webdev');
return;
}
final isReset = argResults[_resetFlag] as bool?;
final newVersion = argResults[_versionOption] as String?;
final skipStableCheck = argResults[_skipStableCheckFlag] as bool?;
int exitCode;
if (isReset == true) {
exitCode = await runReset(
package: package,
newVersion: newVersion,
);
} else {
exitCode = await runRelease(
package: package,
newVersion: newVersion,
skipStableCheck: skipStableCheck,
);
}
if (exitCode != 0) {
_logWarning('Run terminated unexpectedly with exit code: $exitCode');
}
}
Future<int> runReset({
required String package,
String? newVersion,
}) {
// Check that a new wip version has been provided.
final currentVersion = _readVersionFile(package);
if (newVersion == null || !newVersion.contains('wip')) {
_logInfo(
'''
Please provide the next wip version for $package, e.g. -v 3.0.1-wip
Current version is $currentVersion.
''',
);
return Future.value(1);
}
// Reset the dependency overrides for the package:
_updateOverrides(package, includeOverrides: true);
// Update the version strings in CHANGELOG and pubspec.yaml.
_updateVersionStrings(
package,
currentVersion: currentVersion,
nextVersion: newVersion,
isReset: true,
);
// Build the package.
final exitCode = _buildPackage(package);
return exitCode;
}
Future<int> runRelease({
required String package,
String? newVersion,
bool? skipStableCheck,
}) async {
// Check that we are on a stable version of Dart.
if (skipStableCheck != true) {
final checkVersionProcess = await Process.run('dart', ['--version']);
final versionInfo = checkVersionProcess.stdout as String;
if (!versionInfo.contains('stable')) {
_logWarning(
'''
Expected to be on stable version of Dart, instead on:
$versionInfo
To skip this check, re-run with --skipStableCheck
''',
);
return checkVersionProcess.exitCode;
}
}
// Update the pinned version of DWDS for webdev releases.
if (package == 'webdev') {
final newVersion = await _updateDwdsPin('webdev');
_logInfo('Add pinned DWDS info to CHANGELOG.');
final changelog = File('../webdev/CHANGELOG.md');
_addNewLine(
changelog,
newLine: '- Update `dwds` constraint to `${newVersion ?? 'TODO'}`.',
insertAt: 2,
);
}
// Remove any dependency overrides for the package:
_logInfo('Removing dependency overrides for $package.');
_updateOverrides(package, includeOverrides: false);
// Run dart pub upgrade.
for (final packagePath in [
'../dwds',
'../webdev',
'../frontend_server_common',
'../frontend_server_client',
'../test_common',
]) {
_logInfo('Upgrading pub packages for $packagePath');
final pubUpgradeProcess = await Process.run(
'dart',
[
'pub',
'upgrade',
],
workingDirectory: packagePath,
);
final upgradeErrors = pubUpgradeProcess.stderr as String;
if (upgradeErrors.isNotEmpty) {
_logWarning(upgradeErrors);
return pubUpgradeProcess.exitCode;
}
}
// Update the version strings in CHANGELOG and pubspec.yaml.
final currentVersion = _readVersionFile(package);
final nextVersion = newVersion ?? _removeWip(currentVersion);
_updateVersionStrings(
package,
currentVersion: currentVersion,
nextVersion: nextVersion,
);
// Build the package.
final exitCode = _buildPackage(package);
return exitCode;
}
Future<int> _buildPackage(String package) async {
_logInfo('Building $package');
final buildProcess = await Process.run(
'dart',
['run', 'build_runner', 'build'],
workingDirectory: '../$package',
);
final buildErrors = buildProcess.stderr as String;
if (buildErrors.isNotEmpty) {
_logWarning(buildErrors);
}
return buildProcess.exitCode;
}
void _updateOverrides(
String package, {
required bool includeOverrides,
}) {
final overridesFilePath = '../$package/pubspec_overrides.yaml';
final noOverridesFilePath = '../$package/ignore_pubspec_overrides.yaml';
if (includeOverrides) {
_renameFile(currentName: noOverridesFilePath, newName: overridesFilePath);
} else {
_renameFile(currentName: overridesFilePath, newName: noOverridesFilePath);
}
}
void _renameFile({required String currentName, required String newName}) {
final currentFile = File(currentName);
if (!currentFile.existsSync()) {
_logInfo('Skip renaming $currentName to $newName, file does not exist.');
return;
}
currentFile.rename(newName);
}
void _updateVersionStrings(
String package, {
required String nextVersion,
required String currentVersion,
bool isReset = false,
}) {
_logInfo('Updating $package from $currentVersion to $nextVersion');
final pubspec = File('../$package/pubspec.yaml');
final changelog = File('../$package/CHANGELOG.md');
if (isReset) {
_addNewLine(changelog, newLine: '## $nextVersion');
_replaceInFile(pubspec, query: currentVersion, replaceWith: nextVersion);
} else {
for (final file in [pubspec, changelog]) {
_replaceInFile(file, query: currentVersion, replaceWith: nextVersion);
}
}
}
void _addNewLine(
File file, {
required String newLine,
int insertAt = 0,
}) {
final currentLines = file.readAsLinesSync();
final linesBefore = currentLines.sublist(0, insertAt);
final linesAfter = currentLines.sublist(insertAt);
final newLines = [...linesBefore, newLine, '', ...linesAfter];
final content = newLines.joinWithNewLine();
return file.writeAsStringSync(content);
}
bool _replaceInFile(
File file, {
required String query,
required String replaceWith,
}) {
final newLines = <String>[];
var replaced = false;
for (final line in file.readAsLinesSync()) {
if (line.contains(query)) {
newLines.add(line.replaceAll(query, replaceWith));
replaced = true;
} else {
newLines.add(line);
}
}
final content = newLines.joinWithNewLine();
file.writeAsStringSync(content);
return replaced;
}
String _readVersionFile(String package) {
final versionFile = File('../$package/lib/src/version.dart');
final lines = versionFile.readAsLinesSync();
for (final line in lines) {
if (line.startsWith('const packageVersion =')) {
final version = line
.split('=')
.last
.split('')
.where((char) => char != ';' && char != "'" && char != '"')
.join('');
return version.trim();
}
}
throw Exception('Could not read version in $package/lib/src/version.dart');
}
String _removeWip(String wipVersion) {
if (!wipVersion.contains('wip')) {
throw Exception('$wipVersion is not a wip version.');
}
return wipVersion.split('-wip').first;
}
/// Returns the new pinned DWDS version on success.
Future<String?> _updateDwdsPin(String package) async {
final pubOutdatedProcess = await Process.run(
'dart',
[
'pub',
'outdated',
'--no-dependency-overrides',
],
workingDirectory: '../$package',
);
final lines = pubOutdatedProcess.stdout.split('\n') as List<String>;
String? nextDwdsVersion;
String? currentDwdsVersion;
for (final line in lines) {
if (line.trim().startsWith('dwds')) {
final segments =
line.trim().split(' ').where((segment) => segment != ' ');
nextDwdsVersion = segments.last;
currentDwdsVersion =
segments.lastWhere((segment) => segment.startsWith('*')).substring(1);
break;
}
}
final next = nextDwdsVersion ?? '';
final current = currentDwdsVersion ?? '';
if (next.isNotEmpty && current.isNotEmpty) {
_logInfo('Changing DWDS pin from $current to $next');
_replaceInFile(
File('../$package/pubspec.yaml'),
query: current,
replaceWith: next,
);
return nextDwdsVersion;
}
_logWarning('Unable to determine DWDS version to pin.');
return null;
}
void _logInfo(String message) {
stdout.writeln(message);
}
void _logWarning(String warning) {
stderr.writeln(warning);
}
extension JoinExtension on List<String> {
String joinWithNewLine() {
return '${join('\n')}\n';
}
}