OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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 code_transformers.src.dart_sdk; | 5 library code_transformers.src.dart_sdk; |
6 | 6 |
7 import 'dart:convert' as convert; | 7 import 'dart:convert' as convert; |
8 import 'dart:io' show File, Platform, Process; | 8 import 'dart:io' show File, Platform, Process; |
9 import 'package:path/path.dart' as path; | 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'; |
10 | 15 |
11 | 16 |
12 /// Attempts to provide the current Dart SDK directory. | 17 /// Attempts to provide the current Dart SDK directory. |
13 /// | 18 /// |
14 /// This will return null if the SDK cannot be found | 19 /// This will return null if the SDK cannot be found |
15 /// | 20 /// |
16 /// Note that this may not be correct when executing outside of `pub`. | 21 /// Note that this may not be correct when executing outside of `pub`. |
17 String get dartSdkDirectory { | 22 String get dartSdkDirectory { |
18 | 23 |
19 bool isSdkDir(String dirname) => | 24 bool isSdkDir(String dirname) => |
(...skipping 20 matching lines...) Expand all Loading... |
40 | 45 |
41 var parts = path.split(dartDir); | 46 var parts = path.split(dartDir); |
42 // If the dart executable is within the sdk dir then get the root. | 47 // If the dart executable is within the sdk dir then get the root. |
43 if (parts.contains('dart-sdk')) { | 48 if (parts.contains('dart-sdk')) { |
44 var dartSdkDir = path.joinAll(parts.take(parts.indexOf('dart-sdk') + 1)); | 49 var dartSdkDir = path.joinAll(parts.take(parts.indexOf('dart-sdk') + 1)); |
45 if (isSdkDir(dartSdkDir)) return dartSdkDir; | 50 if (isSdkDir(dartSdkDir)) return dartSdkDir; |
46 } | 51 } |
47 | 52 |
48 return null; | 53 return null; |
49 } | 54 } |
| 55 |
| 56 /// Sources that are annotated with a source uri, so it is easy to resolve how |
| 57 /// to support `Resolver.getImportUri`. |
| 58 abstract class UriAnnotatedSource extends Source { |
| 59 Uri get uri; |
| 60 } |
| 61 |
| 62 /// Dart SDK which wraps all Dart sources as [UriAnnotatedSource] to ensure they |
| 63 /// are tracked with Uris. |
| 64 class DirectoryBasedDartSdkProxy extends DirectoryBasedDartSdk { |
| 65 DirectoryBasedDartSdkProxy(String sdkDirectory) |
| 66 : super(new JavaFile(sdkDirectory)); |
| 67 |
| 68 Source mapDartUri(String dartUri) => |
| 69 DartSourceProxy.wrap(super.mapDartUri(dartUri), Uri.parse(dartUri)); |
| 70 } |
| 71 |
| 72 /// Dart SDK resolver which wraps all Dart sources to ensure they are tracked |
| 73 /// with URIs. |
| 74 class DartUriResolverProxy implements DartUriResolver { |
| 75 final DartUriResolver _proxy; |
| 76 DartUriResolverProxy(DartSdk sdk) : |
| 77 _proxy = new DartUriResolver(sdk); |
| 78 |
| 79 Source resolveAbsolute(Uri uri) => |
| 80 DartSourceProxy.wrap(_proxy.resolveAbsolute(uri), uri); |
| 81 |
| 82 DartSdk get dartSdk => _proxy.dartSdk; |
| 83 |
| 84 Source fromEncoding(UriKind kind, Uri uri) => |
| 85 throw new UnsupportedError('fromEncoding is not supported'); |
| 86 |
| 87 Uri restoreAbsolute(Source source) => |
| 88 throw new UnsupportedError('restoreAbsolute is not supported'); |
| 89 } |
| 90 |
| 91 /// Source file for dart: sources which track the sources with dart: URIs. |
| 92 /// |
| 93 /// This is primarily to support [Resolver.getImportUri] for Dart SDK (dart:) |
| 94 /// based libraries. |
| 95 class DartSourceProxy implements UriAnnotatedSource { |
| 96 |
| 97 /// Absolute URI which this source can be imported from |
| 98 final Uri uri; |
| 99 |
| 100 /// Underlying source object. |
| 101 final Source _proxy; |
| 102 |
| 103 DartSourceProxy(this._proxy, this.uri); |
| 104 |
| 105 /// Ensures that [source] is a DartSourceProxy. |
| 106 static DartSourceProxy wrap(Source source, Uri uri) { |
| 107 if (source == null || source is DartSourceProxy) return source; |
| 108 return new DartSourceProxy(source, uri); |
| 109 } |
| 110 |
| 111 Source resolveRelative(Uri relativeUri) { |
| 112 // Assume that the type can be accessed via this URI, since these |
| 113 // should only be parts for dart core files. |
| 114 return wrap(_proxy.resolveRelative(relativeUri), uri); |
| 115 } |
| 116 |
| 117 bool exists() => _proxy.exists(); |
| 118 |
| 119 bool operator ==(Object other) => |
| 120 (other is DartSourceProxy && _proxy == other._proxy); |
| 121 |
| 122 int get hashCode => _proxy.hashCode; |
| 123 |
| 124 TimestampedData<String> get contents => _proxy.contents; |
| 125 |
| 126 String get encoding => _proxy.encoding; |
| 127 |
| 128 String get fullName => _proxy.fullName; |
| 129 |
| 130 int get modificationStamp => _proxy.modificationStamp; |
| 131 |
| 132 String get shortName => _proxy.shortName; |
| 133 |
| 134 UriKind get uriKind => _proxy.uriKind; |
| 135 |
| 136 bool get isInSystemLibrary => _proxy.isInSystemLibrary; |
| 137 } |
| 138 |
| 139 |
| 140 /// Dart SDK which contains a mock implementation of the SDK libraries. May be |
| 141 /// used to speed up resultion when most of the core libraries is not needed. |
| 142 class MockDartSdk implements DartSdk { |
| 143 final Map<Uri, _MockSdkSource> _sources = {}; |
| 144 final bool reportMissing; |
| 145 final Map<String, SdkLibrary> _libs = {}; |
| 146 final String sdkVersion = '0'; |
| 147 List<String> get uris => _sources.keys.map((uri) => '$uri').toList(); |
| 148 final AnalysisContext context = new SdkAnalysisContext(); |
| 149 DartUriResolver _resolver; |
| 150 DartUriResolver get resolver => _resolver; |
| 151 |
| 152 MockDartSdk(Map<String, String> sources, {this.reportMissing}) { |
| 153 sources.forEach((uriString, contents) { |
| 154 var uri = Uri.parse(uriString); |
| 155 _sources[uri] = new _MockSdkSource(uri, contents); |
| 156 _libs[uriString] = new SdkLibraryImpl(uri.path) |
| 157 ..setDart2JsLibrary() |
| 158 ..setVmLibrary(); |
| 159 }); |
| 160 _resolver = new DartUriResolver(this); |
| 161 context.sourceFactory = new SourceFactory([_resolver]); |
| 162 } |
| 163 |
| 164 List<SdkLibrary> get sdkLibraries => _libs.values.toList(); |
| 165 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri]; |
| 166 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri)); |
| 167 |
| 168 Source fromEncoding(UriKind kind, Uri uri) { |
| 169 if (kind != UriKind.DART_URI) { |
| 170 throw new UnsupportedError('expected dart: uri kind, got $kind.'); |
| 171 } |
| 172 return _getSource(uri); |
| 173 } |
| 174 |
| 175 Source _getSource(Uri uri) { |
| 176 var src = _sources[uri]; |
| 177 if (src == null) { |
| 178 if (reportMissing) print('warning: missing mock for $uri.'); |
| 179 _sources[uri] = src = |
| 180 new _MockSdkSource(uri, 'library dart.${uri.path};'); |
| 181 } |
| 182 return src; |
| 183 } |
| 184 } |
| 185 |
| 186 class _MockSdkSource implements UriAnnotatedSource { |
| 187 /// Absolute URI which this source can be imported from. |
| 188 final Uri uri; |
| 189 final String _contents; |
| 190 |
| 191 _MockSdkSource(this.uri, this._contents); |
| 192 |
| 193 bool exists() => true; |
| 194 |
| 195 int get hashCode => uri.hashCode; |
| 196 |
| 197 final int modificationStamp = 1; |
| 198 |
| 199 TimestampedData<String> get contents => |
| 200 new TimestampedData(modificationStamp, _contents); |
| 201 |
| 202 String get encoding => "${uriKind.encoding}$uri"; |
| 203 |
| 204 String get fullName => shortName; |
| 205 |
| 206 String get shortName => uri.path; |
| 207 |
| 208 UriKind get uriKind => UriKind.DART_URI; |
| 209 |
| 210 bool get isInSystemLibrary => true; |
| 211 |
| 212 Source resolveRelative(Uri relativeUri) => |
| 213 throw new UnsupportedError('not expecting relative urls in dart: mocks'); |
| 214 } |
OLD | NEW |