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

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
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 {
Siggi Cherem (dart-lang) 2014/06/04 02:26:58 this interface is new (common to DartSourceProxy a
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 {
Siggi Cherem (dart-lang) 2014/06/04 02:26:58 this is the same code as before, but I moved it he
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 {
Siggi Cherem (dart-lang) 2014/06/04 02:26:59 here is where the new stuff begins
143 final Map<Uri, _MockSdkSource> _sources = {};
144 final Map<String, SdkLibrary> _libs = {};
145 final String sdkVersion = '0';
146 List<String> get uris => _sources.keys.map((uri) => '$uri').toList();
147 final AnalysisContext context = new _SdkAnalysisContext();
148
149 MockDartSdk(Map<String, String> sources) {
150 sources.forEach((uriString, contents) {
151 var uri = Uri.parse(uriString);
152 _sources[uri] = new _MockSdkSource(uri, contents);
153 _libs[uriString] = new SdkLibraryImpl(uri.path)
154 ..setDart2JsLibrary()
155 ..setVmLibrary();
156 });
157 }
158
159 List<SdkLibrary> get sdkLibraries => _libs.values.toList();
160 SdkLibrary getSdkLibrary(String dartUri) => _libs[dartUri];
161 Source mapDartUri(String dartUri) => _sources[Uri.parse(dartUri)];
162
163 Source fromEncoding(UriKind kind, Uri uri) {
164 if (kind != UriKind.DART_URI) {
165 throw new UnsupportedError('expected dart: uri kind, got $kind.');
166 }
167 var src = _sources[uri];
168 if (src == null) throw new StateError('missing mock for $uri');
169 return src;
170 }
171 }
172
173 class _MockSdkSource implements UriAnnotatedSource {
174 /// Absolute URI which this source can be imported from.
175 final Uri uri;
176 final String _contents;
177
178 _MockSdkSource(this.uri, this._contents);
179
180 bool exists() => true;
181
182 int get hashCode => uri.hashCode;
183
184 final int modificationStamp = new DateTime.now().millisecondsSinceEpoch;
blois 2014/06/04 16:31:22 Can just do '1' or something as well.
Siggi Cherem (dart-lang) 2014/06/04 21:25:19 Done.
185
186 TimestampedData<String> get contents =>
187 new TimestampedData(modificationStamp, _contents);
188
189 String get encoding => "${uriKind.encoding}$uri";
190
191 String get fullName => shortName;
192
193 String get shortName => uri.path;
194
195 UriKind get uriKind => UriKind.DART_URI;
196
197 bool get isInSystemLibrary => true;
198
199 Source resolveRelative(Uri relativeUri) =>
200 throw new UnsupportedError('not expecting relative urls in dart: mocks');
201 }
202
203 // TODO(sigmund): delete once we bump the dependency on analyzer. We copied
204 // this code from SdkAnalysisContext in 'analyzer/src/generated/engine.dart' to
205 // prevent bumping the dependency on analyzer. We should remove this as soon as
206 // we start depending on analyzer >= 0.15.5.
207 class _SdkAnalysisContext extends AnalysisContextImpl {
208 @override
209 AnalysisCache createCacheFromSourceFactory(SourceFactory factory) {
210 if (factory == null) return super.createCacheFromSourceFactory(factory);
211 var sdk = factory.dartSdk;
212 if (sdk == null) throw "Missing a DartUriResolver in source factory";
213 return new AnalysisCache(
214 [AnalysisEngine.instance.partitionManager.forSdk(sdk)]);
215 }
216 }
OLDNEW
« no previous file with comments | « no previous file | pkg/code_transformers/lib/src/resolver_impl.dart » ('j') | pkg/code_transformers/lib/src/resolver_impl.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698