perf(dart/transform) Restructure transform to independent phases
Update summary: - Removes the need for resolution, gaining transform speed at the cost of some precision and ability to detect errors - Generates type registrations in the package alongside their declarations - Ensures that line numbers do not change in transformed user code
This commit is contained in:
@ -1,134 +1,35 @@
|
||||
library angular2.src.transform;
|
||||
|
||||
import 'dart:async';
|
||||
import 'package:analyzer/src/generated/ast.dart';
|
||||
import 'package:analyzer/src/generated/element.dart';
|
||||
import 'package:barback/barback.dart';
|
||||
import 'package:code_transformers/resolver.dart';
|
||||
import 'package:dart_style/dart_style.dart';
|
||||
|
||||
import 'annotation_processor.dart';
|
||||
import 'codegen.dart' as codegen;
|
||||
import 'find_bootstrap.dart';
|
||||
import 'find_reflection_capabilities.dart';
|
||||
import 'logging.dart' as log;
|
||||
import 'options.dart';
|
||||
import 'resolvers.dart';
|
||||
import 'traversal.dart';
|
||||
import 'directive_linker/transformer.dart';
|
||||
import 'directive_processor/transformer.dart';
|
||||
import 'bind_generator/transformer.dart';
|
||||
import 'reflection_remover/transformer.dart';
|
||||
import 'common/formatter.dart' as formatter;
|
||||
import 'common/options.dart';
|
||||
|
||||
export 'options.dart';
|
||||
export 'common/options.dart';
|
||||
|
||||
/// Removes the mirror-based initialization logic and replaces it with static
|
||||
/// logic.
|
||||
class AngularTransformer extends Transformer {
|
||||
final Resolvers _resolvers;
|
||||
final TransformerOptions options;
|
||||
|
||||
AngularTransformer(this.options) : _resolvers = createResolvers();
|
||||
|
||||
factory AngularTransformer.asPlugin(BarbackSettings settings) {
|
||||
var config = settings.configuration;
|
||||
return new AngularTransformer(new TransformerOptions(
|
||||
config[entryPointParam],
|
||||
reflectionEntryPoint: config[reflectionEntryPointParam],
|
||||
newEntryPoint: config[newEntryPointParam]));
|
||||
class AngularTransformerGroup extends TransformerGroup {
|
||||
AngularTransformerGroup(TransformerOptions options) : super([
|
||||
[new DirectiveProcessor(options)],
|
||||
[new DirectiveLinker(options)],
|
||||
[new BindGenerator(options), new ReflectionRemover(options)]
|
||||
]) {
|
||||
formatter.init(new DartFormatter());
|
||||
}
|
||||
|
||||
bool isPrimary(AssetId id) => options.reflectionEntryPoint == id.path;
|
||||
|
||||
Future apply(Transform transform) {
|
||||
log.init(transform);
|
||||
|
||||
var entryPointId =
|
||||
new AssetId(transform.primaryInput.id.package, options.entryPoint);
|
||||
var reflectionEntryPointId = new AssetId(
|
||||
transform.primaryInput.id.package, options.reflectionEntryPoint);
|
||||
var newEntryPointId =
|
||||
new AssetId(transform.primaryInput.id.package, options.newEntryPoint);
|
||||
|
||||
var reflectionExists = transform.hasInput(reflectionEntryPointId);
|
||||
var newEntryPointExists = transform.hasInput(newEntryPointId);
|
||||
|
||||
Resolver myResolver;
|
||||
return Future
|
||||
.wait([reflectionExists, newEntryPointExists])
|
||||
.then((existsList) {
|
||||
if (!existsList[0]) {
|
||||
log.logger.error('Reflection entry point file '
|
||||
'${reflectionEntryPointId} does not exist.');
|
||||
} else if (existsList[1]) {
|
||||
log.logger
|
||||
.error('New entry point file $newEntryPointId already exists.');
|
||||
} else {
|
||||
return _resolvers
|
||||
.get(transform, [entryPointId, reflectionEntryPointId])
|
||||
.then((resolver) {
|
||||
myResolver = resolver;
|
||||
try {
|
||||
String reflectionCapabilitiesCreation = findReflectionCapabilities(
|
||||
resolver, reflectionEntryPointId, newEntryPointId);
|
||||
|
||||
transform.addOutput(new Asset.fromString(
|
||||
reflectionEntryPointId, reflectionCapabilitiesCreation));
|
||||
// Find the call to `new ReflectionCapabilities()`
|
||||
// Generate new source.
|
||||
} catch (err, stackTrace) {
|
||||
log.logger.error('${err}: ${stackTrace}',
|
||||
asset: reflectionEntryPointId);
|
||||
rethrow;
|
||||
}
|
||||
|
||||
try {
|
||||
new _BootstrapFileBuilder(
|
||||
resolver, transform, entryPointId, newEntryPointId).run();
|
||||
} catch (err, stackTrace) {
|
||||
log.logger.error('${err}: ${stackTrace}',
|
||||
asset: transform.primaryInput.id);
|
||||
rethrow;
|
||||
}
|
||||
});
|
||||
}
|
||||
}).whenComplete(() {
|
||||
if (myResolver != null) {
|
||||
myResolver.release();
|
||||
}
|
||||
});
|
||||
factory AngularTransformerGroup.asPlugin(BarbackSettings settings) {
|
||||
return new AngularTransformerGroup(_parseOptions(settings));
|
||||
}
|
||||
}
|
||||
|
||||
class _BootstrapFileBuilder {
|
||||
final Resolver _resolver;
|
||||
final Transform _transform;
|
||||
final AssetId _entryPoint;
|
||||
final AssetId _newEntryPoint;
|
||||
|
||||
_BootstrapFileBuilder(Resolver resolver, Transform transform,
|
||||
this._entryPoint, this._newEntryPoint)
|
||||
: _resolver = resolver,
|
||||
_transform = transform;
|
||||
|
||||
/// Adds the new entry point file to the transform. Should only be ran once.
|
||||
void run() {
|
||||
Set<BootstrapCallInfo> bootstrapCalls =
|
||||
findBootstrapCalls(_resolver, _resolver.getLibrary(_entryPoint));
|
||||
|
||||
log.logger.info('found ${bootstrapCalls.length} call(s) to `bootstrap`');
|
||||
bootstrapCalls.forEach((BootstrapCallInfo info) {
|
||||
log.logger.info('Arg1: ${info.bootstrapType}');
|
||||
});
|
||||
|
||||
var types = new Angular2Types(_resolver);
|
||||
// TODO(kegluneq): Also match [Inject].
|
||||
var matcher = new AnnotationMatcher(
|
||||
new Set.from([types.directiveAnnotation, types.templateAnnotation]));
|
||||
|
||||
var traversal = new AngularVisibleTraversal(types, matcher);
|
||||
bootstrapCalls.forEach((call) => traversal.traverse(call.bootstrapType));
|
||||
|
||||
var context = new codegen.Context();
|
||||
matcher.matchQueue
|
||||
.forEach((entry) => context.directiveRegistry.register(entry));
|
||||
|
||||
_transform.addOutput(new Asset.fromString(_newEntryPoint,
|
||||
codegen.codegenEntryPoint(context, newEntryPoint: _newEntryPoint)));
|
||||
}
|
||||
TransformerOptions _parseOptions(BarbackSettings settings) {
|
||||
var config = settings.configuration;
|
||||
return new TransformerOptions(config[ENTRY_POINT_PARAM],
|
||||
reflectionEntryPoint: config[REFLECTION_ENTRY_POINT_PARAM]);
|
||||
}
|
||||
|
Reference in New Issue
Block a user