forked from dart-lang/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_package_config.dart
304 lines (257 loc) · 9.72 KB
/
generate_package_config.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
// Copyright (c) 2020, 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.
/// Generates the repo's ".dart_tool/package_config.json" file.
import 'dart:convert';
import 'dart:io';
// Important! Do not add package: imports to this file.
// Do not add relative deps for libraries that themselves use package deps.
// This tool runs before the .dart_tool/package_config.json file is created, so
// can not itself use package references.
final repoRoot = dirname(dirname(fromUri(Platform.script)));
void main(List<String> args) {
var packageDirs = [
...listSubdirectories(platform('pkg')),
...listSubdirectories(platform('third_party/pkg')),
platform('pkg/vm_service/test/test_package'),
platform(
'runtime/observatory_2/tests/service_2/observatory_test_package_2'),
platform('runtime/observatory'),
platform('runtime/observatory/tests/service/observatory_test_package'),
platform('runtime/observatory_2'),
platform('runtime/tools/heapsnapshot'),
platform('sdk/lib/_internal/sdk_library_metadata'),
platform('third_party/devtools/devtools_shared'),
platform('tools/package_deps'),
];
var cfePackageDirs = [
platform('pkg/front_end/testcases'),
];
var feAnalyzerSharedPackageDirs = [
platform('pkg/_fe_analyzer_shared/test/exhaustiveness/data'),
platform('pkg/_fe_analyzer_shared/test/flow_analysis/assigned_variables'),
platform('pkg/_fe_analyzer_shared/test/flow_analysis/definite_assignment'),
platform(
'pkg/_fe_analyzer_shared/test/flow_analysis/definite_unassignment'),
platform('pkg/_fe_analyzer_shared/test/flow_analysis/nullability'),
platform('pkg/_fe_analyzer_shared/test/flow_analysis/reachability'),
platform('pkg/_fe_analyzer_shared/test/flow_analysis/type_promotion'),
platform('pkg/_fe_analyzer_shared/test/flow_analysis/why_not_promoted'),
platform('pkg/_fe_analyzer_shared/test/inheritance'),
];
var pkgVmPackageDirs = [
platform('pkg/vm/testcases'),
];
// Validate that all the given directories exist.
var hasMissingDirectories = false;
for (var path in [
...packageDirs,
...cfePackageDirs,
...feAnalyzerSharedPackageDirs,
...pkgVmPackageDirs
]) {
if (!Directory(join(repoRoot, path)).existsSync()) {
stderr.writeln("Unable to locate directory: '$path'.");
hasMissingDirectories = true;
}
}
if (hasMissingDirectories) {
exit(1);
}
var packages = <Package>[
...makePackageConfigs(packageDirs),
...makeCfePackageConfigs(cfePackageDirs),
...makeFeAnalyzerSharedPackageConfigs(feAnalyzerSharedPackageDirs),
...makePkgVmPackageConfigs(pkgVmPackageDirs),
];
packages.sort((a, b) => a.name.compareTo(b.name));
// Remove any packages with identical names.
final uniqueNames = packages.map((p) => p.name).toSet();
var hasDuplicatePackages = false;
for (var name in uniqueNames) {
var matches = packages.where((p) => p.name == name).toList();
if (matches.length > 1) {
print('Duplicates found for package:$name');
for (var package in matches) {
print(' ${package.rootUri}');
}
hasDuplicatePackages = true;
}
}
if (hasDuplicatePackages) {
exit(1);
}
var configFile = File(join(repoRoot, '.dart_tool', 'package_config.json'));
var packageConfig = PackageConfig(
packages,
extraData: {
'copyright': [
'Copyright (c) 2020, 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.',
],
'comment': [
'Package configuration for all packages in pkg/ and third_party/pkg/',
],
},
);
writeIfDifferent(configFile, packageConfig.generateJson('..'));
}
/// Writes the given [contents] string to [file] if the contents are different
/// than what's currently in the file.
///
/// This updates the file to the given contents, while preserving the file
/// timestamp if there are no changes.
void writeIfDifferent(File file, String contents) {
if (!file.existsSync() || file.readAsStringSync() != contents) {
file.writeAsStringSync(contents);
}
}
/// Generates package configurations for each package in [packageDirs].
Iterable<Package> makePackageConfigs(List<String> packageDirs) sync* {
for (var packageDir in packageDirs) {
var version = pubspecLanguageVersion(packageDir);
var hasLibDirectory =
Directory(join(repoRoot, packageDir, 'lib')).existsSync();
yield Package(
name: basename(packageDir),
rootUri: packageDir,
packageUri: hasLibDirectory ? 'lib/' : null,
languageVersion: version,
);
}
}
/// Generates package configurations for the special pseudo-packages.
Iterable<Package> makeSpecialPackageConfigs(
String packageNamePrefix, List<String> packageDirs) sync* {
// TODO: Remove the use of '.nonexisting/'.
for (var packageDir in packageDirs) {
yield Package(
name: '${packageNamePrefix}_${basename(packageDir)}',
rootUri: packageDir,
packageUri: '.nonexisting/',
);
}
}
/// Generates package configurations for the special pseudo-packages used by the
/// CFE unit tests (`pkg/front_end/test/unit_test_suites.dart`).
Iterable<Package> makeCfePackageConfigs(List<String> packageDirs) =>
makeSpecialPackageConfigs('front_end', packageDirs);
/// Generates package configurations for the special pseudo-packages used by the
/// _fe_analyzer_shared id tests.
Iterable<Package> makeFeAnalyzerSharedPackageConfigs(
List<String> packageDirs) =>
makeSpecialPackageConfigs('_fe_analyzer_shared', packageDirs);
/// Generates package configurations for the special pseudo-packages used by the
/// pkg/vm unit tests (`pkg/vm/test`).
Iterable<Package> makePkgVmPackageConfigs(List<String> packageDirs) =>
makeSpecialPackageConfigs('pkg_vm', packageDirs);
/// Finds the paths of the subdirectories of [dirPath] that contain pubspecs.
///
/// This method recurses until it finds a pubspec.yaml file.
Iterable<String> listSubdirectories(String parentPath) sync* {
final parent = Directory(join(repoRoot, parentPath));
for (var child in parent.listSync().whereType<Directory>()) {
var name = basename(child.path);
// Don't recurse into dot directories.
if (name.startsWith('.')) continue;
if (File(join(child.path, 'pubspec.yaml')).existsSync()) {
yield join(parentPath, name);
} else {
yield* listSubdirectories(join(parentPath, name));
}
}
}
final versionRE = RegExp(r"(?:\^|>=)(\d+\.\d+)");
/// Infers the language version from the SDK constraint in the pubspec for
/// [packageDir].
///
/// The version is returned in the form `major.minor`.
String? pubspecLanguageVersion(String packageDir) {
var pubspecFile = File(join(repoRoot, packageDir, 'pubspec.yaml'));
if (!pubspecFile.existsSync()) {
print("Error: Missing pubspec for $packageDir.");
exit(1);
}
var contents = pubspecFile.readAsLinesSync();
if (!contents.any((line) => line.contains('sdk: '))) {
print("Error: Pubspec for $packageDir has no SDK constraint.");
exit(1);
}
// Handle either "sdk: >=2.14.0 <3.0.0" or "sdk: ^2.3.0".
var sdkConstraint = contents.firstWhere((line) => line.contains('sdk: '));
sdkConstraint = sdkConstraint.trim().substring('sdk:'.length).trim();
if (sdkConstraint.startsWith('"') || sdkConstraint.startsWith("'")) {
sdkConstraint = sdkConstraint.substring(1, sdkConstraint.length - 2);
}
var match = versionRE.firstMatch(sdkConstraint);
if (match == null) {
print("Error: unknown version range for $packageDir: '$sdkConstraint'.");
exit(1);
}
return match[1]!;
}
class Package {
final String name;
final String rootUri;
final String? packageUri;
final String? languageVersion;
Package({
required this.name,
required this.rootUri,
this.packageUri,
this.languageVersion,
});
Map<String, Object?> toMap(String relativeTo) {
return {
'name': name,
'rootUri': posix(join(relativeTo, rootUri)),
if (packageUri != null) 'packageUri': posix(packageUri!),
if (languageVersion != null) 'languageVersion': languageVersion,
};
}
}
class PackageConfig {
final List<Package> packages;
final Map<String, Object?>? extraData;
PackageConfig(this.packages, {this.extraData});
String generateJson(String relativeTo) {
var config = <String, Object?>{};
if (extraData != null) {
for (var key in extraData!.keys) {
config[key] = extraData![key];
}
}
config['configVersion'] = 2;
config['generator'] = 'tools/generate_package_config.dart';
config['packages'] =
packages.map((package) => package.toMap(relativeTo)).toList();
var jsonString = JsonEncoder.withIndent(' ').convert(config);
return '$jsonString\n';
}
}
// Below are some (very simplified) versions of the package:path functions.
final String _separator = Platform.pathSeparator;
String dirname(String s) {
return s.substring(0, s.lastIndexOf(_separator));
}
String join(String s1, String s2, [String? s3]) {
if (s3 != null) {
return join(join(s1, s2), s3);
} else {
return s1.endsWith(_separator) ? '$s1$s2' : '$s1$_separator$s2';
}
}
String basename(String s) {
while (s.endsWith(_separator)) {
s = s.substring(0, s.length - 1);
}
return s.substring(s.lastIndexOf(_separator) + 1);
}
String fromUri(Uri uri) => uri.toFilePath();
/// Given a platform path, return a posix one.
String posix(String s) =>
Platform.isWindows ? s.replaceAll(_separator, '/') : s;
/// Given a posix path, return a platform one.
String platform(String s) =>
Platform.isWindows ? s.replaceAll('/', _separator) : s;