feat(dart/transform): Allow multiple transformer entry points

- Allow the user to specify multiple entry points to an app.
- Allow the Angular 2 transformer to run without explicit entry points to
generate necessary setters & getters on built-in directives like `For`
and `If`.

Closes #1246
This commit is contained in:
Tim Blasi
2015-04-10 13:41:26 -07:00
parent bba849909c
commit 2cab7c79c3
12 changed files with 120 additions and 74 deletions
@@ -12,14 +12,14 @@ class Codegen {
/// The prefix used to import our generated file.
final String prefix;
/// The import uri
final String importUri;
/// The import uris
final Iterable<String> importUris;
Codegen(String reflectionEntryPointPath, String newEntryPointPath,
Codegen(String reflectionEntryPointPath, Iterable<String> newEntryPointPaths,
{String prefix})
: this.prefix = prefix == null ? _PREFIX_BASE : prefix,
importUri = path.relative(newEntryPointPath,
from: path.dirname(reflectionEntryPointPath)) {
importUris = newEntryPointPaths.map((p) =>
path.relative(p, from: path.dirname(reflectionEntryPointPath))) {
if (this.prefix.isEmpty) throw new ArgumentError.value('(empty)', 'prefix');
}
@@ -43,7 +43,10 @@ class Codegen {
/// The code generated here should follow the example of code generated for
/// an [ImportDirective] node.
String codegenImport() {
return 'import \'${importUri}\' as ${prefix};';
var count = 0;
return importUris
.map((importUri) => 'import \'${importUri}\' as ${prefix}${count++};')
.join('');
}
/// Generates code to call the method which sets up Angular2 reflection
@@ -63,7 +66,11 @@ class Codegen {
reflectorExpression = 'reflector';
}
return '${prefix}.${SETUP_METHOD_NAME}(${reflectorExpression});';
var count = 0;
return importUris
.map((_) =>
'${prefix}${count++}.${SETUP_METHOD_NAME}(${reflectorExpression});')
.join('');
}
}
@@ -1,21 +1,26 @@
library angular2.transform.reflection_remover.remove_reflection_capabilities;
import 'dart:async';
import 'package:analyzer/analyzer.dart';
import 'package:barback/barback.dart';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'codegen.dart';
import 'rewriter.dart';
/// Finds the call to the Angular2 [ReflectionCapabilities] constructor
/// in [code] and replaces it with a call to `setupReflection` in
/// [newEntryPoint].
/// Finds the call to the Angular2 `ReflectionCapabilities` constructor
/// in [reflectionEntryPoint] and replaces it with a call to
/// `setupReflection` in [newEntryPoint].
///
/// [reflectionEntryPointPath] is the path where [code] is defined and is
/// used to display parsing errors.
///
/// This only searches [code] not `part`s, `import`s, `export`s, etc.
String removeReflectionCapabilities(
String code, String reflectionEntryPointPath, String newEntryPointPath) {
var codegen = new Codegen(reflectionEntryPointPath, newEntryPointPath);
/// This only searches the code in [reflectionEntryPoint], not `part`s,
/// `import`s, `export`s, etc.
Future<String> removeReflectionCapabilities(AssetReader reader,
AssetId reflectionEntryPoint, Iterable<AssetId> newEntryPoints) async {
var code = await reader.readAsString(reflectionEntryPoint);
var reflectionEntryPointPath = reflectionEntryPoint.path;
var newEntryPointPaths = newEntryPoints.map((id) => id.path);
var codegen = new Codegen(reflectionEntryPointPath, newEntryPointPaths);
return new Rewriter(code, codegen)
.rewrite(parseCompilationUnit(code, name: reflectionEntryPointPath));
}
@@ -1,6 +1,7 @@
library angular2.transform.reflection_remover.transformer;
import 'dart:async';
import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart' as log;
import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/options.dart';
@@ -23,21 +24,24 @@ class ReflectionRemover extends Transformer {
ReflectionRemover(this.options);
@override
bool isPrimary(AssetId id) => options.reflectionEntryPoint == id.path;
bool isPrimary(AssetId id) => options.reflectionEntryPoints != null &&
options.reflectionEntryPoints.contains(id.path);
@override
Future apply(Transform transform) async {
log.init(transform);
try {
var newEntryPoint = new AssetId(
transform.primaryInput.id.package, options.entryPoint)
.changeExtension(DEPS_EXTENSION);
var newEntryPoints = options.entryPoints.map((entryPoint) {
return new AssetId(transform.primaryInput.id.package, entryPoint)
.changeExtension(DEPS_EXTENSION);
});
var reader = new AssetReader.fromTransform(transform);
var assetCode = await transform.primaryInput.readAsString();
transform.addOutput(new Asset.fromString(transform.primaryInput.id,
removeReflectionCapabilities(
assetCode, transform.primaryInput.id.path, newEntryPoint.path)));
var transformedCode = await removeReflectionCapabilities(
reader, transform.primaryInput.id, newEntryPoints);
transform.addOutput(
new Asset.fromString(transform.primaryInput.id, transformedCode));
} catch (ex, stackTrace) {
log.logger.error('Removing reflection failed.\n'
'Exception: $ex\n'