| 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' show AssetId, Transformer, Transform; | 8 import 'package:barback/barback.dart' show AssetId, Transformer, Transform; |
| 9 | 9 |
| 10 import 'resolver.dart'; | 10 import 'resolver.dart'; |
| 11 import 'resolver_impl.dart'; | 11 import 'resolver_impl.dart'; |
| 12 | 12 |
| 13 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for | 13 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for |
| 14 /// the specified code entry points. | 14 /// the specified code entry points. |
| 15 /// | 15 /// |
| 16 /// This can used by transformers dependent on resolved ASTs to handle the | 16 /// This can used by transformers dependent on resolved ASTs to handle the |
| 17 /// resolution of the AST and cache the results between compilations. | 17 /// resolution of the AST and cache the results between compilations. |
| 18 /// | 18 /// |
| 19 /// If multiple transformers rely on a resolved AST they should (ideally) share | 19 /// If multiple transformers rely on a resolved AST they should (ideally) share |
| 20 /// the same Resolvers object to minimize re-parsing the AST. | 20 /// the same Resolvers object to minimize re-parsing the AST. |
| 21 class Resolvers { | 21 class Resolvers { |
| 22 final Map<AssetId, ResolverImpl> _resolvers = {}; | 22 final Map<AssetId, Resolver> _resolvers = {}; |
| 23 final String dartSdkDirectory; | 23 final String dartSdkDirectory; |
| 24 | 24 |
| 25 Resolvers(this.dartSdkDirectory); | 25 Resolvers(this.dartSdkDirectory); |
| 26 | 26 |
| 27 /// Get a resolver for the AST starting from [id]. | 27 /// Get a resolver for [transform]. If provided, this resolves the code |
| 28 /// starting from each of the assets in [entryPoints]. If not, this resolves |
| 29 /// the code starting from `transform.primaryInput.id` by default. |
| 28 /// | 30 /// |
| 29 /// [Resolver.release] must be called once it's done being used, or | 31 /// [Resolver.release] must be called once it's done being used, or |
| 30 /// [ResolverTransformer] should be used to automatically release the | 32 /// [ResolverTransformer] should be used to automatically release the |
| 31 /// resolver. | 33 /// resolver. |
| 32 Future<Resolver> get(Transform transform) { | 34 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) { |
| 33 var id = transform.primaryInput.id; | 35 var id = transform.primaryInput.id; |
| 34 var resolver = _resolvers.putIfAbsent(id, | 36 var resolver = _resolvers.putIfAbsent(id, |
| 35 () => new ResolverImpl(id, dartSdkDirectory)); | 37 () => new ResolverImpl(dartSdkDirectory)); |
| 36 return resolver.resolve(transform); | 38 return resolver.resolve(transform, entryPoints); |
| 37 } | 39 } |
| 38 } | 40 } |
| 39 | 41 |
| 40 /// Transformer mixin which automatically gets and releases resolvers. | 42 /// Transformer mixin which automatically gets and releases resolvers. |
| 41 /// | 43 /// |
| 42 /// To use mix this class in, set the resolvers field and override | 44 /// To use mix this class in, set the resolvers field and override |
| 43 /// [applyResolver]. | 45 /// [applyResolver]. |
| 44 abstract class ResolverTransformer implements Transformer { | 46 abstract class ResolverTransformer implements Transformer { |
| 45 /// The cache of resolvers- must be set from subclass. | 47 /// The cache of resolvers- must be set from subclass. |
| 46 Resolvers resolvers; | 48 Resolvers resolvers; |
| 47 | 49 |
| 48 Future apply(Transform transform) { | 50 /// This provides a default implementation of `Transformer.apply` that will |
| 49 return resolvers.get(transform).then((resolver) { | 51 /// get and release resolvers automatically. Internally this: |
| 52 /// * Gets a resolver associated with the transform primary input. |
| 53 /// * Does resolution to the code starting from that input. |
| 54 /// * Calls [applyResolver]. |
| 55 /// * Then releases the resolver. |
| 56 /// |
| 57 /// Use [applyToEntryPoints] instead if you need to override the entry points |
| 58 /// to run the resolver on. |
| 59 Future apply(Transform transform) => applyToEntryPoints(transform); |
| 60 |
| 61 /// Helper function to make it easy to write an `Transformer.apply` method |
| 62 /// that automatically gets and releases the resolver. This is typically used |
| 63 /// as follows: |
| 64 /// |
| 65 /// Future apply(Transform transform) { |
| 66 /// var entryPoints = ...; // compute entry points |
| 67 /// return applyToEntryPoints(transform, entryPoints); |
| 68 /// } |
| 69 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) { |
| 70 return resolvers.get(transform, entryPoints).then((resolver) { |
| 50 return new Future.value(applyResolver(transform, resolver)).then((_) { | 71 return new Future.value(applyResolver(transform, resolver)).then((_) { |
| 51 resolver.release(); | 72 resolver.release(); |
| 52 }); | 73 }); |
| 53 }); | 74 }); |
| 54 } | 75 } |
| 55 | 76 |
| 56 /// Invoked when the resolver is ready to be processed. | 77 /// Invoked when the resolver is ready to be processed. |
| 57 /// | 78 /// |
| 58 /// Return a Future to indicate when apply is completed. | 79 /// Return a Future to indicate when apply is completed. |
| 59 applyResolver(Transform transform, Resolver resolver); | 80 applyResolver(Transform transform, Resolver resolver); |
| 60 } | 81 } |
| OLD | NEW |