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

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

Issue 1400473008: Roll Observatory packages and add a roll script (Closed) Base URL: git@github.com:dart-lang/observatory_pub_packages.git@master
Patch Set: Created 5 years, 2 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
(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(
57 id, () => 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 /// This provides a default implementation of `Transformer.apply` that will
88 /// get and release resolvers automatically. Internally this:
89 /// * Gets a resolver associated with the transform primary input.
90 /// * Does resolution to the code starting from that input.
91 /// * Calls [applyResolver].
92 /// * Then releases the resolver.
93 ///
94 /// Use [applyToEntryPoints] instead if you need to override the entry points
95 /// to run the resolver on.
96 Future apply(Transform transform) =>
97 shouldApplyResolver(transform.primaryInput).then((result) {
98 if (result) return applyToEntryPoints(transform);
99 });
100
101 /// Helper function to make it easy to write an `Transformer.apply` method
102 /// that automatically gets and releases the resolver. This is typically used
103 /// as follows:
104 ///
105 /// Future apply(Transform transform) {
106 /// var entryPoints = ...; // compute entry points
107 /// return applyToEntryPoints(transform, entryPoints);
108 /// }
109 Future applyToEntryPoints(Transform transform, [List<AssetId> entryPoints]) {
110 return resolvers.get(transform, entryPoints).then((resolver) {
111 return new Future(() => applyResolver(transform, resolver)).whenComplete(
112 () {
113 resolver.release();
114 });
115 });
116 }
117
118 /// Invoked when the resolver is ready to be processed.
119 ///
120 /// Return a Future to indicate when apply is completed.
121 applyResolver(Transform transform, Resolver resolver);
122 }
OLDNEW
« no previous file with comments | « code_transformers/lib/src/resolver_impl.dart ('k') | code_transformers/lib/src/test_harness.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698