Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Unified Diff: pkg/analyzer/lib/source/embedder.dart

Issue 1437893003: Add _embedder.yaml support to analyzer and analysis_server (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/analyzer/lib/source/embedder.dart
diff --git a/pkg/analyzer/lib/source/sdk_ext.dart b/pkg/analyzer/lib/source/embedder.dart
similarity index 56%
copy from pkg/analyzer/lib/source/sdk_ext.dart
copy to pkg/analyzer/lib/source/embedder.dart
index 646a19fc60404eaf3e454395623da205522f3b32..c1086bff1b89b6ac13441b4ef4a334484e60ec31 100644
--- a/pkg/analyzer/lib/source/sdk_ext.dart
+++ b/pkg/analyzer/lib/source/embedder.dart
@@ -2,9 +2,8 @@
// 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.
-library source.sdk_ext;
+library source.embedder;
-import 'dart:convert';
import 'dart:core' hide Resource;
import 'package:analyzer/file_system/file_system.dart';
@@ -12,44 +11,143 @@ import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
import 'package:analyzer/src/generated/source.dart';
import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource;
import 'package:path/path.dart' as pathos;
+import 'package:yaml/yaml.dart';
-/// Given a packageMap (see [PackageMapProvider]), check in each package's lib
-/// directory for the existence of a `_sdkext` file. This file must contain a
-/// JSON encoded map. Each key in the map is a `dart:` library name. Each value
-/// is a path (relative to the directory containing `_sdkext`) to a dart script
+/// Given a packageMap, check in each package's lib directory for the
+/// existence of an `_embedder.yaml` file. If the file contains a top level
+/// YamlMap, it will be added to the [embedderYamls] map.
+class EmbedderYamlLocator {
+ static const String EMBEDDER_FILE_NAME = '_embedder.yaml';
+
+ // Map from package's library directory to the parsed
+ // YamlMap.
+ final Map<Folder, YamlMap> embedderYamls = {};
Brian Wilkerson 2015/11/13 19:54:18 "{}" --> "new HashMap<Folder, YamlMap>()" It's mo
Cutch 2015/11/14 00:15:17 Done.
+
+ EmbedderYamlLocator(Map<String, List<Folder>> packageMap) {
+ if (packageMap != null) {
+ refresh(packageMap);
+ }
+ }
+
+ void refresh(Map<String, List<Folder>> packageMap) {
+ // Clear existing.
+ embedderYamls.clear();
+ if (packageMap == null) {
+ return;
+ }
+ packageMap.forEach(_processPackage);
+ }
+
+ /// Programatically add an _embedder.yaml mapping.
+ void addEmbedderYaml(Folder libDir, String embedderYaml) {
+ _processEmbedderYaml(libDir, embedderYaml);
+ }
+
+ /// Given a package [name] and a list of folders ([libDirs]),
+ /// add any found `_embedder.yaml` files.
+ void _processPackage(String name, List<Folder> libDirs) {
+ for (var libDir in libDirs) {
Brian Wilkerson 2015/11/13 19:54:18 In the analyzer code base we use type annotations
Cutch 2015/11/14 00:15:17 Done here and elsewhere.
+ var embedderYaml = _readEmbedderYaml(libDir);
+ if (embedderYaml != null) {
+ _processEmbedderYaml(libDir, embedderYaml);
+ }
+ }
+ }
+
+ /// Given the yaml for an embedder ([embedderYaml]) and a folder
+ /// ([libDir]), setup the uri mapping.
+ void _processEmbedderYaml(Folder libDir, String embedderYaml) {
+ YamlNode yaml;
+ try {
+ yaml = loadYaml(embedderYaml);
+ } catch (e) {
+ // TODO(pquitslund): Notify developer that something is wrong with the
pquitslund 2015/11/12 17:43:08 I guess I'm on the hook! :)
+ // _embedder.yaml file in libDir.
+ return;
+ }
+ if (yaml == null) {
+ // TODO(pquitslund): Notify developer that something is wrong with the
+ // _embedder.yaml file in libDir.
+ return;
+ }
+ if (yaml is! YamlMap) {
+ // TODO(pquitslund): Notify developer that something is wrong with the
+ // _embedder.yaml file in libDir.
+ return;
+ }
+ embedderYamls[libDir] = yaml;
+ }
+
+
+ /// Read the contents of [libDir]/[EMBEDDER_FILE_NAME] as a string.
+ /// Returns null if the file doesn't exist.
+ String _readEmbedderYaml(Folder libDir) {
+ var file = libDir.getChild(EMBEDDER_FILE_NAME);
+ try {
+ return file.readAsStringSync();
+ } on FileSystemException {
+ // File can't be read.
+ return null;
+ }
+ }
+}
+
+/// Given the [embedderYamls] from [EmbedderYamlLocator] check each one for the
+/// top level key 'embedder_libs'. Under the 'embedder_libs' key are key value
+/// pairs. Each key is a 'dart:' library uri and each value is a path
+/// (relative to the directory containing `_embedder.yaml`) to a dart script
/// for the given library. For example:
-/// {
-/// "dart:sky": "../sdk_ext/dart_sky.dart"
-/// }
+///
+/// embedder_libs:
+/// 'dart:io': '../../sdk/io/io.dart'
///
/// If a key doesn't begin with `dart:` it is ignored.
-class SdkExtUriResolver extends UriResolver {
- static const String SDK_EXT_NAME = '_sdkext';
+///
+class EmbedderUriResolver extends UriResolver {
static const String DART_COLON_PREFIX = 'dart:';
final Map<String, String> _urlMappings = <String, String>{};
- /// Construct a [SdkExtUriResolver] from a package map
+ /// Construct a [EmbedderUriResolver] from a package map
/// (see [PackageMapProvider]).
- SdkExtUriResolver(Map<String, List<Folder>> packageMap) {
- if (packageMap == null) {
+ EmbedderUriResolver(Map<Folder, YamlMap> embedderYamls) {
+ if (embedderYamls == null) {
return;
}
- packageMap.forEach(_processPackage);
+ embedderYamls.forEach(_processEmbedderYaml);
}
- /// Number of sdk extensions.
+ void _processEmbedderYaml(Folder libDir, YamlMap map) {
+ YamlNode embedder_libs = map['embedder_libs'];
+ if (embedder_libs == null) {
+ return;
+ }
+ if (embedder_libs is! YamlMap) {
+ return;
+ }
+ (embedder_libs as YamlMap).forEach((k, v) =>
+ _processEmbedderLibs(k, v, libDir));
+ }
+
+ /// Install the mapping from [name] to [libDir]/[file].
+ void _processEmbedderLibs(String name, String file, Folder libDir) {
+ if (!name.startsWith(DART_COLON_PREFIX)) {
+ // SDK libraries must begin with 'dart:'.
+ // TODO(pquitslund): Notify developer that something is wrong with the
+ // _embedder.yaml file in libDir.
+ return;
+ }
+ var key = name;
+ var value = libDir.canonicalizePath(file);
+ _urlMappings[key] = value;
+ }
+
+ /// Number of embedder libraries.
int get length => _urlMappings.length;
/// Return the path mapping for [libName] or null if there is none.
String operator [](String libName) => _urlMappings[libName];
- /// Programmatically add a new SDK extension given a JSON description
- /// ([sdkExtJSON]) and a lib directory ([libDir]).
- void addSdkExt(String sdkExtJSON, Folder libDir) {
- _processSdkExt(sdkExtJSON, libDir);
- }
-
@override
Source resolveAbsolute(Uri importUri, [Uri actualUri]) {
String libraryName = _libraryName(importUri);
@@ -60,7 +158,7 @@ class SdkExtUriResolver extends UriResolver {
// Not found.
return null;
}
- // This mapping points to the main entry file of the sdk extension.
+ // This mapping points to the main entry file of the dart: library.
Uri libraryEntry = new Uri.file(mapping);
if (!libraryEntry.isAbsolute) {
// We expect an absolute path.
@@ -115,55 +213,6 @@ class SdkExtUriResolver extends UriResolver {
return null;
}
- /// Given a package [name] and a list of folders ([libDirs]),
- /// add any found sdk extensions.
- void _processPackage(String name, List<Folder> libDirs) {
- for (var libDir in libDirs) {
- var sdkExt = _readDotSdkExt(libDir);
- if (sdkExt != null) {
- _processSdkExt(sdkExt, libDir);
- }
- }
- }
-
- /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder
- /// ([libDir]), setup the uri mapping.
- void _processSdkExt(String sdkExtJSON, Folder libDir) {
- var sdkExt;
- try {
- sdkExt = JSON.decode(sdkExtJSON);
- } catch (e) {
- return;
- }
- if ((sdkExt == null) || (sdkExt is! Map)) {
- return;
- }
- sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir));
- }
-
- /// Install the mapping from [name] to [libDir]/[file].
- void _processSdkExtension(String name, String file, Folder libDir) {
- if (!name.startsWith(DART_COLON_PREFIX)) {
- // SDK extensions must begin with 'dart:'.
- return;
- }
- var key = name;
- var value = libDir.canonicalizePath(file);
- _urlMappings[key] = value;
- }
-
- /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string.
- /// Returns null if the file doesn't exist.
- String _readDotSdkExt(Folder libDir) {
- var file = libDir.getChild(SDK_EXT_NAME);
- try {
- return file.readAsStringSync();
- } on FileSystemException {
- // File can't be read.
- return null;
- }
- }
-
/// Resolve an import of an sdk extension.
Source _resolveEntry(Uri libraryEntry, Uri importUri) {
// Library entry.

Powered by Google App Engine
This is Rietveld 408576698