| 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 analyzer.source.embedder; | 5 library analyzer.source.embedder; |
| 6 | 6 |
| 7 import 'dart:collection' show HashMap; | 7 import 'dart:collection' show HashMap; |
| 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/source/package_map_provider.dart' | 11 import 'package:analyzer/source/package_map_provider.dart' |
| 12 show PackageMapProvider; | 12 show PackageMapProvider; |
| 13 import 'package:analyzer/src/context/context.dart'; | |
| 14 import 'package:analyzer/src/generated/engine.dart'; | |
| 15 import 'package:analyzer/src/generated/java_core.dart'; | 13 import 'package:analyzer/src/generated/java_core.dart'; |
| 16 import 'package:analyzer/src/generated/java_engine.dart'; | |
| 17 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | 14 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 18 import 'package:analyzer/src/generated/sdk.dart'; | 15 import 'package:analyzer/src/generated/sdk.dart'; |
| 16 import 'package:analyzer/src/generated/sdk_io.dart'; |
| 19 import 'package:analyzer/src/generated/source.dart'; | 17 import 'package:analyzer/src/generated/source.dart'; |
| 20 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; | 18 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
| 19 import 'package:analyzer/src/summary/idl.dart'; |
| 21 import 'package:yaml/yaml.dart'; | 20 import 'package:yaml/yaml.dart'; |
| 22 | 21 |
| 23 const String _DART_COLON_PREFIX = 'dart:'; | 22 const String _DART_COLON_PREFIX = 'dart:'; |
| 24 const String _EMBEDDED_LIB_MAP_KEY = 'embedded_libs'; | 23 const String _EMBEDDED_LIB_MAP_KEY = 'embedded_libs'; |
| 25 | 24 |
| 26 /// Check if this map defines embedded libraries. | 25 /// Check if this map defines embedded libraries. |
| 27 bool definesEmbeddedLibs(Map map) => map[_EMBEDDED_LIB_MAP_KEY] != null; | 26 bool definesEmbeddedLibs(Map map) => map[_EMBEDDED_LIB_MAP_KEY] != null; |
| 28 | 27 |
| 29 /// An SDK backed by URI mappings derived from an `_embedder.yaml` file. | 28 /// An SDK backed by URI mappings derived from an `_embedder.yaml` file. |
| 30 class EmbedderSdk implements DartSdk { | 29 class EmbedderSdk extends AbstractDartSdk { |
| 31 /// The resolver associated with this SDK. | |
| 32 EmbedderUriResolver _resolver; | |
| 33 | |
| 34 /// The [AnalysisContext] used for this SDK's sources. | |
| 35 InternalAnalysisContext _analysisContext; | |
| 36 | |
| 37 final LibraryMap _librariesMap = new LibraryMap(); | |
| 38 | |
| 39 final Map<String, String> _urlMappings = new HashMap<String, String>(); | 30 final Map<String, String> _urlMappings = new HashMap<String, String>(); |
| 40 | 31 |
| 41 /// Analysis options for this SDK. | |
| 42 AnalysisOptions analysisOptions; | |
| 43 | |
| 44 EmbedderSdk([Map<Folder, YamlMap> embedderYamls]) { | 32 EmbedderSdk([Map<Folder, YamlMap> embedderYamls]) { |
| 45 embedderYamls?.forEach(_processEmbedderYaml); | 33 embedderYamls?.forEach(_processEmbedderYaml); |
| 46 _resolver = new EmbedderUriResolver._forSdk(this); | |
| 47 } | 34 } |
| 48 | 35 |
| 49 @override | |
| 50 AnalysisContext get context { | |
| 51 if (_analysisContext == null) { | |
| 52 _analysisContext = new SdkAnalysisContext(analysisOptions); | |
| 53 SourceFactory factory = new SourceFactory([_resolver]); | |
| 54 _analysisContext.sourceFactory = factory; | |
| 55 | |
| 56 ChangeSet changeSet = new ChangeSet(); | |
| 57 for (String uri in uris) { | |
| 58 changeSet.addedSource(factory.forUri(uri)); | |
| 59 } | |
| 60 _analysisContext.applyChanges(changeSet); | |
| 61 } | |
| 62 return _analysisContext; | |
| 63 } | |
| 64 | |
| 65 @override | |
| 66 List<SdkLibrary> get sdkLibraries => _librariesMap.sdkLibraries; | |
| 67 | |
| 68 // TODO(danrubel) Determine SDK version | 36 // TODO(danrubel) Determine SDK version |
| 69 @override | 37 @override |
| 70 String get sdkVersion => '0'; | 38 String get sdkVersion => '0'; |
| 71 | 39 |
| 72 @override | |
| 73 List<String> get uris => _librariesMap.uris; | |
| 74 | |
| 75 /// The url mappings for this SDK. | 40 /// The url mappings for this SDK. |
| 76 Map<String, String> get urlMappings => _urlMappings; | 41 Map<String, String> get urlMappings => _urlMappings; |
| 77 | 42 |
| 78 @override | 43 @override |
| 79 Source fromFileUri(Uri uri) { | 44 String getRelativePathFromFile(JavaFile file) => file.getAbsolutePath(); |
| 80 JavaFile file = new JavaFile.fromUri(uri); | |
| 81 String filePath = file.getAbsolutePath(); | |
| 82 | |
| 83 String path; | |
| 84 for (SdkLibrary library in _librariesMap.sdkLibraries) { | |
| 85 String libraryPath = library.path.replaceAll('/', JavaFile.separator); | |
| 86 if (filePath == libraryPath) { | |
| 87 path = library.shortName; | |
| 88 break; | |
| 89 } | |
| 90 } | |
| 91 if (path == null) { | |
| 92 for (SdkLibrary library in _librariesMap.sdkLibraries) { | |
| 93 String libraryPath = library.path.replaceAll('/', JavaFile.separator); | |
| 94 int index = libraryPath.lastIndexOf(JavaFile.separator); | |
| 95 if (index == -1) { | |
| 96 continue; | |
| 97 } | |
| 98 String prefix = libraryPath.substring(0, index + 1); | |
| 99 if (!filePath.startsWith(prefix)) { | |
| 100 continue; | |
| 101 } | |
| 102 var relPath = filePath | |
| 103 .substring(prefix.length) | |
| 104 .replaceAll(JavaFile.separator, '/'); | |
| 105 path = '${library.shortName}/$relPath'; | |
| 106 break; | |
| 107 } | |
| 108 } | |
| 109 | |
| 110 if (path != null) { | |
| 111 try { | |
| 112 return new FileBasedSource(file, parseUriWithException(path)); | |
| 113 } on URISyntaxException catch (exception, stackTrace) { | |
| 114 AnalysisEngine.instance.logger.logInformation( | |
| 115 "Failed to create URI: $path", | |
| 116 new CaughtException(exception, stackTrace)); | |
| 117 return null; | |
| 118 } | |
| 119 } | |
| 120 return null; | |
| 121 } | |
| 122 | 45 |
| 123 @override | 46 @override |
| 124 SdkLibrary getSdkLibrary(String dartUri) => _librariesMap.getLibrary(dartUri); | 47 PackageBundle getSummarySdkBundle(bool strongMode) => null; |
| 125 | 48 |
| 126 @override | 49 @override |
| 127 Source mapDartUri(String dartUri) { | 50 FileBasedSource internalMapDartUri(String dartUri) { |
| 128 String libraryName; | 51 String libraryName; |
| 129 String relativePath; | 52 String relativePath; |
| 130 int index = dartUri.indexOf('/'); | 53 int index = dartUri.indexOf('/'); |
| 131 if (index >= 0) { | 54 if (index >= 0) { |
| 132 libraryName = dartUri.substring(0, index); | 55 libraryName = dartUri.substring(0, index); |
| 133 relativePath = dartUri.substring(index + 1); | 56 relativePath = dartUri.substring(index + 1); |
| 134 } else { | 57 } else { |
| 135 libraryName = dartUri; | 58 libraryName = dartUri; |
| 136 relativePath = ""; | 59 relativePath = ""; |
| 137 } | 60 } |
| (...skipping 28 matching lines...) Expand all Loading... |
| 166 /// Install the mapping from [name] to [libDir]/[file]. | 89 /// Install the mapping from [name] to [libDir]/[file]. |
| 167 void _processEmbeddedLibs(String name, String file, Folder libDir) { | 90 void _processEmbeddedLibs(String name, String file, Folder libDir) { |
| 168 if (!name.startsWith(_DART_COLON_PREFIX)) { | 91 if (!name.startsWith(_DART_COLON_PREFIX)) { |
| 169 // SDK libraries must begin with 'dart:'. | 92 // SDK libraries must begin with 'dart:'. |
| 170 return; | 93 return; |
| 171 } | 94 } |
| 172 String libPath = libDir.canonicalizePath(file); | 95 String libPath = libDir.canonicalizePath(file); |
| 173 _urlMappings[name] = libPath; | 96 _urlMappings[name] = libPath; |
| 174 SdkLibraryImpl library = new SdkLibraryImpl(name); | 97 SdkLibraryImpl library = new SdkLibraryImpl(name); |
| 175 library.path = libPath; | 98 library.path = libPath; |
| 176 _librariesMap.setLibrary(name, library); | 99 libraryMap.setLibrary(name, library); |
| 177 } | 100 } |
| 178 | 101 |
| 179 /// Given the 'embedderYamls' from [EmbedderYamlLocator] check each one for th
e | 102 /// Given the 'embedderYamls' from [EmbedderYamlLocator] check each one for th
e |
| 180 /// top level key 'embedded_libs'. Under the 'embedded_libs' key are key value | 103 /// top level key 'embedded_libs'. Under the 'embedded_libs' key are key value |
| 181 /// pairs. Each key is a 'dart:' library uri and each value is a path | 104 /// pairs. Each key is a 'dart:' library uri and each value is a path |
| 182 /// (relative to the directory containing `_embedder.yaml`) to a dart script | 105 /// (relative to the directory containing `_embedder.yaml`) to a dart script |
| 183 /// for the given library. For example: | 106 /// for the given library. For example: |
| 184 /// | 107 /// |
| 185 /// embedded_libs: | 108 /// embedded_libs: |
| 186 /// 'dart:io': '../../sdk/io/io.dart' | 109 /// 'dart:io': '../../sdk/io/io.dart' |
| (...skipping 17 matching lines...) Expand all Loading... |
| 204 /// 'dart:io': '../../sdk/io/io.dart' | 127 /// 'dart:io': '../../sdk/io/io.dart' |
| 205 /// | 128 /// |
| 206 /// If a key doesn't begin with `dart:` it is ignored. | 129 /// If a key doesn't begin with `dart:` it is ignored. |
| 207 /// | 130 /// |
| 208 class EmbedderUriResolver implements DartUriResolver { | 131 class EmbedderUriResolver implements DartUriResolver { |
| 209 EmbedderSdk _embedderSdk; | 132 EmbedderSdk _embedderSdk; |
| 210 DartUriResolver _dartUriResolver; | 133 DartUriResolver _dartUriResolver; |
| 211 | 134 |
| 212 /// Construct a [EmbedderUriResolver] from a package map | 135 /// Construct a [EmbedderUriResolver] from a package map |
| 213 /// (see [PackageMapProvider]). | 136 /// (see [PackageMapProvider]). |
| 214 EmbedderUriResolver(Map<Folder, YamlMap> embedderMap) : | 137 EmbedderUriResolver(Map<Folder, YamlMap> embedderMap) |
| 215 this._forSdk(new EmbedderSdk(embedderMap)); | 138 : this._forSdk(new EmbedderSdk(embedderMap)); |
| 216 | 139 |
| 217 /// (Provisional API.) | 140 /// (Provisional API.) |
| 218 EmbedderUriResolver._forSdk(this._embedderSdk) { | 141 EmbedderUriResolver._forSdk(this._embedderSdk) { |
| 219 _dartUriResolver = new DartUriResolver(_embedderSdk); | 142 _dartUriResolver = new DartUriResolver(_embedderSdk); |
| 220 } | 143 } |
| 221 | 144 |
| 222 @override | 145 @override |
| 223 DartSdk get dartSdk => _embedderSdk; | 146 DartSdk get dartSdk => _embedderSdk; |
| 224 | 147 |
| 225 /// Number of embedded libraries. | 148 /// Number of embedded libraries. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 String _readEmbedderYaml(Folder libDir) { | 223 String _readEmbedderYaml(Folder libDir) { |
| 301 File file = libDir.getChild(EMBEDDER_FILE_NAME); | 224 File file = libDir.getChild(EMBEDDER_FILE_NAME); |
| 302 try { | 225 try { |
| 303 return file.readAsStringSync(); | 226 return file.readAsStringSync(); |
| 304 } on FileSystemException { | 227 } on FileSystemException { |
| 305 // File can't be read. | 228 // File can't be read. |
| 306 return null; | 229 return null; |
| 307 } | 230 } |
| 308 } | 231 } |
| 309 } | 232 } |
| OLD | NEW |