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

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 51%
copy from pkg/analyzer/lib/source/sdk_ext.dart
copy to pkg/analyzer/lib/source/embedder.dart
index 646a19fc60404eaf3e454395623da205522f3b32..4f2cd39e41a648a8369f788ffc57c79829737993 100644
--- a/pkg/analyzer/lib/source/sdk_ext.dart
+++ b/pkg/analyzer/lib/source/embedder.dart
@@ -2,54 +2,153 @@
// 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 'dart:collection' show HashMap;
import 'package:analyzer/file_system/file_system.dart';
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 = new HashMap<Folder, YamlMap>();
+
+ 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 (Folder libDir in libDirs) {
+ String 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) {
pquitslund 2015/11/14 00:32:53 Nit: (e) => (_)
Cutch 2015/11/14 00:36:44 Done.
+ // TODO(pquitslund): Notify developer that something is wrong with the
+ // _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) {
+ File 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;
+ }
+ String key = name;
+ String 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 +159,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.
@@ -86,7 +185,7 @@ class SdkExtUriResolver extends UriResolver {
/// Return the extension name for [fullName] or `null`.
String _findExtensionNameFor(String fullName) {
- var result;
+ String result;
_urlMappings.forEach((extensionName, pathMapping) {
if (pathMapping == fullName) {
result = extensionName;
@@ -97,7 +196,7 @@ class SdkExtUriResolver extends UriResolver {
/// Return the library name of [importUri].
String _libraryName(Uri importUri) {
- var uri = importUri.toString();
+ String uri = importUri.toString();
int index = uri.indexOf('/');
if (index >= 0) {
return uri.substring(0, index);
@@ -107,7 +206,7 @@ class SdkExtUriResolver extends UriResolver {
/// Return the part path of [importUri].
String _partPath(Uri importUri) {
- var uri = importUri.toString();
+ String uri = importUri.toString();
int index = uri.indexOf('/');
if (index >= 0) {
return uri.substring(index + 1);
@@ -115,55 +214,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.
@@ -174,8 +224,8 @@ class SdkExtUriResolver extends UriResolver {
/// Resolve a 'part' statement inside an sdk extension.
Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) {
// Library part.
- var directory = pathos.dirname(libraryEntry.path);
- var partUri = new Uri.file(pathos.join(directory, partPath));
+ String directory = pathos.dirname(libraryEntry.path);
+ Uri partUri = new Uri.file(pathos.join(directory, partPath));
assert(partUri.isAbsolute);
JavaFile javaFile = new JavaFile.fromUri(partUri);
return new FileBasedSource(javaFile, importUri);

Powered by Google App Engine
This is Rietveld 408576698