OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library code_transformers.src.resolver_transformer; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'package:barback/barback.dart'; |
| 9 |
| 10 import 'resolver.dart'; |
| 11 import 'resolver_impl.dart'; |
| 12 |
| 13 /// Filter for whether the specified asset is an entry point to the Dart |
| 14 /// application. |
| 15 typedef EntryPointFilter(Asset input); |
| 16 |
| 17 /// Transformer which maintains up-to-date resolved ASTs for the specified |
| 18 /// code entry points. |
| 19 /// |
| 20 /// This can used by transformers dependent on resolved ASTs which can reference |
| 21 /// this transformer to get the resolver needed. |
| 22 /// |
| 23 /// This transformer must be in a phase before any dependent transformers. The |
| 24 /// resolve AST is automatically updated any time any dependent assets are |
| 25 /// changed. |
| 26 /// |
| 27 /// This will only resolve the AST for code beginning from assets which are |
| 28 /// accepted by [entryPointFilter]. |
| 29 /// |
| 30 /// If multiple transformers rely on a resolved AST they should (ideally) share |
| 31 /// the same ResolverTransformer to avoid re-parsing the AST. |
| 32 class ResolverTransformer extends Transformer { |
| 33 final Map<AssetId, Resolver> _resolvers = {}; |
| 34 final EntryPointFilter entryPointFilter; |
| 35 final String dartSdkDirectory; |
| 36 |
| 37 ResolverTransformer(this.dartSdkDirectory, this.entryPointFilter); |
| 38 |
| 39 Future<bool> isPrimary(Asset input) => |
| 40 new Future.value(entryPointFilter(input)); |
| 41 |
| 42 /// Updates the resolved AST for the primary input of the transform. |
| 43 Future apply(Transform transform) { |
| 44 var resolver = getResolver(transform.primaryInput.id); |
| 45 |
| 46 return resolver.updateSources(transform).then((_) { |
| 47 transform.addOutput(transform.primaryInput); |
| 48 return null; |
| 49 }); |
| 50 } |
| 51 |
| 52 /// Get a resolver for the AST starting from [id]. |
| 53 Resolver getResolver(AssetId id) => |
| 54 _resolvers.putIfAbsent(id, () => new ResolverImpl(id, dartSdkDirectory)); |
| 55 } |
OLD | NEW |