| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library analysis_server.src.sdk_ext; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:collection'; | |
| 9 import 'dart:convert'; | |
| 10 import 'dart:core' hide Resource; | |
| 11 | |
| 12 import 'package:analyzer/file_system/file_system.dart'; | |
| 13 import 'package:analyzer/source/package_map_resolver.dart'; | |
| 14 import 'package:analyzer/src/generated/source.dart'; | |
| 15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | |
| 16 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | |
| 17 import 'package:path/path.dart' as pathos; | |
| 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 | |
| 25 /// 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 | |
| 27 /// is a path (relative to the directory containing `.sdkext`) to a dart script | |
| 28 /// for the given library. For example: | |
| 29 /// { | |
| 30 /// "dart:sky": "../sdk_ext/dart_sky.dart" | |
| 31 /// } | |
| 32 /// | |
| 33 /// If a key doesn't begin with `dart:` it is ignored. | |
| 34 class SdkExtUriResolver extends UriResolver { | |
| 35 static const String DOT_SDK_EXT_NAME = '.sdkext'; | |
| 36 static const String DART_COLON_PREFIX = 'dart:'; | |
| 37 | |
| 38 final Map<String, String> _urlMappings = <String,String>{}; | |
| 39 | |
| 40 /// Construct a [SdkExtUriResolver] from a package map | |
| 41 /// (see [PackageMapProvider]). | |
| 42 SdkExtUriResolver(Map<String, List<Folder>> packageMap) { | |
| 43 if (packageMap == null) { | |
| 44 return; | |
| 45 } | |
| 46 packageMap.forEach(_processPackage); | |
| 47 } | |
| 48 | |
| 49 /// Programmatically add a new SDK extension given a JSON description | |
| 50 /// ([sdkExtJSON]) and a lib directory ([libDir]). | |
| 51 void addSdkExt(String sdkExtJSON, Folder libDir) { | |
| 52 _processSdkExt(sdkExtJSON, libDir); | |
| 53 } | |
| 54 | |
| 55 /// Return the path mapping for [libName] or null if there is none. | |
| 56 String operator[](String libName) => _urlMappings[libName]; | |
| 57 | |
| 58 /// Number of sdk extensions. | |
| 59 int get length => _urlMappings.length; | |
| 60 | |
| 61 /// Resolve a 'part' statement inside an sdk extension. | |
| 62 Source _resolvePart(Uri libraryEntry, String partPath, Uri importUri) { | |
| 63 // Library part. | |
| 64 var directory = pathos.dirname(libraryEntry.path); | |
| 65 var partUri = new Uri.file(pathos.join(directory, partPath)); | |
| 66 assert(partUri.isAbsolute); | |
| 67 JavaFile javaFile = new JavaFile.fromUri(partUri); | |
| 68 return new _SdkExtFileBasedSource(javaFile, importUri); | |
| 69 } | |
| 70 | |
| 71 /// Resolve an import of an sdk extension. | |
| 72 Source _resolveEntry(Uri libraryEntry, Uri importUri) { | |
| 73 // Library entry. | |
| 74 JavaFile javaFile = new JavaFile.fromUri(libraryEntry); | |
| 75 return new _SdkExtFileBasedSource(javaFile, importUri); | |
| 76 } | |
| 77 | |
| 78 @override | |
| 79 Source resolveAbsolute(Uri importUri) { | |
| 80 // Split import uri into library name and (optionally) a part path. | |
| 81 var uri = importUri.toString(); | |
| 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. | |
| 92 String mapping = _urlMappings[libraryName]; | |
| 93 if (mapping == null) { | |
| 94 // Not found. | |
| 95 return null; | |
| 96 } | |
| 97 // This mapping points to the main entry file of the sdk extension. | |
| 98 Uri libraryEntry = new Uri.file(mapping); | |
| 99 if (!libraryEntry.isAbsolute) { | |
| 100 // We expect an absolute path. | |
| 101 return null; | |
| 102 } | |
| 103 | |
| 104 if (index >= 0) { | |
| 105 return _resolvePart(libraryEntry, partPath, importUri); | |
| 106 } else { | |
| 107 return _resolveEntry(libraryEntry, importUri); | |
| 108 } | |
| 109 } | |
| 110 | |
| 111 @override | |
| 112 Uri restoreAbsolute(Source source) { | |
| 113 if (source is _SdkExtFileBasedSource) { | |
| 114 return source.uri; | |
| 115 } | |
| 116 return null; | |
| 117 } | |
| 118 | |
| 119 /// Given a package [name] and a list of folders ([libDirs]), | |
| 120 /// add any found sdk extensions. | |
| 121 void _processPackage(String name, List<Folder> libDirs) { | |
| 122 for (var libDir in libDirs) { | |
| 123 var sdkExt = _readDotSdkExt(libDir); | |
| 124 if (sdkExt != null) { | |
| 125 _processSdkExt(sdkExt, libDir); | |
| 126 } | |
| 127 } | |
| 128 } | |
| 129 | |
| 130 /// Read the contents of [libDir]/[DOT_SDK_EXT_NAME] as a string. | |
| 131 /// Returns null if the file doesn't exist. | |
| 132 String _readDotSdkExt(Folder libDir) { | |
| 133 var file = libDir.getChild(DOT_SDK_EXT_NAME); | |
| 134 try { | |
| 135 return file.readAsStringSync(); | |
| 136 } on FileSystemException catch (e) { | |
| 137 // File can't be read. | |
| 138 return null; | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 /// Given the JSON for an SDK extension ([sdkExtJSON]) and a folder | |
| 143 /// ([libDir]), setup the uri mapping. | |
| 144 void _processSdkExt(String sdkExtJSON, Folder libDir) { | |
| 145 var sdkExt; | |
| 146 try { | |
| 147 sdkExt = JSON.decode(sdkExtJSON); | |
| 148 } catch (e) { | |
| 149 return; | |
| 150 } | |
| 151 if ((sdkExt == null) || (sdkExt is! Map)) { | |
| 152 return; | |
| 153 } | |
| 154 sdkExt.forEach((k, v) => _processSdkExtension(k, v, libDir)); | |
| 155 } | |
| 156 | |
| 157 /// Install the mapping from [name] to [libDir]/[file]. | |
| 158 void _processSdkExtension(String name, String file, Folder libDir) { | |
| 159 if (!name.startsWith(DART_COLON_PREFIX)) { | |
| 160 // SDK extensions must begin with 'dart:'. | |
| 161 return; | |
| 162 } | |
| 163 var key = name; | |
| 164 var value = libDir.canonicalizePath(file); | |
| 165 _urlMappings[key] = value; | |
| 166 } | |
| 167 } | |
| OLD | NEW |