dart-sdk/site/try/build_sdk_json.dart
Sigmund Cherem 0b29970605 Build sdk.json without source-mirrors.
This basically removes the dependency on source-mirrors from the try site and will let us delete source-mirrors from the compiler.

I was a torn for a bit about how to replace the logic here. I considered these 4
options:

(a) directly use the compiler internal APIs to crawl for reachable libraries.
(b) use `analyze-all`, then iterate over the resolved libraries.
(c) avoid parsing altogether, just enumerate recursively all files under
    sdk/lib/ and create the json file that way.
(d) submit a fixed version of the sdk, since we unlikely need it up-to-date
    for the site/try tests

I decided decided to go with (c) in this CL. It's simple and, given that the
only purpose is to preserve the site/try tests until we replace them, it seems
like the option that will slow us down the least (it makes the .json file
bigger, but that's OK now that this is not a service in use). It seemed
unnecessary to do full resolution, so I wasn't convinced of doing (b), but I did experiment with (a) now that we are
making the frontend libraries easier to use on their own. The result was OK (see
https://codereview.chromium.org/2003233002/), but probably not worth submitting
because we'd have to remember to update the code every time we modify the
internal dart2js APIs. I rather have the code in that CL be a test instead.

R=het@google.com

Review URL: https://codereview.chromium.org/2006993002 .
2016-05-24 09:50:06 -07:00

47 lines
1.5 KiB
Dart

// Copyright (c) 2013, 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 'dart:convert';
import 'package:compiler/src/util/uri_extras.dart' show relativize;
main(List<String> arguments) async {
if (arguments.length == 0) {
print('usage: build_sdk_json.dart <out-path>');
exit(1);
}
var out = arguments[0];
List<Uri> sdkFiles = await collectSdkFiles();
new File(out).writeAsStringSync(emitSdkAsJson(sdkFiles));
}
Uri sdkRoot = Uri.base.resolveUri(Platform.script).resolve('../../');
/// Collects a list of files that are part of the SDK.
List<Uri> collectSdkFiles() {
var files = <Uri>[];
var sdkDir = new Directory.fromUri(sdkRoot.resolve('sdk/lib/'));
for (var entity in sdkDir.listSync(recursive: true)) {
if (entity is File &&
(entity.path.endsWith('.dart') || entity.path.endsWith('.platform'))) {
files.add(entity.uri);
}
}
return files;
}
/// Creates a string that encodes the contents of the sdk libraries in json.
///
/// The keys of the json file are sdk-relative paths to source files, and the
/// values are the contents of the file.
String emitSdkAsJson(List<Uri> paths) {
var map = <String, String>{};
for (var uri in paths) {
String filename = relativize(sdkRoot, uri, false);
var contents = new File.fromUri(uri).readAsStringSync();
map['sdk:/$filename'] = contents;
}
return JSON.encode(map);
}