Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: pkg/code_transformers/lib/src/dart_sdk.dart

Issue 315813003: Support using a mock SDK with the resolver in code-transformers, and switch (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/code_transformers/lib/src/resolver_impl.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
150 MockDartSdk(Map<String, String> sources, {this.reportMissing}) {
151 sources.forEach((uriString, contents) {
152 var uri = Uri.parse(uriString);
153 _sources[uri] = new _MockSdkSource(uri, contents);
154 _libs[uriString] = new SdkLibraryImpl(uri.path)
155 ..setDart2JsLibrary()
156 ..setVmLibrary();
157 });
158 }
159
160 List<SdkLibrary> get sdkLibraries => _libs.values.toList();
161 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri];
162 Source mapDartUri(String dartUri) => _getSource(Uri.parse(dartUri));
163
164 Source fromEncoding(UriKind kind, Uri uri) {
165 if (kind != UriKind.DART_URI) {
166 throw new UnsupportedError('expected dart: uri kind, got $kind.');
167 }
168 return _getSource(uri);
169 }
170
171 Source _getSource(Uri uri) {
172 var src = _sources[uri];
173 if (src == null) {
174 if (reportMissing) print('warning: missing mock for $uri.');
175 _sources[uri] = src =
176 new _MockSdkSource(uri, 'library dart.${uri.path};');
177 }
178 return src;
179 }
180 }
181
182 class _MockSdkSource implements UriAnnotatedSource {
183 /// Absolute URI which this source can be imported from.
184 final Uri uri;
185 final String _contents;
186
187 _MockSdkSource(this.uri, this._contents);
188
189 bool exists() => true;
190
191 int get hashCode => uri.hashCode;
192
193 final int modificationStamp = 1;
194
195 TimestampedData<String> get contents =>
196 new TimestampedData(modificationStamp, _contents);
197
198 String get encoding => "${uriKind.encoding}$uri";
199
200 String get fullName => shortName;
201
202 String get shortName => uri.path;
203
204 UriKind get uriKind => UriKind.DART_URI;
205
206 bool get isInSystemLibrary => true;
207
208 Source resolveRelative(Uri relativeUri) =>
209 throw new UnsupportedError('not expecting relative urls in dart: mocks');
210 }
211
212 // TODO(sigmund): delete once we bump the dependency on analyzer. We copied
213 // this code from SdkAnalysisContext in 'analyzer/src/generated/engine.dart' to
214 // prevent bumping the dependency on analyzer. We should remove this as soon as
215 // we start depending on analyzer >= 0.15.5.
216 class _SdkAnalysisContext extends AnalysisContextImpl {
217 @override
218 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) {
219 if (factory == null) return super.createCacheFromSourceFactory(factory);
220 var sdk = factory.dartSdk;
221 if (sdk == null) throw "Missing a DartUriResolver in source factory";
222 return new AnalysisCache(
223 [AnalysisEngine.instance.partitionManager.forSdk(sdk)]);
224 }
225 }
OLDNEW
« no previous file with comments | « no previous file | pkg/code_transformers/lib/src/resolver_impl.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698