| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 code_transformers.src.dart_sdk; | |
| 6 | |
| 7 import 'dart:convert' as convert; | |
| 8 import 'dart:io' show File, Link, Platform, Process; | |
| 9 import 'package:path/path.dart' as path; | |
| 10 import 'package:analyzer/src/generated/engine.dart'; | |
| 11 import 'package:analyzer/src/generated/java_io.dart'; | |
| 12 import 'package:analyzer/src/generated/sdk.dart'; | |
| 13 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | |
| 14 import 'package:analyzer/src/generated/source.dart'; | |
| 15 | |
| 16 | |
| 17 /// Attempts to provide the current Dart SDK directory. | |
| 18 /// | |
| 19 /// This will return null if the SDK cannot be found | |
| 20 /// | |
| 21 /// Note that this may not be correct when executing outside of `pub`. | |
| 22 String get dartSdkDirectory { | |
| 23 | |
| 24 bool isSdkDir(String dirname) => | |
| 25 new File(path.join(dirname, 'lib', '_internal', 'libraries.dart')) | |
| 26 .existsSync(); | |
| 27 | |
| 28 String executable = Platform.executable; | |
| 29 if (path.split(executable).length == 1) { | |
| 30 // TODO(blois): make this cross-platform. | |
| 31 // HACK: A single part, hope it's on the path. | |
| 32 executable = Process.runSync('which', ['dart'], | |
| 33 stdoutEncoding: convert.UTF8).stdout.trim(); | |
| 34 // In case Dart is symlinked (e.g. homebrew on Mac) follow symbolic links. | |
| 35 var link = new Link(executable); | |
| 36 if (link.existsSync()) { | |
| 37 executable = link.resolveSymbolicLinksSync(); | |
| 38 } | |
| 39 var sdkDir = path.dirname(path.dirname(executable)); | |
| 40 if (isSdkDir(sdkDir)) return sdkDir; | |
| 41 } | |
| 42 | |
| 43 var dartDir = path.dirname(path.absolute(executable)); | |
| 44 // If there's a sub-dir named dart-sdk then we're most likely executing from | |
| 45 // a dart enlistment build directory. | |
| 46 if (isSdkDir(path.join(dartDir, 'dart-sdk'))) { | |
| 47 return path.join(dartDir, 'dart-sdk'); | |
| 48 } | |
| 49 // If we can find libraries.dart then it's the root of the SDK. | |
| 50 if (isSdkDir(dartDir)) return dartDir; | |
| 51 | |
| 52 var parts = path.split(dartDir); | |
| 53 // If the dart executable is within the sdk dir then get the root. | |
| 54 if (parts.contains('dart-sdk')) { | |
| 55 var dartSdkDir = path.joinAll(parts.take(parts.indexOf('dart-sdk') + 1)); | |
| 56 if (isSdkDir(dartSdkDir)) return dartSdkDir; | |
| 57 } | |
| 58 | |
| 59 return null; | |
| 60 } | |
| 61 | |
| 62 /// Sources that are annotated with a source uri, so it is easy to resolve how | |
| 63 /// to support `Resolver.getImportUri`. | |
| 64 abstract class UriAnnotatedSource extends Source { | |
| 65 Uri get uri; | |
| 66 } | |
| 67 | |
| 68 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they | |
| 69 /// are tracked with Uris. | |
| 70 class DirectoryBasedDartSdkProxy extends DirectoryBasedDartSdk { | |
| 71 DirectoryBasedDartSdkProxy(String sdkDirectory) | |
| 72 : super(new JavaFile(sdkDirectory)); | |
| 73 | |
| 74 Source mapDartUri(String dartUri) => | |
| 75 DartSourceProxy.wrap(super.mapDartUri(dartUri), Uri.parse(dartUri)); | |
| 76 } | |
| 77 | |
| 78 /// Dart SDK resolver which wraps all Dart sources to ensure they are tracked | |
| 79 /// with URIs. | |
| 80 class DartUriResolverProxy implements DartUriResolver { | |
| 81 final DartUriResolver _proxy; | |
| 82 DartUriResolverProxy(DartSdk sdk) : | |
| 83 _proxy = new DartUriResolver(sdk); | |
| 84 | |
| 85 Source resolveAbsolute(Uri uri) => | |
| 86 DartSourceProxy.wrap(_proxy.resolveAbsolute(uri), uri); | |
| 87 | |
| 88 DartSdk get dartSdk => _proxy.dartSdk; | |
| 89 | |
| 90 Source fromEncoding(UriKind kind, Uri uri) => | |
| 91 throw new UnsupportedError('fromEncoding is not supported'); | |
| 92 | |
| 93 Uri restoreAbsolute(Source source) => | |
| 94 throw new UnsupportedError('restoreAbsolute is not supported'); | |
| 95 } | |
| 96 | |
| 97 /// Source file for dart: sources which track the sources with dart: URIs. | |
| 98 /// | |
| 99 /// This is primarily to support [Resolver.getImportUri] for Dart SDK (dart:) | |
| 100 /// based libraries. | |
| 101 class DartSourceProxy implements UriAnnotatedSource { | |
| 102 | |
| 103 /// Absolute URI which this source can be imported from | |
| 104 final Uri uri; | |
| 105 | |
| 106 /// Underlying source object. | |
| 107 final Source _proxy; | |
| 108 | |
| 109 DartSourceProxy(this._proxy, this.uri); | |
| 110 | |
| 111 /// Ensures that [source] is a DartSourceProxy. | |
| 112 static DartSourceProxy wrap(Source source, Uri uri) { | |
| 113 if (source == null || source is DartSourceProxy) return source; | |
| 114 return new DartSourceProxy(source, uri); | |
| 115 } | |
| 116 | |
| 117 // Note: to support both analyzer versions <0.22.0 and analyzer >=0.22.0, we | |
| 118 // implement both `resolveRelative` and `resolveRelativeUri`. Only one of them | |
| 119 // is available at a time in the analyzer package, so we use the `as dynamic` | |
| 120 // in these methods to hide warnings for the code that is missing. These APIs | |
| 121 // are invoked from the analyzer itself, so we don't expect them to cause | |
| 122 // failures. | |
| 123 Source resolveRelative(Uri relativeUri) { | |
| 124 // Assume that the type can be accessed via this URI, since these | |
| 125 // should only be parts for dart core files. | |
| 126 return wrap((_proxy as dynamic).resolveRelative(relativeUri), uri); | |
| 127 } | |
| 128 | |
| 129 Uri resolveRelativeUri(Uri relativeUri) { | |
| 130 return (_proxy as dynamic).resolveRelativeUri(relativeUri); | |
| 131 } | |
| 132 | |
| 133 bool exists() => _proxy.exists(); | |
| 134 | |
| 135 bool operator ==(Object other) => | |
| 136 (other is DartSourceProxy && _proxy == other._proxy); | |
| 137 | |
| 138 int get hashCode => _proxy.hashCode; | |
| 139 | |
| 140 TimestampedData<String> get contents => _proxy.contents; | |
| 141 | |
| 142 String get encoding => _proxy.encoding; | |
| 143 | |
| 144 String get fullName => _proxy.fullName; | |
| 145 | |
| 146 int get modificationStamp => _proxy.modificationStamp; | |
| 147 | |
| 148 String get shortName => _proxy.shortName; | |
| 149 | |
| 150 UriKind get uriKind => _proxy.uriKind; | |
| 151 | |
| 152 bool get isInSystemLibrary => _proxy.isInSystemLibrary; | |
| 153 } | |
| 154 | |
| 155 | |
| 156 /// Dart SDK which contains a mock implementation of the SDK libraries. May be | |
| 157 /// used to speed up resultion when most of the core libraries is not needed. | |
| 158 class MockDartSdk implements DartSdk { | |
| 159 final Map<Uri, _MockSdkSource> _sources = {}; | |
| 160 final bool reportMissing; | |
| 161 final Map<String, SdkLibrary> _libs = {}; | |
| 162 final String sdkVersion = '0'; | |
| 163 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); | |
| 164 final AnalysisContext context = new SdkAnalysisContext(); | |
| 165 DartUriResolver _resolver; | |
| 166 DartUriResolver get resolver => _resolver; | |
| 167 | |
| 168 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { | |
| 169 sources.forEach((uriString, contents) { | |
| 170 var uri = Uri.parse(uriString); | |
| 171 _sources[uri] = new _MockSdkSource(uri, contents); | |
| 172 _libs[uriString] = new SdkLibraryImpl(uri.path) | |
| 173 ..setDart2JsLibrary() | |
| 174 ..setVmLibrary(); | |
| 175 }); | |
| 176 _resolver = new DartUriResolver(this); | |
| 177 context.sourceFactory = new SourceFactory([_resolver]); | |
| 178 } | |
| 179 | |
| 180 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); | |
| 181 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; | |
| 182 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); | |
| 183 | |
| 184 Source fromEncoding(UriKind kind, Uri uri) { | |
| 185 if (kind != UriKind.DART_URI) { | |
| 186 throw new UnsupportedError('expected dart: uri kind, got $kind.'); | |
| 187 } | |
| 188 return _getSource(uri); | |
| 189 } | |
| 190 | |
| 191 Source _getSource(Uri uri) { | |
| 192 var src = _sources[uri]; | |
| 193 if (src == null) { | |
| 194 if (reportMissing) print('warning: missing mock for $uri.'); | |
| 195 _sources[uri] = src = | |
| 196 new _MockSdkSource(uri, 'library dart.${uri.path};'); | |
| 197 } | |
| 198 return src; | |
| 199 } | |
| 200 | |
| 201 @override | |
| 202 Source fromFileUri(Uri uri) { | |
| 203 throw new UnsupportedError('MockDartSdk.fromFileUri'); | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 class _MockSdkSource implements UriAnnotatedSource { | |
| 208 /// Absolute URI which this source can be imported from. | |
| 209 final Uri uri; | |
| 210 final String _contents; | |
| 211 | |
| 212 _MockSdkSource(this.uri, this._contents); | |
| 213 | |
| 214 bool exists() => true; | |
| 215 | |
| 216 int get hashCode => uri.hashCode; | |
| 217 | |
| 218 final int modificationStamp = 1; | |
| 219 | |
| 220 TimestampedData<String> get contents => | |
| 221 new TimestampedData(modificationStamp, _contents); | |
| 222 | |
| 223 String get encoding => "${uriKind.encoding}$uri"; | |
| 224 | |
| 225 String get fullName => shortName; | |
| 226 | |
| 227 String get shortName => uri.path; | |
| 228 | |
| 229 UriKind get uriKind => UriKind.DART_URI; | |
| 230 | |
| 231 bool get isInSystemLibrary => true; | |
| 232 | |
| 233 Source resolveRelative(Uri relativeUri) => | |
| 234 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
| 235 | |
| 236 Uri resolveRelativeUri(Uri relativeUri) => | |
| 237 throw new UnsupportedError('not expecting relative urls in dart: mocks'); | |
| 238 } | |
| OLD | NEW |