86f6fac791
With pub build and pub serve the same transformer can be run on the same asset in parallel. We want to maintain the cache between the runs, so making the resolver pipeline these requests. In addition, refactoring to dramatically change the role of ResolverTransformer- previously it was a transformer that automatically kept the AST up to date, but this does not work well when in a pipeline of transformers that are generating or modifying code. What was ResolverTransformer is Resolvers- a cache of resolvers for which individual transformers now need to request the resolver from. ResolverTransformer is now a mixin which can be used to automatically release a Resolver when it's done being used. BUG= R=sigmund@google.com Review URL: https://codereview.chromium.org//181383015 git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@33163 260f80e4-7a28-3924-810f-c04153c831b5
61 lines
2.1 KiB
Dart
61 lines
2.1 KiB
Dart
// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
library code_transformers.src.resolvers;
|
|
|
|
import 'dart:async';
|
|
import 'package:barback/barback.dart' show AssetId, Transformer, Transform;
|
|
|
|
import 'resolver.dart';
|
|
import 'resolver_impl.dart';
|
|
|
|
/// Barback-based code resolvers which maintains up-to-date resolved ASTs for
|
|
/// the specified code entry points.
|
|
///
|
|
/// This can used by transformers dependent on resolved ASTs to handle the
|
|
/// resolution of the AST and cache the results between compilations.
|
|
///
|
|
/// If multiple transformers rely on a resolved AST they should (ideally) share
|
|
/// the same Resolvers object to minimize re-parsing the AST.
|
|
class Resolvers {
|
|
final Map<AssetId, ResolverImpl> _resolvers = {};
|
|
final String dartSdkDirectory;
|
|
|
|
Resolvers(this.dartSdkDirectory);
|
|
|
|
/// Get a resolver for the AST starting from [id].
|
|
///
|
|
/// [Resolver.release] must be called once it's done being used, or
|
|
/// [ResolverTransformer] should be used to automatically release the
|
|
/// resolver.
|
|
Future<Resolver> get(Transform transform) {
|
|
var id = transform.primaryInput.id;
|
|
var resolver = _resolvers.putIfAbsent(id,
|
|
() => new ResolverImpl(id, dartSdkDirectory));
|
|
return resolver.resolve(transform);
|
|
}
|
|
}
|
|
|
|
/// Transformer mixin which automatically gets and releases resolvers.
|
|
///
|
|
/// To use mix this class in, set the resolvers field and override
|
|
/// [applyResolver].
|
|
abstract class ResolverTransformer implements Transformer {
|
|
/// The cache of resolvers- must be set from subclass.
|
|
Resolvers resolvers;
|
|
|
|
Future apply(Transform transform) {
|
|
return resolvers.get(transform).then((resolver) {
|
|
return new Future.value(applyResolver(transform, resolver)).then((_) {
|
|
resolver.release();
|
|
});
|
|
});
|
|
}
|
|
|
|
/// Invoked when the resolver is ready to be processed.
|
|
///
|
|
/// Return a Future to indicate when apply is completed.
|
|
applyResolver(Transform transform, Resolver resolver);
|
|
}
|