| 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'; |
| 9 | 9 |
| 10 import 'entry_point.dart'; |
| 10 import 'resolver.dart'; | 11 import 'resolver.dart'; |
| 11 import 'resolver_impl.dart'; | 12 import 'resolver_impl.dart'; |
| 12 | 13 |
| 13 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for | 14 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for |
| 14 /// the specified code entry points. | 15 /// the specified code entry points. |
| 15 /// | 16 /// |
| 16 /// This can used by transformers dependent on resolved ASTs to handle the | 17 /// This can used by transformers dependent on resolved ASTs to handle the |
| 17 /// resolution of the AST and cache the results between compilations. | 18 /// resolution of the AST and cache the results between compilations. |
| 18 /// | 19 /// |
| 19 /// If multiple transformers rely on a resolved AST they should (ideally) share | 20 /// If multiple transformers rely on a resolved AST they should (ideally) share |
| (...skipping 20 matching lines...) Expand all Loading... |
| 40 } | 41 } |
| 41 | 42 |
| 42 /// Transformer mixin which automatically gets and releases resolvers. | 43 /// Transformer mixin which automatically gets and releases resolvers. |
| 43 /// | 44 /// |
| 44 /// To use mix this class in, set the resolvers field and override | 45 /// To use mix this class in, set the resolvers field and override |
| 45 /// [applyResolver]. | 46 /// [applyResolver]. |
| 46 abstract class ResolverTransformer implements Transformer { | 47 abstract class ResolverTransformer implements Transformer { |
| 47 /// The cache of resolvers- must be set from subclass. | 48 /// The cache of resolvers- must be set from subclass. |
| 48 Resolvers resolvers; | 49 Resolvers resolvers; |
| 49 | 50 |
| 51 /// By default only process prossible entry point assets. |
| 52 /// |
| 53 /// This is only a preliminary check based on the asset ID. |
| 54 Future<bool> isPrimary(assetOrId) { |
| 55 // assetOrId is to handle the transition from Asset to AssetID between |
| 56 // pub 1.3 and 1.4. Once support for 1.3 is dropped this should only |
| 57 // support AssetId. |
| 58 var id = assetOrId is AssetId ? assetOrId : assetOrId.id; |
| 59 return new Future.value(isPossibleDartEntryId(id)); |
| 60 } |
| 61 |
| 62 /// Check to see if this should apply with the resolver on the provided asset. |
| 63 /// |
| 64 /// By default this will only apply on possible Dart entry points (see |
| 65 /// [isPossibleDartEntry]). |
| 66 Future<bool> shouldApplyResolver(Asset asset) => isPossibleDartEntry(asset); |
| 67 |
| 68 |
| 50 /// This provides a default implementation of `Transformer.apply` that will | 69 /// This provides a default implementation of `Transformer.apply` that will |
| 51 /// get and release resolvers automatically. Internally this: | 70 /// get and release resolvers automatically. Internally this: |
| 52 /// * Gets a resolver associated with the transform primary input. | 71 /// * Gets a resolver associated with the transform primary input. |
| 53 /// * Does resolution to the code starting from that input. | 72 /// * Does resolution to the code starting from that input. |
| 54 /// * Calls [applyResolver]. | 73 /// * Calls [applyResolver]. |
| 55 /// * Then releases the resolver. | 74 /// * Then releases the resolver. |
| 56 /// | 75 /// |
| 57 /// Use [applyToEntryPoints] instead if you need to override the entry points | 76 /// Use [applyToEntryPoints] instead if you need to override the entry points |
| 58 /// to run the resolver on. | 77 /// to run the resolver on. |
| 59 Future apply(Transform transform) => applyToEntryPoints(transform); | 78 Future apply(Transform transform) => |
| 79 shouldApplyResolver(transform.primaryInput).then((result) { |
| 80 if (result) return applyToEntryPoints(transform); |
| 81 }); |
| 82 |
| 60 | 83 |
| 61 /// Helper function to make it easy to write an `Transformer.apply` method | 84 /// Helper function to make it easy to write an `Transformer.apply` method |
| 62 /// that automatically gets and releases the resolver. This is typically used | 85 /// that automatically gets and releases the resolver. This is typically used |
| 63 /// as follows: | 86 /// as follows: |
| 64 /// | 87 /// |
| 65 /// Future apply(Transform transform) { | 88 /// Future apply(Transform transform) { |
| 66 /// var entryPoints = ...; // compute entry points | 89 /// var entryPoints = ...; // compute entry points |
| 67 /// return applyToEntryPoints(transform, entryPoints); | 90 /// return applyToEntryPoints(transform, entryPoints); |
| 68 /// } | 91 /// } |
| 69 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) { | 92 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) { |
| 70 return resolvers.get(transform, entryPoints).then((resolver) { | 93 return resolvers.get(transform, entryPoints).then((resolver) { |
| 71 return new Future(() => applyResolver(transform, resolver)) | 94 return new Future(() => applyResolver(transform, resolver)) |
| 72 .whenComplete(() { | 95 .whenComplete(() { |
| 73 resolver.release(); | 96 resolver.release(); |
| 74 }); | 97 }); |
| 75 }); | 98 }); |
| 76 } | 99 } |
| 77 | 100 |
| 78 /// Invoked when the resolver is ready to be processed. | 101 /// Invoked when the resolver is ready to be processed. |
| 79 /// | 102 /// |
| 80 /// Return a Future to indicate when apply is completed. | 103 /// Return a Future to indicate when apply is completed. |
| 81 applyResolver(Transform transform, Resolver resolver); | 104 applyResolver(Transform transform, Resolver resolver); |
| 82 } | 105 } |
| OLD | NEW |