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.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.fromSdk(this.dartSdk, this.dartUriResolver); |
| 32 |
| 33 factory Resolvers(dartSdkDirectory) { |
| 34 var sdk = new DirectoryBasedDartSdkProxy(dartSdkDirectory); |
| 35 var uriResolver = new DartUriResolverProxy(sdk); |
| 36 return new Resolvers.fromSdk(sdk, uriResolver); |
| 37 } |
| 38 |
| 39 factory Resolvers.fromMock(Map<String, String> sources, |
| 40 {bool reportMissing: false}) { |
| 41 var sdk = new MockDartSdk(sources, reportMissing: reportMissing); |
| 42 return new Resolvers.fromSdk(sdk, new DartUriResolver(sdk)); |
| 43 } |
27 | 44 |
28 /// Get a resolver for [transform]. If provided, this resolves the code | 45 /// Get a resolver for [transform]. If provided, this resolves the code |
29 /// starting from each of the assets in [entryPoints]. If not, this resolves | 46 /// starting from each of the assets in [entryPoints]. If not, this resolves |
30 /// the code starting from `transform.primaryInput.id` by default. | 47 /// the code starting from `transform.primaryInput.id` by default. |
31 /// | 48 /// |
32 /// [Resolver.release] must be called once it's done being used, or | 49 /// [Resolver.release] must be called once it's done being used, or |
33 /// [ResolverTransformer] should be used to automatically release the | 50 /// [ResolverTransformer] should be used to automatically release the |
34 /// resolver. | 51 /// resolver. |
35 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) { | 52 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) { |
36 var id = transform.primaryInput.id; | 53 var id = transform.primaryInput.id; |
37 var resolver = _resolvers.putIfAbsent(id, | 54 var resolver = _resolvers.putIfAbsent(id, |
38 () => new ResolverImpl(dartSdkDirectory)); | 55 () => new ResolverImpl(dartSdk, dartUriResolver)); |
39 return resolver.resolve(transform, entryPoints); | 56 return resolver.resolve(transform, entryPoints); |
40 } | 57 } |
41 } | 58 } |
42 | 59 |
43 /// Transformer mixin which automatically gets and releases resolvers. | 60 /// Transformer mixin which automatically gets and releases resolvers. |
44 /// | 61 /// |
45 /// To use mix this class in, set the resolvers field and override | 62 /// To use mix this class in, set the resolvers field and override |
46 /// [applyResolver]. | 63 /// [applyResolver]. |
47 abstract class ResolverTransformer implements Transformer { | 64 abstract class ResolverTransformer implements Transformer { |
48 /// The cache of resolvers- must be set from subclass. | 65 /// The cache of resolvers- must be set from subclass. |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
96 resolver.release(); | 113 resolver.release(); |
97 }); | 114 }); |
98 }); | 115 }); |
99 } | 116 } |
100 | 117 |
101 /// Invoked when the resolver is ready to be processed. | 118 /// Invoked when the resolver is ready to be processed. |
102 /// | 119 /// |
103 /// Return a Future to indicate when apply is completed. | 120 /// Return a Future to indicate when apply is completed. |
104 applyResolver(Transform transform, Resolver resolver); | 121 applyResolver(Transform transform, Resolver resolver); |
105 } | 122 } |
OLD | NEW |