-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathupdate_mono_pkg_yaml.dart
59 lines (52 loc) · 1.65 KB
/
update_mono_pkg_yaml.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
// Copyright (c) 2021, 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';
/// Updates mono_pkg.yaml files with the current Dart SDK runtime.
/// When an extra command line argument is provided, it will be used
/// instead of the current Dart SDK runtime.
void main(List<String> args) {
final sdkVersion =
args.isEmpty ? Platform.version.split(' ').first : args.first;
final baseDir = _inferPubDevBaseDir();
final packagePaths = _detectPackagePaths(baseDir);
for (final path in packagePaths) {
_updatePackage('$baseDir/$path', sdkVersion);
}
}
String _inferPubDevBaseDir() {
return Platform.script
.toFilePath()
.split('/')
.reversed
.skip(2)
.toList()
.reversed
.join('/');
}
List<String> _detectPackagePaths(String baseDir) {
return <String>[
'app',
...Directory('$baseDir/pkg')
.listSync()
.whereType<Directory>()
.map((d) => d.path.split('/').last)
.map((e) => 'pkg/$e')
.toList(),
];
}
void _updatePackage(String path, String sdkVersion) {
final monoPkg = File('$path/mono_pkg.yaml');
if (monoPkg.existsSync()) {
print('updating: ${monoPkg.path}');
final lines = monoPkg.readAsLinesSync();
final index = lines.indexOf('sdk:');
if (index >= 0 &&
index + 2 < lines.length &&
lines[index + 1].startsWith(' - ') &&
lines[index + 2].isEmpty) {
lines[index + 1] = ' - $sdkVersion';
monoPkg.writeAsStringSync(lines.join('\n') + '\n');
}
}
}