| 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 analyzer.source.sdk_ext; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:core' hide Resource; | 8 import 'dart:core'; |
| 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' |
| 12 show PackageMapProvider; |
| 11 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 13 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 12 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | 15 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
| 14 import 'package:path/path.dart' as pathos; | 16 import 'package:path/path.dart' as pathos; |
| 15 | 17 |
| 16 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib | 18 /// 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 | 19 /// directory for the existence of a `_sdkext` file. This file must contain a |
| 18 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value | 20 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value |
| 19 /// is a path (relative to the directory containing `_sdkext`) to a dart script | 21 /// is a path (relative to the directory containing `_sdkext`) to a dart script |
| 20 /// for the given library. For example: | 22 /// for the given library. For example: |
| 21 /// { | 23 /// { |
| 22 /// "dart:sky": "../sdk_ext/dart_sky.dart" | 24 /// "dart:sky": "../sdk_ext/dart_sky.dart" |
| 23 /// } | 25 /// } |
| 24 /// | 26 /// |
| 25 /// If a key doesn't begin with `dart:` it is ignored. | 27 /// If a key doesn't begin with `dart:` it is ignored. |
| 26 class SdkExtUriResolver extends UriResolver { | 28 class SdkExtUriResolver extends UriResolver { |
| 27 static const String SDK_EXT_NAME = '_sdkext'; | 29 static const String SDK_EXT_NAME = '_sdkext'; |
| 28 static const String DART_COLON_PREFIX = 'dart:'; | 30 static const String DART_COLON_PREFIX = 'dart:'; |
| 29 | 31 |
| 30 final Map<String, String> _urlMappings = <String, String>{}; | 32 final Map<String, String> _urlMappings = <String, String>{}; |
| 31 | 33 |
| 34 /** |
| 35 * The absolute paths of the extension files that contributed to the |
| 36 * [_urlMappings]. |
| 37 */ |
| 38 final List<String> extensionFilePaths = <String>[]; |
| 39 |
| 32 /// Construct a [SdkExtUriResolver] from a package map | 40 /// Construct a [SdkExtUriResolver] from a package map |
| 33 /// (see [PackageMapProvider]). | 41 /// (see [PackageMapProvider]). |
| 34 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { | 42 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { |
| 35 if (packageMap == null) { | 43 if (packageMap == null) { |
| 36 return; | 44 return; |
| 37 } | 45 } |
| 38 packageMap.forEach(_processPackage); | 46 packageMap.forEach(_processPackage); |
| 39 } | 47 } |
| 40 | 48 |
| 41 /// Number of sdk extensions. | 49 /// Number of sdk extensions. |
| 42 int get length => _urlMappings.length; | 50 int get length => _urlMappings.length; |
| 43 | 51 |
| 52 /** |
| 53 * Return a table mapping the names of extensions to the paths where those |
| 54 * extensions can be found. |
| 55 */ |
| 56 Map<String, String> get urlMappings => |
| 57 new Map<String, String>.from(_urlMappings); |
| 58 |
| 44 /// Return the path mapping for [libName] or null if there is none. | 59 /// Return the path mapping for [libName] or null if there is none. |
| 45 String operator [](String libName) => _urlMappings[libName]; | 60 String operator [](String libName) => _urlMappings[libName]; |
| 46 | 61 |
| 47 /// Programmatically add a new SDK extension given a JSON description | 62 /// Programmatically add a new SDK extension given a JSON description |
| 48 /// ([sdkExtJSON]) and a lib directory ([libDir]). | 63 /// ([sdkExtJSON]) and a lib directory ([libDir]). |
| 49 void addSdkExt(String sdkExtJSON, Folder libDir) { | 64 void addSdkExt(String sdkExtJSON, Folder libDir) { |
| 50 _processSdkExt(sdkExtJSON, libDir); | 65 _processSdkExt(sdkExtJSON, libDir); |
| 51 } | 66 } |
| 52 | 67 |
| 53 @override | 68 @override |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 void _processSdkExt(String sdkExtJSON, Folder libDir) { | 146 void _processSdkExt(String sdkExtJSON, Folder libDir) { |
| 132 var sdkExt; | 147 var sdkExt; |
| 133 try { | 148 try { |
| 134 sdkExt = JSON.decode(sdkExtJSON); | 149 sdkExt = JSON.decode(sdkExtJSON); |
| 135 } catch (e) { | 150 } catch (e) { |
| 136 return; | 151 return; |
| 137 } | 152 } |
| 138 if ((sdkExt == null) || (sdkExt is! Map)) { | 153 if ((sdkExt == null) || (sdkExt is! Map)) { |
| 139 return; | 154 return; |
| 140 } | 155 } |
| 141 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir)); | 156 bool contributed = false; |
| 157 sdkExt.forEach((k, v) { |
| 158 if (_processSdkExtension(k, v, libDir)) { |
| 159 contributed = true; |
| 160 } |
| 161 }); |
| 162 if (contributed) { |
| 163 extensionFilePaths.add(libDir.getChild(SDK_EXT_NAME).path); |
| 164 } |
| 142 } | 165 } |
| 143 | 166 |
| 144 /// Install the mapping from [name] to [libDir]/[file]. | 167 /// Install the mapping from [name] to [libDir]/[file]. |
| 145 void _processSdkExtension(String name, String file, Folder libDir) { | 168 bool _processSdkExtension(String name, String file, Folder libDir) { |
| 146 if (!name.startsWith(DART_COLON_PREFIX)) { | 169 if (!name.startsWith(DART_COLON_PREFIX)) { |
| 147 // SDK extensions must begin with 'dart:'. | 170 // SDK extensions must begin with 'dart:'. |
| 148 return; | 171 return false; |
| 149 } | 172 } |
| 150 var key = name; | 173 var key = name; |
| 151 var value = libDir.canonicalizePath(file); | 174 var value = libDir.canonicalizePath(file); |
| 152 _urlMappings[key] = value; | 175 _urlMappings[key] = value; |
| 176 return true; |
| 153 } | 177 } |
| 154 | 178 |
| 155 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string. | 179 /// Read the contents of [libDir]/[SDK_EXT_NAME] as a string. |
| 156 /// Returns null if the file doesn't exist. | 180 /// Returns null if the file doesn't exist. |
| 157 String _readDotSdkExt(Folder libDir) { | 181 String _readDotSdkExt(Folder libDir) { |
| 158 var file = libDir.getChild(SDK_EXT_NAME); | 182 File file = libDir.getChild(SDK_EXT_NAME); |
| 159 try { | 183 try { |
| 160 return file.readAsStringSync(); | 184 return file.readAsStringSync(); |
| 161 } on FileSystemException { | 185 } on FileSystemException { |
| 162 // File can't be read. | 186 // File can't be read. |
| 163 return null; | 187 return null; |
| 164 } | 188 } |
| 165 } | 189 } |
| 166 | 190 |
| 167 /// Resolve an import of an sdk extension. | 191 /// Resolve an import of an sdk extension. |
| 168 Source _resolveEntry(Uri libraryEntry, Uri importUri) { | 192 Source _resolveEntry(Uri libraryEntry, Uri importUri) { |
| 169 // Library entry. | 193 // Library entry. |
| 170 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); | 194 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); |
| 171 return new FileBasedSource(javaFile, importUri); | 195 return new FileBasedSource(javaFile, importUri); |
| 172 } | 196 } |
| 173 | 197 |
| 174 /// Resolve a 'part' statement inside an sdk extension. | 198 /// Resolve a 'part' statement inside an sdk extension. |
| 175 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { | 199 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { |
| 176 // Library part. | 200 // Library part. |
| 177 var directory = pathos.dirname(libraryEntry.path); | 201 var directory = pathos.dirname(libraryEntry.path); |
| 178 var partUri = new Uri.file(pathos.join(directory, partPath)); | 202 var partUri = new Uri.file(pathos.join(directory, partPath)); |
| 179 assert(partUri.isAbsolute); | 203 assert(partUri.isAbsolute); |
| 180 JavaFile javaFile = new JavaFile.fromUri(partUri); | 204 JavaFile javaFile = new JavaFile.fromUri(partUri); |
| 181 return new FileBasedSource(javaFile, importUri); | 205 return new FileBasedSource(javaFile, importUri); |
| 182 } | 206 } |
| 183 } | 207 } |
| OLD | NEW |