| 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.sdk_ext; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 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/src/generated/java_io.dart' show JavaFile; | 11 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 12 import 'package:analyzer/src/generated/source.dart'; | 12 import 'package:analyzer/src/generated/source.dart'; |
| 13 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | 13 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
| 14 import 'package:path/path.dart' as pathos; | 14 import 'package:path/path.dart' as pathos; |
| 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 `_sdkext` file. This file must contain a |
| 18 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value | 18 /// 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 | 19 /// is a path (relative to the directory containing `_sdkext`) to a dart script |
| 20 /// for the given library. For example: | 20 /// for the given library. For example: |
| 21 /// { | 21 /// { |
| 22 /// "dart:sky": "../sdk_ext/dart_sky.dart" | 22 /// "dart:sky": "../sdk_ext/dart_sky.dart" |
| 23 /// } | 23 /// } |
| 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 class SdkExtUriResolver extends UriResolver { |
| 27 static const String SDK_EXT_NAME = '_sdkext'; | 27 static const String SDK_EXT_NAME = '_sdkext'; |
| 28 static const String DART_COLON_PREFIX = 'dart:'; | 28 static const String DART_COLON_PREFIX = 'dart:'; |
| 29 | 29 |
| 30 final Map<String, String> _urlMappings = <String,String>{}; | 30 final Map<String, String> _urlMappings = <String, String>{}; |
| 31 | 31 |
| 32 /// Construct a [SdkExtUriResolver] from a package map | 32 /// Construct a [SdkExtUriResolver] from a package map |
| 33 /// (see [PackageMapProvider]). | 33 /// (see [PackageMapProvider]). |
| 34 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { | 34 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { |
| 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 /// Number of sdk extensions. |
| 42 int get length => _urlMappings.length; | 42 int get length => _urlMappings.length; |
| 43 | 43 |
| 44 /// Return the path mapping for [libName] or null if there is none. | 44 /// Return the path mapping for [libName] or null if there is none. |
| 45 String operator[](String libName) => _urlMappings[libName]; | 45 String operator [](String libName) => _urlMappings[libName]; |
| 46 | 46 |
| 47 /// Programmatically add a new SDK extension given a JSON description | 47 /// Programmatically add a new SDK extension given a JSON description |
| 48 /// ([sdkExtJSON]) and a lib directory ([libDir]). | 48 /// ([sdkExtJSON]) and a lib directory ([libDir]). |
| 49 void addSdkExt(String sdkExtJSON, Folder libDir) { | 49 void addSdkExt(String sdkExtJSON, Folder libDir) { |
| 50 _processSdkExt(sdkExtJSON, libDir); | 50 _processSdkExt(sdkExtJSON, libDir); |
| 51 } | 51 } |
| 52 | 52 |
| 53 @override | 53 @override |
| 54 Source resolveAbsolute(Uri importUri, [Uri actualUri]) { | 54 Source resolveAbsolute(Uri importUri, [Uri actualUri]) { |
| 55 String libraryName = _libraryName(importUri); | 55 String libraryName = _libraryName(importUri); |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 /// Resolve a 'part' statement inside an sdk extension. | 174 /// Resolve a 'part' statement inside an sdk extension. |
| 175 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { | 175 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { |
| 176 // Library part. | 176 // Library part. |
| 177 var directory = pathos.dirname(libraryEntry.path); | 177 var directory = pathos.dirname(libraryEntry.path); |
| 178 var partUri = new Uri.file(pathos.join(directory, partPath)); | 178 var partUri = new Uri.file(pathos.join(directory, partPath)); |
| 179 assert(partUri.isAbsolute); | 179 assert(partUri.isAbsolute); |
| 180 JavaFile javaFile = new JavaFile.fromUri(partUri); | 180 JavaFile javaFile = new JavaFile.fromUri(partUri); |
| 181 return new FileBasedSource(javaFile, importUri); | 181 return new FileBasedSource(javaFile, importUri); |
| 182 } | 182 } |
| 183 } | 183 } |
| OLD | NEW |