Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Side by Side Diff: packages/code_transformers/lib/src/resolvers.dart

Issue 2989763002: Update charted to 0.4.8 and roll (Closed)
Patch Set: Removed Cutch from list of reviewers Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/file_system/physical_file_system.dart';
10 import 'package:analyzer/src/generated/engine.dart' show AnalysisOptions; 11 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/sdk.dart' show DartSdk;
13 import 'package:analyzer/src/generated/engine.dart';
12 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; 14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver;
13 15
14 import 'entry_point.dart'; 16 import 'entry_point.dart';
15 import 'resolver.dart'; 17 import 'resolver.dart';
16 import 'resolver_impl.dart'; 18 import 'resolver_impl.dart';
17 import 'dart_sdk.dart' hide dartSdkDirectory; 19 import 'dart_sdk.dart' hide dartSdkDirectory;
18 20
19 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for 21 /// Barback-based code resolvers which maintains up-to-date resolved ASTs for
20 /// the specified code entry points. 22 /// the specified code entry points.
21 /// 23 ///
22 /// This can used by transformers dependent on resolved ASTs to handle the 24 /// This can used by transformers dependent on resolved ASTs to handle the
23 /// resolution of the AST and cache the results between compilations. 25 /// resolution of the AST and cache the results between compilations.
24 /// 26 ///
25 /// If multiple transformers rely on a resolved AST they should (ideally) share 27 /// If multiple transformers rely on a resolved AST they should (ideally) share
26 /// the same Resolvers object to minimize re-parsing the AST. 28 /// the same Resolvers object to minimize re-parsing the AST.
27 class Resolvers { 29 class Resolvers {
28 final Map<AssetId, Resolver> _resolvers = {}; 30 final Resolver _resolver;
29 final DartSdk dartSdk;
30 final DartUriResolver dartUriResolver;
31 final AnalysisOptions options;
32 31
33 Resolvers.fromSdk(this.dartSdk, this.dartUriResolver, {this.options}); 32 Resolvers.fromSdk(DartSdk dartSdk, DartUriResolver dartUriResolver,
33 {AnalysisOptions options})
34 : _resolver =
35 new ResolverImpl(dartSdk, dartUriResolver, options: options);
34 36
35 factory Resolvers(dartSdkDirectory, {AnalysisOptions options}) { 37 factory Resolvers(String dartSdkDirectory, {AnalysisOptions options}) {
36 var sdk = new DirectoryBasedDartSdkProxy(dartSdkDirectory); 38 _initAnalysisEngine();
39 var sdk = new FolderBasedDartSdkProxy(
40 PhysicalResourceProvider.INSTANCE, dartSdkDirectory);
37 var uriResolver = new DartUriResolverProxy(sdk); 41 var uriResolver = new DartUriResolverProxy(sdk);
38 return new Resolvers.fromSdk(sdk, uriResolver, options: options); 42 return new Resolvers.fromSdk(sdk, uriResolver, options: options);
39 } 43 }
40 44
41 factory Resolvers.fromMock(Map<String, String> sources, 45 factory Resolvers.fromMock(Map<String, String> sources,
42 {bool reportMissing: false, AnalysisOptions options}) { 46 {bool reportMissing: false, AnalysisOptions options}) {
43 var sdk = new MockDartSdk(sources, reportMissing: reportMissing); 47 _initAnalysisEngine();
48 var sdk = new MockDartSdk(sources, options, reportMissing: reportMissing);
44 return new Resolvers.fromSdk(sdk, sdk.resolver, options: options); 49 return new Resolvers.fromSdk(sdk, sdk.resolver, options: options);
45 } 50 }
46 51
47 /// Get a resolver for [transform]. If provided, this resolves the code 52 /// Get a resolver for [transform]. If provided, this resolves the code
48 /// starting from each of the assets in [entryPoints]. If not, this resolves 53 /// starting from each of the assets in [entryPoints]. If not, this resolves
49 /// the code starting from `transform.primaryInput.id` by default. 54 /// the code starting from `transform.primaryInput.id` by default.
50 /// 55 ///
51 /// [Resolver.release] must be called once it's done being used, or 56 /// [Resolver.release] must be called once it's done being used, or
52 /// [ResolverTransformer] should be used to automatically release the 57 /// [ResolverTransformer] should be used to automatically release the
53 /// resolver. 58 /// resolver.
54 Future<Resolver> get(Transform transform, [List<AssetId> entryPoints]) { 59 ///
55 var id = transform.primaryInput.id; 60 /// See [Resolver#resolve] for more info on the `resolveAllLibraries` option.
56 var resolver = _resolvers.putIfAbsent( 61 Future<Resolver> get(Transform transform,
57 id, () => new ResolverImpl(dartSdk, dartUriResolver, options: options)); 62 [List<AssetId> entryPoints, bool resolveAllLibraries]) =>
58 return resolver.resolve(transform, entryPoints); 63 _resolver.resolve(transform, entryPoints, resolveAllLibraries);
59 }
60 } 64 }
61 65
62 /// Transformer mixin which automatically gets and releases resolvers. 66 /// Transformer mixin which automatically gets and releases resolvers.
63 /// 67 ///
64 /// To use mix this class in, set the resolvers field and override 68 /// To use mix this class in, set the resolvers field and override
65 /// [applyResolver]. 69 /// [applyResolver].
66 abstract class ResolverTransformer implements Transformer { 70 abstract class ResolverTransformer implements Transformer {
67 /// The cache of resolvers- must be set from subclass. 71 /// The cache of resolvers- must be set from subclass.
68 Resolvers resolvers; 72 Resolvers resolvers;
69 73
70 /// By default only process prossible entry point assets. 74 /// See [Resolver#resolve] for more info - can be overridden by a subclass.
75 bool get resolveAllLibraries => true;
76
77 /// By default only process possible entry point assets.
71 /// 78 ///
72 /// This is only a preliminary check based on the asset ID. 79 /// This is only a preliminary check based on the asset ID.
73 Future<bool> isPrimary(assetOrId) { 80 Future<bool> isPrimary(assetOrId) {
74 // assetOrId is to handle the transition from Asset to AssetID between 81 // 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 82 // pub 1.3 and 1.4. Once support for 1.3 is dropped this should only
76 // support AssetId. 83 // support AssetId.
77 var id = assetOrId is AssetId ? assetOrId : assetOrId.id; 84 var id = assetOrId is AssetId ? assetOrId : (assetOrId as Asset).id;
78 return new Future.value(isPossibleDartEntryId(id)); 85 return new Future.value(isPossibleDartEntryId(id));
79 } 86 }
80 87
81 /// Check to see if this should apply with the resolver on the provided asset. 88 /// Check to see if this should apply with the resolver on the provided asset.
82 /// 89 ///
83 /// By default this will only apply on possible Dart entry points (see 90 /// By default this will only apply on possible Dart entry points (see
84 /// [isPossibleDartEntry]). 91 /// [isPossibleDartEntry]).
85 Future<bool> shouldApplyResolver(Asset asset) => isPossibleDartEntry(asset); 92 Future<bool> shouldApplyResolver(Asset asset) => isPossibleDartEntry(asset);
86 93
87 /// This provides a default implementation of `Transformer.apply` that will 94 /// This provides a default implementation of `Transformer.apply` that will
88 /// get and release resolvers automatically. Internally this: 95 /// get and release resolvers automatically. Internally this:
89 /// * Gets a resolver associated with the transform primary input. 96 /// * Gets a resolver associated with the transform primary input.
90 /// * Does resolution to the code starting from that input. 97 /// * Does resolution to the code starting from that input.
91 /// * Calls [applyResolver]. 98 /// * Calls [applyResolver].
92 /// * Then releases the resolver. 99 /// * Then releases the resolver.
93 /// 100 ///
94 /// Use [applyToEntryPoints] instead if you need to override the entry points 101 /// Use [applyToEntryPoints] instead if you need to override the entry points
95 /// to run the resolver on. 102 /// to run the resolver on.
96 Future apply(Transform transform) => 103 Future apply(Transform transform) =>
97 shouldApplyResolver(transform.primaryInput).then((result) { 104 shouldApplyResolver(transform.primaryInput).then((result) {
98 if (result) return applyToEntryPoints(transform); 105 if (result) return applyToEntryPoints(transform);
99 }); 106 });
100 107
101 /// Helper function to make it easy to write an `Transformer.apply` method 108 /// Helper function to make it easy to write an `Transformer.apply` method
102 /// that automatically gets and releases the resolver. This is typically used 109 /// that automatically gets and releases the resolver. This is typically used
103 /// as follows: 110 /// as follows:
104 /// 111 ///
105 /// Future apply(Transform transform) { 112 /// Future apply(Transform transform) {
106 /// var entryPoints = ...; // compute entry points 113 /// var entryPoints = ...; // compute entry points
107 /// return applyToEntryPoints(transform, entryPoints); 114 /// return applyToEntryPoints(transform, entryPoints);
108 /// } 115 /// }
109 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) { 116 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) {
110 return resolvers.get(transform, entryPoints).then((resolver) { 117 return resolvers
111 return new Future(() => applyResolver(transform, resolver)).whenComplete( 118 .get(transform, entryPoints, resolveAllLibraries)
112 () { 119 .then((resolver) {
120 return new Future(() => applyResolver(transform, resolver))
121 .whenComplete(() {
113 resolver.release(); 122 resolver.release();
114 }); 123 });
115 }); 124 });
116 } 125 }
117 126
118 /// Invoked when the resolver is ready to be processed. 127 /// Invoked when the resolver is ready to be processed.
119 /// 128 ///
120 /// Return a Future to indicate when apply is completed. 129 /// Return a Future to indicate when apply is completed.
121 applyResolver(Transform transform, Resolver resolver); 130 applyResolver(Transform transform, Resolver resolver);
122 } 131 }
132
133 bool _analysisEngineInitialized = false;
134 _initAnalysisEngine() {
135 if (_analysisEngineInitialized) return;
136 _analysisEngineInitialized = true;
137 AnalysisEngine.instance.processRequiredPlugins();
138 }
OLDNEW
« no previous file with comments | « packages/code_transformers/lib/src/resolver_impl.dart ('k') | packages/code_transformers/lib/src/test_harness.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698