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

Side by Side Diff: pkg/code_transformers/lib/src/resolvers.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.resolvers; 5 library code_transformers.src.resolvers;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'package:barback/barback.dart'; 8 import 'package:barback/barback.dart';
9 9
10 import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
11 import 'package:analyzer/src/generated/source.dart' show DartUriResolver;
12
10 import 'entry_point.dart'; 13 import 'entry_point.dart';
11 import 'resolver.dart'; 14 import 'resolver.dart';
12 import 'resolver_impl.dart'; 15 import 'resolver_impl.dart';
16 import 'dart_sdk.dart' hide dartSdkDirectory;
13 17
14 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for 18 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for
15 /// the specified code entry points. 19 /// the specified code entry points.
16 /// 20 ///
17 /// This can used by transformers dependent on resolved ASTs to handle the 21 /// This can used by transformers dependent on resolved ASTs to handle the
18 /// resolution of the AST and cache the results between compilations. 22 /// resolution of the AST and cache the results between compilations.
19 /// 23 ///
20 /// If multiple transformers rely on a resolved AST they should (ideally) share 24 /// If multiple transformers rely on a resolved AST they should (ideally) share
21 /// the same Resolvers object to minimize re-parsing the AST. 25 /// the same Resolvers object to minimize re-parsing the AST.
22 class Resolvers { 26 class Resolvers {
23 final Map<AssetId, Resolver> _resolvers = {}; 27 final Map<AssetId, Resolver> _resolvers = {};
24 final String dartSdkDirectory; 28 final DartSdk dartSdk;
29 final DartUriResolver dartUriResolver;
25 30
26 Resolvers(this.dartSdkDirectory); 31 Resolvers(this.dartSdk, this.dartUriResolver);
32
33 factory Resolvers.fromSdkDirectory(dartSdkDirectory) {
blois 2014/06/04 16:31:22 Can these two constructors be swapped to avoid a b
Siggi Cherem (dart-lang) 2014/06/04 21:25:19 Good idea, done.
34 var sdk = new DirectoryBasedDartSdkProxy(dartSdkDirectory);
35 var uriResolver = new DartUriResolverProxy(sdk);
36 return new Resolvers(sdk, uriResolver);
37 }
38
39 factory Resolvers.fromMock(Map<String, String> sources) {
40 var sdk = new MockDartSdk(sources);
41 return new Resolvers(sdk, new DartUriResolver(sdk));
42 }
27 43
28 /// Get a resolver for [transform]. If provided, this resolves the code 44 /// Get a resolver for [transform]. If provided, this resolves the code
29 /// starting from each of the assets in [entryPoints]. If not, this resolves 45 /// starting from each of the assets in [entryPoints]. If not, this resolves
30 /// the code starting from `transform.primaryInput.id` by default. 46 /// the code starting from `transform.primaryInput.id` by default.
31 /// 47 ///
32 /// [Resolver.release] must be called once it's done being used, or 48 /// [Resolver.release] must be called once it's done being used, or
33 /// [ResolverTransformer] should be used to automatically release the 49 /// [ResolverTransformer] should be used to automatically release the
34 /// resolver. 50 /// resolver.
35 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) { 51 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) {
36 var id = transform.primaryInput.id; 52 var id = transform.primaryInput.id;
37 var resolver = _resolvers.putIfAbsent(id, 53 var resolver = _resolvers.putIfAbsent(id,
38 () => new ResolverImpl(dartSdkDirectory)); 54 () => new ResolverImpl(dartSdk, dartUriResolver));
39 return resolver.resolve(transform, entryPoints); 55 return resolver.resolve(transform, entryPoints);
40 } 56 }
41 } 57 }
42 58
43 /// Transformer mixin which automatically gets and releases resolvers. 59 /// Transformer mixin which automatically gets and releases resolvers.
44 /// 60 ///
45 /// To use mix this class in, set the resolvers field and override 61 /// To use mix this class in, set the resolvers field and override
46 /// [applyResolver]. 62 /// [applyResolver].
47 abstract class ResolverTransformer implements Transformer { 63 abstract class ResolverTransformer implements Transformer {
48 /// The cache of resolvers- must be set from subclass. 64 /// The cache of resolvers- must be set from subclass.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
96 resolver.release(); 112 resolver.release();
97 }); 113 });
98 }); 114 });
99 } 115 }
100 116
101 /// Invoked when the resolver is ready to be processed. 117 /// Invoked when the resolver is ready to be processed.
102 /// 118 ///
103 /// Return a Future to indicate when apply is completed. 119 /// Return a Future to indicate when apply is completed.
104 applyResolver(Transform transform, Resolver resolver); 120 applyResolver(Transform transform, Resolver resolver);
105 } 121 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698