Chromium Code Reviews| 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 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 (see [PackageMapProvider]), check in each package's lib |
| 17 /// directory for the existence of a `_sdkext` file. This file must contain a | 17 /// directory for the existence of a `_embedder.yaml` file. This file must |
| 18 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value | 18 /// contain a yaml map with the top level key 'embedder_libs'. Under the |
| 19 /// is a path (relative to the directory containing `_sdkext`) to a dart script | 19 /// 'embedder_libs' key are key value pairs. Each key is a 'dart:' library uri |
| 20 /// for the given library. For example: | 20 /// Each value is a path (relative to the directory containing `_embedder.yaml`) |
| 21 /// { | 21 /// to a dart script for the given library. For example: |
| 22 /// "dart:sky": "../sdk_ext/dart_sky.dart" | 22 /// embedder_libs: |
| 23 /// } | 23 /// 'dart:io': '../../sdk/io/io.dart' |
| 24 /// | 24 /// |
| 25 /// If a key doesn't begin with `dart:` it is ignored. | 25 /// If a key doesn't begin with `dart:` it is ignored. |
| 26 class SdkExtUriResolver extends UriResolver { | 26 /// |
| 27 static const String SDK_EXT_NAME = '_sdkext'; | 27 class EmbedderUriResolver extends UriResolver { |
| 28 static const String EMBEDDER_FILE_NAME = '_embedder.yaml'; | |
| 28 static const String DART_COLON_PREFIX = 'dart:'; | 29 static const String DART_COLON_PREFIX = 'dart:'; |
| 29 | 30 |
| 30 final Map<String, String> _urlMappings = <String, String>{}; | 31 final Map<String, String> _urlMappings = <String, String>{}; |
| 31 | 32 |
| 32 /// Construct a [SdkExtUriResolver] from a package map | 33 /// Construct a [EmbedderUriResolver] from a package map |
| 33 /// (see [PackageMapProvider]). | 34 /// (see [PackageMapProvider]). |
| 34 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { | 35 EmbedderUriResolver(Map<String, List<Folder>> packageMap) { |
| 35 if (packageMap == null) { | 36 if (packageMap == null) { |
| 36 return; | 37 return; |
| 37 } | 38 } |
| 38 packageMap.forEach(_processPackage); | 39 packageMap.forEach(_processPackage); |
| 39 } | 40 } |
| 40 | 41 |
| 41 /// Number of sdk extensions. | 42 /// Number of embedder libraries. |
| 42 int get length => _urlMappings.length; | 43 int get length => _urlMappings.length; |
| 43 | 44 |
| 44 /// Return the path mapping for [libName] or null if there is none. | 45 /// Return the path mapping for [libName] or null if there is none. |
| 45 String operator [](String libName) => _urlMappings[libName]; | 46 String operator [](String libName) => _urlMappings[libName]; |
| 46 | 47 |
| 47 /// Programmatically add a new SDK extension given a JSON description | 48 /// Programmatically add a new embedder given a yaml description |
| 48 /// ([sdkExtJSON]) and a lib directory ([libDir]). | 49 /// ([embedderYaml]) and a lib directory ([libDir]). |
| 49 void addSdkExt(String sdkExtJSON, Folder libDir) { | 50 void addEmbedderYaml(String embedderYaml, Folder libDir) { |
| 50 _processSdkExt(sdkExtJSON, libDir); | 51 _processEmbedderYaml(embedderYaml, libDir); |
| 51 } | 52 } |
| 52 | 53 |
| 53 @override | 54 @override |
| 54 Source resolveAbsolute(Uri importUri, [Uri actualUri]) { | 55 Source resolveAbsolute(Uri importUri, [Uri actualUri]) { |
| 55 String libraryName = _libraryName(importUri); | 56 String libraryName = _libraryName(importUri); |
| 56 String partPath = _partPath(importUri); | 57 String partPath = _partPath(importUri); |
| 57 // Lookup library name in mappings. | 58 // Lookup library name in mappings. |
| 58 String mapping = _urlMappings[libraryName]; | 59 String mapping = _urlMappings[libraryName]; |
| 59 if (mapping == null) { | 60 if (mapping == null) { |
| 60 // Not found. | 61 // Not found. |
| 61 return null; | 62 return null; |
| 62 } | 63 } |
| 63 // This mapping points to the main entry file of the sdk extension. | 64 // This mapping points to the main entry file of the dart: library. |
| 64 Uri libraryEntry = new Uri.file(mapping); | 65 Uri libraryEntry = new Uri.file(mapping); |
| 65 if (!libraryEntry.isAbsolute) { | 66 if (!libraryEntry.isAbsolute) { |
| 66 // We expect an absolute path. | 67 // We expect an absolute path. |
| 67 return null; | 68 return null; |
| 68 } | 69 } |
| 69 | 70 |
| 70 if (partPath != null) { | 71 if (partPath != null) { |
| 71 return _resolvePart(libraryEntry, partPath, importUri); | 72 return _resolvePart(libraryEntry, partPath, importUri); |
| 72 } else { | 73 } else { |
| 73 return _resolveEntry(libraryEntry, importUri); | 74 return _resolveEntry(libraryEntry, importUri); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 109 String _partPath(Uri importUri) { | 110 String _partPath(Uri importUri) { |
| 110 var uri = importUri.toString(); | 111 var uri = importUri.toString(); |
| 111 int index = uri.indexOf('/'); | 112 int index = uri.indexOf('/'); |
| 112 if (index >= 0) { | 113 if (index >= 0) { |
| 113 return uri.substring(index + 1); | 114 return uri.substring(index + 1); |
| 114 } | 115 } |
| 115 return null; | 116 return null; |
| 116 } | 117 } |
| 117 | 118 |
| 118 /// Given a package [name] and a list of folders ([libDirs]), | 119 /// Given a package [name] and a list of folders ([libDirs]), |
| 119 /// add any found sdk extensions. | 120 /// add any found `_embedder.yaml` files. |
| 120 void _processPackage(String name, List<Folder> libDirs) { | 121 void _processPackage(String name, List<Folder> libDirs) { |
| 121 for (var libDir in libDirs) { | 122 for (var libDir in libDirs) { |
| 122 var sdkExt = _readDotSdkExt(libDir); | 123 var embedderYaml = _readEmbedderYaml(libDir); |
| 123 if (sdkExt != null) { | 124 if (embedderYaml != null) { |
| 124 _processSdkExt(sdkExt, libDir); | 125 _processEmbedderYaml(embedderYaml, libDir); |
| 125 } | 126 } |
| 126 } | 127 } |
| 127 } | 128 } |
| 128 | 129 |
| 129 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder | 130 /// Given the yaml for an embedder ([embedderYaml]) and a folder |
| 130 /// ([libDir]), setup the uri mapping. | 131 /// ([libDir]), setup the uri mapping. |
| 131 void _processSdkExt(String sdkExtJSON, Folder libDir) { | 132 void _processEmbedderYaml(String embedderYaml, Folder libDir) { |
| 132 var sdkExt; | 133 YamlNode yaml; |
| 133 try { | 134 try { |
| 134 sdkExt = JSON.decode(sdkExtJSON); | 135 yaml = loadYaml(embedderYaml); |
| 135 } catch (e) { | 136 } catch (e) { |
|
pquitslund
2015/11/11 19:19:09
Maybe a TODO to do some error handling down the ro
Cutch
2015/11/12 14:13:43
Done.
| |
| 136 return; | 137 return; |
| 137 } | 138 } |
| 138 if ((sdkExt == null) || (sdkExt is! Map)) { | 139 if ((yaml == null) || (yaml is! YamlMap)) { |
| 139 return; | 140 return; |
| 140 } | 141 } |
| 141 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir)); | 142 YamlNode embedder_libs = (yaml as YamlMap)['embedder_libs']; |
| 143 if ((embedder_libs == null) || (embedder_libs is! YamlMap)) { | |
|
pquitslund
2015/11/11 19:19:09
Maybe just:
if (embedder_libs is! YamlMap) {
.
Brian Wilkerson
2015/11/11 22:09:14
Nope. e is! T is always true (unless T is Object,
Cutch
2015/11/12 14:13:43
Acknowledged.
Cutch
2015/11/12 14:13:43
Acknowledged.
| |
| 144 return; | |
| 145 } | |
| 146 (embedder_libs as YamlMap).forEach((k, v) => | |
| 147 _processEmbedderLibs(k, v, libDir)); | |
| 142 } | 148 } |
| 143 | 149 |
| 144 /// Install the mapping from [name] to [libDir]/[file]. | 150 /// Install the mapping from [name] to [libDir]/[file]. |
| 145 void _processSdkExtension(String name, String file, Folder libDir) { | 151 void _processEmbedderLibs(String name, String file, Folder libDir) { |
| 146 if (!name.startsWith(DART_COLON_PREFIX)) { | 152 if (!name.startsWith(DART_COLON_PREFIX)) { |
| 147 // SDK extensions must begin with 'dart:'. | 153 // SDK libraries must begin with 'dart:'. |
| 148 return; | 154 return; |
| 149 } | 155 } |
| 150 var key = name; | 156 var key = name; |
| 151 var value = libDir.canonicalizePath(file); | 157 var value = libDir.canonicalizePath(file); |
| 152 _urlMappings[key] = value; | 158 _urlMappings[key] = value; |
| 153 } | 159 } |
| 154 | 160 |
| 155 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string. | 161 /// Read the contents of [libDir]/[EMBEDDER_FILE_NAME] as a string. |
| 156 /// Returns null if the file doesn't exist. | 162 /// Returns null if the file doesn't exist. |
| 157 String _readDotSdkExt(Folder libDir) { | 163 String _readEmbedderYaml(Folder libDir) { |
| 158 var file = libDir.getChild(SDK_EXT_NAME); | 164 var file = libDir.getChild(EMBEDDER_FILE_NAME); |
| 159 try { | 165 try { |
| 160 return file.readAsStringSync(); | 166 return file.readAsStringSync(); |
| 161 } on FileSystemException { | 167 } on FileSystemException { |
| 162 // File can't be read. | 168 // File can't be read. |
| 163 return null; | 169 return null; |
| 164 } | 170 } |
| 165 } | 171 } |
| 166 | 172 |
| 167 /// Resolve an import of an sdk extension. | 173 /// Resolve an import of an sdk extension. |
| 168 Source _resolveEntry(Uri libraryEntry, Uri importUri) { | 174 Source _resolveEntry(Uri libraryEntry, Uri importUri) { |
| 169 // Library entry. | 175 // Library entry. |
| 170 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); | 176 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); |
| 171 return new FileBasedSource(javaFile, importUri); | 177 return new FileBasedSource(javaFile, importUri); |
| 172 } | 178 } |
| 173 | 179 |
| 174 /// Resolve a 'part' statement inside an sdk extension. | 180 /// Resolve a 'part' statement inside an sdk extension. |
| 175 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { | 181 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { |
| 176 // Library part. | 182 // Library part. |
| 177 var directory = pathos.dirname(libraryEntry.path); | 183 var directory = pathos.dirname(libraryEntry.path); |
| 178 var partUri = new Uri.file(pathos.join(directory, partPath)); | 184 var partUri = new Uri.file(pathos.join(directory, partPath)); |
| 179 assert(partUri.isAbsolute); | 185 assert(partUri.isAbsolute); |
| 180 JavaFile javaFile = new JavaFile.fromUri(partUri); | 186 JavaFile javaFile = new JavaFile.fromUri(partUri); |
| 181 return new FileBasedSource(javaFile, importUri); | 187 return new FileBasedSource(javaFile, importUri); |
| 182 } | 188 } |
| 183 } | 189 } |
| OLD | NEW |