| 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:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 import 'dart:convert'; | 9 import 'dart:convert'; |
| 10 import 'dart:core' hide Resource; | 10 import 'dart:core' hide Resource; |
| 11 | 11 |
| 12 import 'package:analyzer/file_system/file_system.dart'; | 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; | 13 import 'package:analyzer/source/package_map_resolver.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 16 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | 16 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
| 17 import 'package:path/path.dart' as pathos; | 17 import 'package:path/path.dart' as pathos; |
| 18 | 18 |
| 19 class _SdkExtFileBasedSource extends FileBasedSource { | |
| 20 _SdkExtFileBasedSource(JavaFile file, Uri uri) | |
| 21 : super(file, uri); | |
| 22 } | |
| 23 | |
| 24 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib | 19 /// Given a packageMap (see [PackageMapProvider]), check in each package's lib |
| 25 /// directory for the existence of a `.sdkext` file. This file must contain a | 20 /// directory for the existence of a `.sdkext` file. This file must contain a |
| 26 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value | 21 /// JSON encoded map. Each key in the map is a `dart:` library name. Each value |
| 27 /// is a path (relative to the directory containing `.sdkext`) to a dart script | 22 /// is a path (relative to the directory containing `.sdkext`) to a dart script |
| 28 /// for the given library. For example: | 23 /// for the given library. For example: |
| 29 /// { | 24 /// { |
| 30 /// "dart:sky": "../sdk_ext/dart_sky.dart" | 25 /// "dart:sky": "../sdk_ext/dart_sky.dart" |
| 31 /// } | 26 /// } |
| 32 /// | 27 /// |
| 33 /// If a key doesn't begin with `dart:` it is ignored. | 28 /// If a key doesn't begin with `dart:` it is ignored. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 58 /// Number of sdk extensions. | 53 /// Number of sdk extensions. |
| 59 int get length => _urlMappings.length; | 54 int get length => _urlMappings.length; |
| 60 | 55 |
| 61 /// Resolve a 'part' statement inside an sdk extension. | 56 /// Resolve a 'part' statement inside an sdk extension. |
| 62 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { | 57 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { |
| 63 // Library part. | 58 // Library part. |
| 64 var directory = pathos.dirname(libraryEntry.path); | 59 var directory = pathos.dirname(libraryEntry.path); |
| 65 var partUri = new Uri.file(pathos.join(directory, partPath)); | 60 var partUri = new Uri.file(pathos.join(directory, partPath)); |
| 66 assert(partUri.isAbsolute); | 61 assert(partUri.isAbsolute); |
| 67 JavaFile javaFile = new JavaFile.fromUri(partUri); | 62 JavaFile javaFile = new JavaFile.fromUri(partUri); |
| 68 return new _SdkExtFileBasedSource(javaFile, importUri); | 63 return new FileBasedSource(javaFile, importUri); |
| 69 } | 64 } |
| 70 | 65 |
| 71 /// Resolve an import of an sdk extension. | 66 /// Resolve an import of an sdk extension. |
| 72 Source _resolveEntry(Uri libraryEntry, Uri importUri) { | 67 Source _resolveEntry(Uri libraryEntry, Uri importUri) { |
| 73 // Library entry. | 68 // Library entry. |
| 74 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); | 69 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); |
| 75 return new _SdkExtFileBasedSource(javaFile, importUri); | 70 return new FileBasedSource(javaFile, importUri); |
| 71 } |
| 72 |
| 73 /// Return the library name of [importUri]. |
| 74 String _libraryName(Uri importUri) { |
| 75 var uri = importUri.toString(); |
| 76 int index = uri.indexOf('/'); |
| 77 if (index >= 0) { |
| 78 return uri.substring(0, index); |
| 79 } |
| 80 return uri; |
| 81 } |
| 82 |
| 83 /// Return the part path of [importUri]. |
| 84 String _partPath(Uri importUri) { |
| 85 var uri = importUri.toString(); |
| 86 int index = uri.indexOf('/'); |
| 87 if (index >= 0) { |
| 88 return uri.substring(index + 1); |
| 89 } |
| 90 return null; |
| 91 } |
| 92 |
| 93 /// Returns true if [libraryName] is a registered sdk extension. |
| 94 bool _registeredSdkExtension(String libraryName) { |
| 95 return _urlMappings[libraryName] != null; |
| 76 } | 96 } |
| 77 | 97 |
| 78 @override | 98 @override |
| 79 Source resolveAbsolute(Uri importUri) { | 99 Source resolveAbsolute(Uri importUri) { |
| 80 // Split import uri into library name and (optionally) a part path. | 100 String libraryName = _libraryName(importUri); |
| 81 var uri = importUri.toString(); | 101 String partPath = _partPath(importUri); |
| 82 String libraryName; | |
| 83 String partPath; | |
| 84 int index = uri.indexOf('/'); | |
| 85 if (index >= 0) { | |
| 86 libraryName = uri.substring(0, index); | |
| 87 partPath = uri.substring(index + 1); | |
| 88 } else { | |
| 89 libraryName = uri; | |
| 90 } | |
| 91 // Lookup library name in mappings. | 102 // Lookup library name in mappings. |
| 92 String mapping = _urlMappings[libraryName]; | 103 String mapping = _urlMappings[libraryName]; |
| 93 if (mapping == null) { | 104 if (mapping == null) { |
| 94 // Not found. | 105 // Not found. |
| 95 return null; | 106 return null; |
| 96 } | 107 } |
| 97 // This mapping points to the main entry file of the sdk extension. | 108 // This mapping points to the main entry file of the sdk extension. |
| 98 Uri libraryEntry = new Uri.file(mapping); | 109 Uri libraryEntry = new Uri.file(mapping); |
| 99 if (!libraryEntry.isAbsolute) { | 110 if (!libraryEntry.isAbsolute) { |
| 100 // We expect an absolute path. | 111 // We expect an absolute path. |
| 101 return null; | 112 return null; |
| 102 } | 113 } |
| 103 | 114 |
| 104 if (index >= 0) { | 115 if (partPath != null) { |
| 105 return _resolvePart(libraryEntry, partPath, importUri); | 116 return _resolvePart(libraryEntry, partPath, importUri); |
| 106 } else { | 117 } else { |
| 107 return _resolveEntry(libraryEntry, importUri); | 118 return _resolveEntry(libraryEntry, importUri); |
| 108 } | 119 } |
| 109 } | 120 } |
| 110 | 121 |
| 111 @override | 122 @override |
| 112 Uri restoreAbsolute(Source source) { | 123 Uri restoreAbsolute(Source source) { |
| 113 if (source is _SdkExtFileBasedSource) { | 124 String libraryName = _libraryName(source.uri); |
| 125 if (_registeredSdkExtension(libraryName)) { |
| 114 return source.uri; | 126 return source.uri; |
| 115 } | 127 } |
| 116 return null; | 128 return null; |
| 117 } | 129 } |
| 118 | 130 |
| 119 /// Given a package [name] and a list of folders ([libDirs]), | 131 /// Given a package [name] and a list of folders ([libDirs]), |
| 120 /// add any found sdk extensions. | 132 /// add any found sdk extensions. |
| 121 void _processPackage(String name, List<Folder> libDirs) { | 133 void _processPackage(String name, List<Folder> libDirs) { |
| 122 for (var libDir in libDirs) { | 134 for (var libDir in libDirs) { |
| 123 var sdkExt = _readDotSdkExt(libDir); | 135 var sdkExt = _readDotSdkExt(libDir); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 158 void _processSdkExtension(String name, String file, Folder libDir) { | 170 void _processSdkExtension(String name, String file, Folder libDir) { |
| 159 if (!name.startsWith(DART_COLON_PREFIX)) { | 171 if (!name.startsWith(DART_COLON_PREFIX)) { |
| 160 // SDK extensions must begin with 'dart:'. | 172 // SDK extensions must begin with 'dart:'. |
| 161 return; | 173 return; |
| 162 } | 174 } |
| 163 var key = name; | 175 var key = name; |
| 164 var value = libDir.canonicalizePath(file); | 176 var value = libDir.canonicalizePath(file); |
| 165 _urlMappings[key] = value; | 177 _urlMappings[key] = value; |
| 166 } | 178 } |
| 167 } | 179 } |
| OLD | NEW |