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.resolvers; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'package:barback/barback.dart'; |
| 9 |
| 10 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptions; |
| 11 import 'package:analyzer/src/generated/sdk.dart' show DartSdk; |
| 12 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; |
| 13 |
| 14 import 'entry_point.dart'; |
| 15 import 'resolver.dart'; |
| 16 import 'resolver_impl.dart'; |
| 17 import 'dart_sdk.dart' hide dartSdkDirectory; |
| 18 |
| 19 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for |
| 20 /// the specified code entry points. |
| 21 /// |
| 22 /// This can used by transformers dependent on resolved ASTs to handle the |
| 23 /// resolution of the AST and cache the results between compilations. |
| 24 /// |
| 25 /// If multiple transformers rely on a resolved AST they should (ideally) share |
| 26 /// the same Resolvers object to minimize re-parsing the AST. |
| 27 class Resolvers { |
| 28 final Map<AssetId, Resolver> _resolvers = {}; |
| 29 final DartSdk dartSdk; |
| 30 final DartUriResolver dartUriResolver; |
| 31 final AnalysisOptions options; |
| 32 |
| 33 Resolvers.fromSdk(this.dartSdk, this.dartUriResolver, {this.options}); |
| 34 |
| 35 factory Resolvers(dartSdkDirectory, {AnalysisOptions options}) { |
| 36 var sdk = new DirectoryBasedDartSdkProxy(dartSdkDirectory); |
| 37 var uriResolver = new DartUriResolverProxy(sdk); |
| 38 return new Resolvers.fromSdk(sdk, uriResolver, options: options); |
| 39 } |
| 40 |
| 41 factory Resolvers.fromMock(Map<String, String> sources, |
| 42 {bool reportMissing: false, AnalysisOptions options}) { |
| 43 var sdk = new MockDartSdk(sources, reportMissing: reportMissing); |
| 44 return new Resolvers.fromSdk(sdk, sdk.resolver, options: options); |
| 45 } |
| 46 |
| 47 /// Get a resolver for [transform]. If provided, this resolves the code |
| 48 /// starting from each of the assets in [entryPoints]. If not, this resolves |
| 49 /// the code starting from `transform.primaryInput.id` by default. |
| 50 /// |
| 51 /// [Resolver.release] must be called once it's done being used, or |
| 52 /// [ResolverTransformer] should be used to automatically release the |
| 53 /// resolver. |
| 54 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) { |
| 55 var id = transform.primaryInput.id; |
| 56 var resolver = _resolvers.putIfAbsent(id, |
| 57 () => new ResolverImpl(dartSdk, dartUriResolver, options: options)); |
| 58 return resolver.resolve(transform, entryPoints); |
| 59 } |
| 60 } |
| 61 |
| 62 /// Transformer mixin which automatically gets and releases resolvers. |
| 63 /// |
| 64 /// To use mix this class in, set the resolvers field and override |
| 65 /// [applyResolver]. |
| 66 abstract class ResolverTransformer implements Transformer { |
| 67 /// The cache of resolvers- must be set from subclass. |
| 68 Resolvers resolvers; |
| 69 |
| 70 /// By default only process prossible entry point assets. |
| 71 /// |
| 72 /// This is only a preliminary check based on the asset ID. |
| 73 Future<bool> isPrimary(assetOrId) { |
| 74 // assetOrId is to handle the transition from Asset to AssetID between |
| 75 // pub 1.3 and 1.4. Once support for 1.3 is dropped this should only |
| 76 // support AssetId. |
| 77 var id = assetOrId is AssetId ? assetOrId : assetOrId.id; |
| 78 return new Future.value(isPossibleDartEntryId(id)); |
| 79 } |
| 80 |
| 81 /// Check to see if this should apply with the resolver on the provided asset. |
| 82 /// |
| 83 /// By default this will only apply on possible Dart entry points (see |
| 84 /// [isPossibleDartEntry]). |
| 85 Future<bool> shouldApplyResolver(Asset asset) => isPossibleDartEntry(asset); |
| 86 |
| 87 |
| 88 /// This provides a default implementation of `Transformer.apply` that will |
| 89 /// get and release resolvers automatically. Internally this: |
| 90 /// * Gets a resolver associated with the transform primary input. |
| 91 /// * Does resolution to the code starting from that input. |
| 92 /// * Calls [applyResolver]. |
| 93 /// * Then releases the resolver. |
| 94 /// |
| 95 /// Use [applyToEntryPoints] instead if you need to override the entry points |
| 96 /// to run the resolver on. |
| 97 Future apply(Transform transform) => |
| 98 shouldApplyResolver(transform.primaryInput).then((result) { |
| 99 if (result) return applyToEntryPoints(transform); |
| 100 }); |
| 101 |
| 102 |
| 103 /// Helper function to make it easy to write an `Transformer.apply` method |
| 104 /// that automatically gets and releases the resolver. This is typically used |
| 105 /// as follows: |
| 106 /// |
| 107 /// Future apply(Transform transform) { |
| 108 /// var entryPoints = ...; // compute entry points |
| 109 /// return applyToEntryPoints(transform, entryPoints); |
| 110 /// } |
| 111 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) { |
| 112 return resolvers.get(transform, entryPoints).then((resolver) { |
| 113 return new Future(() => applyResolver(transform, resolver)) |
| 114 .whenComplete(() { |
| 115 resolver.release(); |
| 116 }); |
| 117 }); |
| 118 } |
| 119 |
| 120 /// Invoked when the resolver is ready to be processed. |
| 121 /// |
| 122 /// Return a Future to indicate when apply is completed. |
| 123 applyResolver(Transform transform, Resolver resolver); |
| 124 } |
OLD | NEW |