| 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 @deprecated |
| 6 library analyzer.source.embedder; |
| 7 |
| 8 import 'dart:collection' show HashMap; |
| 9 import 'dart:core'; |
| 10 import 'dart:io' as io; |
| 11 |
| 12 import 'package:analyzer/file_system/file_system.dart'; |
| 13 import 'package:analyzer/source/package_map_provider.dart' |
| 14 show PackageMapProvider; |
| 15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; |
| 16 import 'package:analyzer/src/generated/sdk.dart'; |
| 17 import 'package:analyzer/src/generated/sdk_io.dart'; |
| 18 import 'package:analyzer/src/generated/source.dart'; |
| 19 import 'package:analyzer/src/generated/source_io.dart' show FileBasedSource; |
| 20 import 'package:analyzer/src/summary/idl.dart' show PackageBundle; |
| 21 import 'package:yaml/yaml.dart'; |
| 22 |
| 23 export 'package:analyzer/src/context/builder.dart' show EmbedderYamlLocator; |
| 24 |
| 25 const String _DART_COLON_PREFIX = 'dart:'; |
| 26 const String _EMBEDDED_LIB_MAP_KEY = 'embedded_libs'; |
| 27 |
| 28 /// Check if this map defines embedded libraries. |
| 29 @deprecated |
| 30 bool definesEmbeddedLibs(Map map) => map[_EMBEDDED_LIB_MAP_KEY] != null; |
| 31 |
| 32 /// An SDK backed by URI mappings derived from an `_embedder.yaml` file. |
| 33 @deprecated |
| 34 class EmbedderSdk extends AbstractDartSdk { |
| 35 final Map<String, String> _urlMappings = new HashMap<String, String>(); |
| 36 |
| 37 EmbedderSdk([Map<Folder, YamlMap> embedderYamls]) { |
| 38 embedderYamls?.forEach(_processEmbedderYaml); |
| 39 } |
| 40 |
| 41 // TODO(danrubel) Determine SDK version |
| 42 @override |
| 43 String get sdkVersion => '0'; |
| 44 |
| 45 /// The url mappings for this SDK. |
| 46 Map<String, String> get urlMappings => _urlMappings; |
| 47 |
| 48 @override |
| 49 PackageBundle getLinkedBundle() => null; |
| 50 |
| 51 @override |
| 52 String getRelativePathFromFile(JavaFile file) => file.getAbsolutePath(); |
| 53 |
| 54 @override |
| 55 PackageBundle getSummarySdkBundle(bool strongMode) => null; |
| 56 |
| 57 @override |
| 58 FileBasedSource internalMapDartUri(String dartUri) { |
| 59 String libraryName; |
| 60 String relativePath; |
| 61 int index = dartUri.indexOf('/'); |
| 62 if (index >= 0) { |
| 63 libraryName = dartUri.substring(0, index); |
| 64 relativePath = dartUri.substring(index + 1); |
| 65 } else { |
| 66 libraryName = dartUri; |
| 67 relativePath = ""; |
| 68 } |
| 69 SdkLibrary library = getSdkLibrary(libraryName); |
| 70 if (library == null) { |
| 71 return null; |
| 72 } |
| 73 String srcPath; |
| 74 if (relativePath.isEmpty) { |
| 75 srcPath = library.path; |
| 76 } else { |
| 77 String libraryPath = library.path; |
| 78 int index = libraryPath.lastIndexOf(io.Platform.pathSeparator); |
| 79 if (index == -1) { |
| 80 index = libraryPath.lastIndexOf('/'); |
| 81 if (index == -1) { |
| 82 return null; |
| 83 } |
| 84 } |
| 85 String prefix = libraryPath.substring(0, index + 1); |
| 86 srcPath = '$prefix$relativePath'; |
| 87 } |
| 88 String filePath = srcPath.replaceAll('/', io.Platform.pathSeparator); |
| 89 try { |
| 90 JavaFile file = new JavaFile(filePath); |
| 91 return new FileBasedSource(file, Uri.parse(dartUri)); |
| 92 } on FormatException { |
| 93 return null; |
| 94 } |
| 95 } |
| 96 |
| 97 /// Install the mapping from [name] to [libDir]/[file]. |
| 98 void _processEmbeddedLibs(String name, String file, Folder libDir) { |
| 99 if (!name.startsWith(_DART_COLON_PREFIX)) { |
| 100 // SDK libraries must begin with 'dart:'. |
| 101 return; |
| 102 } |
| 103 String libPath = libDir.canonicalizePath(file); |
| 104 _urlMappings[name] = libPath; |
| 105 SdkLibraryImpl library = new SdkLibraryImpl(name); |
| 106 library.path = libPath; |
| 107 libraryMap.setLibrary(name, library); |
| 108 } |
| 109 |
| 110 /// Given the 'embedderYamls' from [EmbedderYamlLocator] check each one for th
e |
| 111 /// top level key 'embedded_libs'. Under the 'embedded_libs' key are key value |
| 112 /// pairs. Each key is a 'dart:' library uri and each value is a path |
| 113 /// (relative to the directory containing `_embedder.yaml`) to a dart script |
| 114 /// for the given library. For example: |
| 115 /// |
| 116 /// embedded_libs: |
| 117 /// 'dart:io': '../../sdk/io/io.dart' |
| 118 /// |
| 119 /// If a key doesn't begin with `dart:` it is ignored. |
| 120 void _processEmbedderYaml(Folder libDir, YamlMap map) { |
| 121 YamlNode embedded_libs = map[_EMBEDDED_LIB_MAP_KEY]; |
| 122 if (embedded_libs is YamlMap) { |
| 123 embedded_libs.forEach((k, v) => _processEmbeddedLibs(k, v, libDir)); |
| 124 } |
| 125 } |
| 126 } |
| 127 |
| 128 /// Given the 'embedderYamls' from [EmbedderYamlLocator] check each one for the |
| 129 /// top level key 'embedded_libs'. Under the 'embedded_libs' key are key value |
| 130 /// pairs. Each key is a 'dart:' library uri and each value is a path |
| 131 /// (relative to the directory containing `_embedder.yaml`) to a dart script |
| 132 /// for the given library. For example: |
| 133 /// |
| 134 /// embedded_libs: |
| 135 /// 'dart:io': '../../sdk/io/io.dart' |
| 136 /// |
| 137 /// If a key doesn't begin with `dart:` it is ignored. |
| 138 /// |
| 139 /// This class is deprecated; use DartUriResolver directly. In particular, if |
| 140 /// there used to be an instance creation of the form: |
| 141 /// |
| 142 /// ``` |
| 143 /// new EmbedderUriResolver(embedderMap) |
| 144 /// ``` |
| 145 /// |
| 146 /// This should be replaced by |
| 147 /// |
| 148 /// ``` |
| 149 /// new DartUriResolver(new EmbedderSdk(embedderMap)) |
| 150 /// ``` |
| 151 @deprecated |
| 152 class EmbedderUriResolver implements DartUriResolver { |
| 153 EmbedderSdk _embedderSdk; |
| 154 DartUriResolver _dartUriResolver; |
| 155 |
| 156 /// Construct a [EmbedderUriResolver] from a package map |
| 157 /// (see [PackageMapProvider]). |
| 158 EmbedderUriResolver(Map<Folder, YamlMap> embedderMap) |
| 159 : this._forSdk(new EmbedderSdk(embedderMap)); |
| 160 |
| 161 /// (Provisional API.) |
| 162 EmbedderUriResolver._forSdk(this._embedderSdk) { |
| 163 _dartUriResolver = new DartUriResolver(_embedderSdk); |
| 164 } |
| 165 |
| 166 @override |
| 167 DartSdk get dartSdk => _embedderSdk; |
| 168 |
| 169 /// Number of embedded libraries. |
| 170 int get length => _embedderSdk?.urlMappings?.length ?? 0; |
| 171 |
| 172 @override |
| 173 Source resolveAbsolute(Uri uri, [Uri actualUri]) => |
| 174 _dartUriResolver.resolveAbsolute(uri, actualUri); |
| 175 |
| 176 @override |
| 177 Uri restoreAbsolute(Source source) { |
| 178 String path = source.fullName; |
| 179 if (path.length > 3 && path[1] == ':' && path[2] == '\\') { |
| 180 path = '/${path[0]}:${path.substring(2).replaceAll('\\', '/')}'; |
| 181 } |
| 182 Source sdkSource = dartSdk.fromFileUri(Uri.parse('file://$path')); |
| 183 return sdkSource?.uri; |
| 184 } |
| 185 } |
| OLD | NEW |