OLD | NEW |
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 analyzer.source.embedder; | 5 library analyzer.source.embedder; |
6 | 6 |
7 import 'dart:collection' show HashMap; | 7 import 'dart:collection' show HashMap; |
8 import 'dart:core' hide Resource; | 8 import 'dart:core' hide Resource; |
9 | 9 |
10 import 'package:analyzer/file_system/file_system.dart'; | 10 import 'package:analyzer/file_system/file_system.dart'; |
11 import 'package:analyzer/source/package_map_provider.dart' | 11 import 'package:analyzer/source/package_map_provider.dart' |
12 show PackageMapProvider; | 12 show PackageMapProvider; |
13 import 'package:analyzer/src/generated/java_core.dart'; | 13 import 'package:analyzer/src/generated/java_core.dart'; |
14 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 14 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
15 import 'package:analyzer/src/generated/sdk.dart'; | 15 import 'package:analyzer/src/generated/sdk.dart'; |
16 import 'package:analyzer/src/generated/sdk_io.dart'; | 16 import 'package:analyzer/src/generated/sdk_io.dart'; |
17 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
18 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | 18 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
19 import 'package:analyzer/src/summary/idl.dart'; | 19 import 'package:analyzer/src/summary/idl.dart'; |
20 import 'package:yaml/yaml.dart'; | 20 import 'package:yaml/yaml.dart'; |
21 | 21 |
| 22 export 'package:analyzer/src/context/builder.dart' show EmbedderYamlLocator; |
| 23 |
22 const String _DART_COLON_PREFIX = 'dart:'; | 24 const String _DART_COLON_PREFIX = 'dart:'; |
23 const String _EMBEDDED_LIB_MAP_KEY = 'embedded_libs'; | 25 const String _EMBEDDED_LIB_MAP_KEY = 'embedded_libs'; |
24 | 26 |
25 /// Check if this map defines embedded libraries. | 27 /// Check if this map defines embedded libraries. |
26 bool definesEmbeddedLibs(Map map) => map[_EMBEDDED_LIB_MAP_KEY] != null; | 28 bool definesEmbeddedLibs(Map map) => map[_EMBEDDED_LIB_MAP_KEY] != null; |
27 | 29 |
28 /// An SDK backed by URI mappings derived from an `_embedder.yaml` file. | 30 /// An SDK backed by URI mappings derived from an `_embedder.yaml` file. |
29 class EmbedderSdk extends AbstractDartSdk { | 31 class EmbedderSdk extends AbstractDartSdk { |
30 final Map<String, String> _urlMappings = new HashMap<String, String>(); | 32 final Map<String, String> _urlMappings = new HashMap<String, String>(); |
31 | 33 |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 @override | 170 @override |
169 Uri restoreAbsolute(Source source) { | 171 Uri restoreAbsolute(Source source) { |
170 String path = source.fullName; | 172 String path = source.fullName; |
171 if (path.length > 3 && path[1] == ':' && path[2] == '\\') { | 173 if (path.length > 3 && path[1] == ':' && path[2] == '\\') { |
172 path = '/${path[0]}:${path.substring(2).replaceAll('\\', '/')}'; | 174 path = '/${path[0]}:${path.substring(2).replaceAll('\\', '/')}'; |
173 } | 175 } |
174 Source sdkSource = dartSdk.fromFileUri(Uri.parse('file://$path')); | 176 Source sdkSource = dartSdk.fromFileUri(Uri.parse('file://$path')); |
175 return sdkSource?.uri; | 177 return sdkSource?.uri; |
176 } | 178 } |
177 } | 179 } |
178 | |
179 /// Given a packageMap, check in each package's lib directory for the | |
180 /// existence of an `_embedder.yaml` file. If the file contains a top level | |
181 /// YamlMap, it will be added to the [embedderYamls] map. | |
182 class EmbedderYamlLocator { | |
183 static const String EMBEDDER_FILE_NAME = '_embedder.yaml'; | |
184 | |
185 /// Map from package's library directory to the parsed YamlMap. | |
186 final Map<Folder, YamlMap> embedderYamls = new HashMap<Folder, YamlMap>(); | |
187 | |
188 EmbedderYamlLocator(Map<String, List<Folder>> packageMap) { | |
189 if (packageMap != null) { | |
190 _processPackageMap(packageMap); | |
191 } | |
192 } | |
193 | |
194 /// Programatically add an _embedder.yaml mapping. | |
195 void addEmbedderYaml(Folder libDir, String embedderYaml) { | |
196 _processEmbedderYaml(libDir, embedderYaml); | |
197 } | |
198 | |
199 void refresh(Map<String, List<Folder>> packageMap) { | |
200 // Clear existing. | |
201 embedderYamls.clear(); | |
202 if (packageMap != null) { | |
203 _processPackageMap(packageMap); | |
204 } | |
205 } | |
206 | |
207 /// Given the yaml for an embedder ([embedderYaml]) and a folder | |
208 /// ([libDir]), setup the uri mapping. | |
209 void _processEmbedderYaml(Folder libDir, String embedderYaml) { | |
210 YamlNode yaml; | |
211 try { | |
212 yaml = loadYaml(embedderYaml); | |
213 } catch (_) { | |
214 return; | |
215 } | |
216 if (yaml is! YamlMap) { | |
217 return; | |
218 } | |
219 embedderYamls[libDir] = yaml; | |
220 } | |
221 | |
222 /// Given a package [name] and a list of folders ([libDirs]), | |
223 /// add any found `_embedder.yaml` files. | |
224 void _processPackage(String name, List<Folder> libDirs) { | |
225 for (Folder libDir in libDirs) { | |
226 String embedderYaml = _readEmbedderYaml(libDir); | |
227 if (embedderYaml != null) { | |
228 _processEmbedderYaml(libDir, embedderYaml); | |
229 } | |
230 } | |
231 } | |
232 | |
233 void _processPackageMap(Map<String, List<Folder>> packageMap) { | |
234 packageMap.forEach(_processPackage); | |
235 } | |
236 | |
237 /// Read the contents of [libDir]/[EMBEDDER_FILE_NAME] as a string. | |
238 /// Returns null if the file doesn't exist. | |
239 String _readEmbedderYaml(Folder libDir) { | |
240 File file = libDir.getChild(EMBEDDER_FILE_NAME); | |
241 try { | |
242 return file.readAsStringSync(); | |
243 } on FileSystemException { | |
244 // File can't be read. | |
245 return null; | |
246 } | |
247 } | |
248 } | |
OLD | NEW |