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

Side by Side 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 unified diff | Download patch
OLDNEW
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library source.sdk_ext; 5 library source.embedder;
6 6
7 import 'dart:convert';
8 import 'dart:core' hide Resource; 7 import 'dart:core' hide Resource;
9 8
10 import 'package:analyzer/file_system/file_system.dart'; 9 import 'package:analyzer/file_system/file_system.dart';
11 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; 10 import 'package:analyzer/src/generated/java_io.dart' show JavaFile;
12 import 'package:analyzer/src/generated/source.dart'; 11 import 'package:analyzer/src/generated/source.dart';
13 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; 12 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource;
14 import 'package:path/path.dart' as pathos; 13 import 'package:path/path.dart' as pathos;
14 import 'package:yaml/yaml.dart';
15 15
16 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib 16 /// Given a packageMap, check in each package's lib directory for the
17 /// directory for the existence of a `_sdkext` file. This file must contain a 17 /// existence of an `_embedder.yaml` file. If the file contains a top level
18 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value 18 /// YamlMap, it will be added to the [embedderYamls] map.
19 /// is a path (relative to the directory containing `_sdkext`) to a dart script 19 class EmbedderYamlLocator {
20 /// for the given library. For example: 20 static const String EMBEDDER_FILE_NAME = '_embedder.yaml';
21 /// {
22 /// "dart:sky": "../sdk_ext/dart_sky.dart"
23 /// }
24 ///
25 /// If a key doesn't begin with `dart:` it is ignored.
26 class SdkExtUriResolver extends UriResolver {
27 static const String SDK_EXT_NAME = '_sdkext';
28 static const String DART_COLON_PREFIX = 'dart:';
29 21
30 final Map<String, String> _urlMappings = <String, String>{}; 22 // Map from package's library directory to the parsed
23 // YamlMap.
24 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.
31 25
32 /// Construct a [SdkExtUriResolver] from a package map 26 EmbedderYamlLocator(Map<String, List<Folder>> packageMap) {
33 /// (see [PackageMapProvider]). 27 if (packageMap != null) {
34 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { 28 refresh(packageMap);
29 }
30 }
31
32 void refresh(Map<String, List<Folder>> packageMap) {
33 // Clear existing.
34 embedderYamls.clear();
35 if (packageMap == null) { 35 if (packageMap == null) {
36 return; 36 return;
37 } 37 }
38 packageMap.forEach(_processPackage); 38 packageMap.forEach(_processPackage);
39 } 39 }
40 40
41 /// Number of sdk extensions. 41 /// Programatically add an _embedder.yaml mapping.
42 void addEmbedderYaml(Folder libDir, String embedderYaml) {
43 _processEmbedderYaml(libDir, embedderYaml);
44 }
45
46 /// Given a package [name] and a list of folders ([libDirs]),
47 /// add any found `_embedder.yaml` files.
48 void _processPackage(String name, List<Folder> libDirs) {
49 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.
50 var embedderYaml = _readEmbedderYaml(libDir);
51 if (embedderYaml != null) {
52 _processEmbedderYaml(libDir, embedderYaml);
53 }
54 }
55 }
56
57 /// Given the yaml for an embedder ([embedderYaml]) and a folder
58 /// ([libDir]), setup the uri mapping.
59 void _processEmbedderYaml(Folder libDir, String embedderYaml) {
60 YamlNode yaml;
61 try {
62 yaml = loadYaml(embedderYaml);
63 } catch (e) {
64 // TODO(pquitslund): Notify developer that something is wrong with the
pquitslund 2015/11/12 17:43:08 I guess I'm on the hook! :)
65 // _embedder.yaml file in libDir.
66 return;
67 }
68 if (yaml == null) {
69 // TODO(pquitslund): Notify developer that something is wrong with the
70 // _embedder.yaml file in libDir.
71 return;
72 }
73 if (yaml is! YamlMap) {
74 // TODO(pquitslund): Notify developer that something is wrong with the
75 // _embedder.yaml file in libDir.
76 return;
77 }
78 embedderYamls[libDir] = yaml;
79 }
80
81
82 /// Read the contents of [libDir]/[EMBEDDER_FILE_NAME] as a string.
83 /// Returns null if the file doesn't exist.
84 String _readEmbedderYaml(Folder libDir) {
85 var file = libDir.getChild(EMBEDDER_FILE_NAME);
86 try {
87 return file.readAsStringSync();
88 } on FileSystemException {
89 // File can't be read.
90 return null;
91 }
92 }
93 }
94
95 /// Given the [embedderYamls] from [EmbedderYamlLocator] check each one for the
96 /// top level key 'embedder_libs'. Under the 'embedder_libs' key are key value
97 /// pairs. Each key is a 'dart:' library uri and each value is a path
98 /// (relative to the directory containing `_embedder.yaml`) to a dart script
99 /// for the given library. For example:
100 ///
101 /// embedder_libs:
102 /// 'dart:io': '../../sdk/io/io.dart'
103 ///
104 /// If a key doesn't begin with `dart:` it is ignored.
105 ///
106 class EmbedderUriResolver extends UriResolver {
107 static const String DART_COLON_PREFIX = 'dart:';
108
109 final Map<String, String> _urlMappings = <String, String>{};
110
111 /// Construct a [EmbedderUriResolver] from a package map
112 /// (see [PackageMapProvider]).
113 EmbedderUriResolver(Map<Folder, YamlMap> embedderYamls) {
114 if (embedderYamls == null) {
115 return;
116 }
117 embedderYamls.forEach(_processEmbedderYaml);
118 }
119
120 void _processEmbedderYaml(Folder libDir, YamlMap map) {
121 YamlNode embedder_libs = map['embedder_libs'];
122 if (embedder_libs == null) {
123 return;
124 }
125 if (embedder_libs is! YamlMap) {
126 return;
127 }
128 (embedder_libs as YamlMap).forEach((k, v) =>
129 _processEmbedderLibs(k, v, libDir));
130 }
131
132 /// Install the mapping from [name] to [libDir]/[file].
133 void _processEmbedderLibs(String name, String file, Folder libDir) {
134 if (!name.startsWith(DART_COLON_PREFIX)) {
135 // SDK libraries must begin with 'dart:'.
136 // TODO(pquitslund): Notify developer that something is wrong with the
137 // _embedder.yaml file in libDir.
138 return;
139 }
140 var key = name;
141 var value = libDir.canonicalizePath(file);
142 _urlMappings[key] = value;
143 }
144
145 /// Number of embedder libraries.
42 int get length => _urlMappings.length; 146 int get length => _urlMappings.length;
43 147
44 /// Return the path mapping for [libName] or null if there is none. 148 /// Return the path mapping for [libName] or null if there is none.
45 String operator [](String libName) => _urlMappings[libName]; 149 String operator [](String libName) => _urlMappings[libName];
46 150
47 /// Programmatically add a new SDK extension given a JSON description
48 /// ([sdkExtJSON]) and a lib directory ([libDir]).
49 void addSdkExt(String sdkExtJSON, Folder libDir) {
50 _processSdkExt(sdkExtJSON, libDir);
51 }
52
53 @override 151 @override
54 Source resolveAbsolute(Uri importUri, [Uri actualUri]) { 152 Source resolveAbsolute(Uri importUri, [Uri actualUri]) {
55 String libraryName = _libraryName(importUri); 153 String libraryName = _libraryName(importUri);
56 String partPath = _partPath(importUri); 154 String partPath = _partPath(importUri);
57 // Lookup library name in mappings. 155 // Lookup library name in mappings.
58 String mapping = _urlMappings[libraryName]; 156 String mapping = _urlMappings[libraryName];
59 if (mapping == null) { 157 if (mapping == null) {
60 // Not found. 158 // Not found.
61 return null; 159 return null;
62 } 160 }
63 // This mapping points to the main entry file of the sdk extension. 161 // This mapping points to the main entry file of the dart: library.
64 Uri libraryEntry = new Uri.file(mapping); 162 Uri libraryEntry = new Uri.file(mapping);
65 if (!libraryEntry.isAbsolute) { 163 if (!libraryEntry.isAbsolute) {
66 // We expect an absolute path. 164 // We expect an absolute path.
67 return null; 165 return null;
68 } 166 }
69 167
70 if (partPath != null) { 168 if (partPath != null) {
71 return _resolvePart(libraryEntry, partPath, importUri); 169 return _resolvePart(libraryEntry, partPath, importUri);
72 } else { 170 } else {
73 return _resolveEntry(libraryEntry, importUri); 171 return _resolveEntry(libraryEntry, importUri);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 /// Return the part path of [importUri]. 206 /// Return the part path of [importUri].
109 String _partPath(Uri importUri) { 207 String _partPath(Uri importUri) {
110 var uri = importUri.toString(); 208 var uri = importUri.toString();
111 int index = uri.indexOf('/'); 209 int index = uri.indexOf('/');
112 if (index >= 0) { 210 if (index >= 0) {
113 return uri.substring(index + 1); 211 return uri.substring(index + 1);
114 } 212 }
115 return null; 213 return null;
116 } 214 }
117 215
118 /// Given a package [name] and a list of folders ([libDirs]),
119 /// add any found sdk extensions.
120 void _processPackage(String name, List<Folder> libDirs) {
121 for (var libDir in libDirs) {
122 var sdkExt = _readDotSdkExt(libDir);
123 if (sdkExt != null) {
124 _processSdkExt(sdkExt, libDir);
125 }
126 }
127 }
128
129 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder
130 /// ([libDir]), setup the uri mapping.
131 void _processSdkExt(String sdkExtJSON, Folder libDir) {
132 var sdkExt;
133 try {
134 sdkExt = JSON.decode(sdkExtJSON);
135 } catch (e) {
136 return;
137 }
138 if ((sdkExt == null) || (sdkExt is! Map)) {
139 return;
140 }
141 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir));
142 }
143
144 /// Install the mapping from [name] to [libDir]/[file].
145 void _processSdkExtension(String name, String file, Folder libDir) {
146 if (!name.startsWith(DART_COLON_PREFIX)) {
147 // SDK extensions must begin with 'dart:'.
148 return;
149 }
150 var key = name;
151 var value = libDir.canonicalizePath(file);
152 _urlMappings[key] = value;
153 }
154
155 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string.
156 /// Returns null if the file doesn't exist.
157 String _readDotSdkExt(Folder libDir) {
158 var file = libDir.getChild(SDK_EXT_NAME);
159 try {
160 return file.readAsStringSync();
161 } on FileSystemException {
162 // File can't be read.
163 return null;
164 }
165 }
166
167 /// Resolve an import of an sdk extension. 216 /// Resolve an import of an sdk extension.
168 Source _resolveEntry(Uri libraryEntry, Uri importUri) { 217 Source _resolveEntry(Uri libraryEntry, Uri importUri) {
169 // Library entry. 218 // Library entry.
170 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); 219 JavaFile javaFile = new JavaFile.fromUri(libraryEntry);
171 return new FileBasedSource(javaFile, importUri); 220 return new FileBasedSource(javaFile, importUri);
172 } 221 }
173 222
174 /// Resolve a 'part' statement inside an sdk extension. 223 /// Resolve a 'part' statement inside an sdk extension.
175 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { 224 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) {
176 // Library part. 225 // Library part.
177 var directory = pathos.dirname(libraryEntry.path); 226 var directory = pathos.dirname(libraryEntry.path);
178 var partUri = new Uri.file(pathos.join(directory, partPath)); 227 var partUri = new Uri.file(pathos.join(directory, partPath));
179 assert(partUri.isAbsolute); 228 assert(partUri.isAbsolute);
180 JavaFile javaFile = new JavaFile.fromUri(partUri); 229 JavaFile javaFile = new JavaFile.fromUri(partUri);
181 return new FileBasedSource(javaFile, importUri); 230 return new FileBasedSource(javaFile, importUri);
182 } 231 }
183 } 232 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698