2015-03-20 15:37:09 -07:00
|
|
|
library angular2.transform.template_compiler.generator;
|
2015-03-13 13:24:56 -07:00
|
|
|
|
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:angular2/src/change_detection/parser/lexer.dart' as ng;
|
|
|
|
import 'package:angular2/src/change_detection/parser/parser.dart' as ng;
|
2015-05-01 17:29:58 -07:00
|
|
|
import 'package:angular2/src/render/api.dart';
|
2015-05-05 10:31:21 -07:00
|
|
|
import 'package:angular2/src/render/dom/compiler/compiler.dart';
|
2015-05-01 17:29:58 -07:00
|
|
|
import 'package:angular2/src/render/dom/compiler/template_loader.dart';
|
2015-05-08 17:29:21 -07:00
|
|
|
import 'package:angular2/src/services/xhr.dart' show XHR;
|
2015-03-13 13:24:56 -07:00
|
|
|
import 'package:angular2/src/reflection/reflection.dart';
|
2015-05-01 17:29:58 -07:00
|
|
|
import 'package:angular2/src/services/url_resolver.dart';
|
2015-03-13 13:24:56 -07:00
|
|
|
import 'package:angular2/src/transform/common/asset_reader.dart';
|
|
|
|
import 'package:angular2/src/transform/common/names.dart';
|
2015-04-09 17:51:06 -07:00
|
|
|
import 'package:angular2/src/transform/common/property_utils.dart' as prop;
|
2015-05-08 17:29:21 -07:00
|
|
|
import 'package:angular2/src/transform/common/xhr_impl.dart';
|
2015-03-13 13:24:56 -07:00
|
|
|
import 'package:barback/barback.dart';
|
|
|
|
|
2015-05-01 17:29:58 -07:00
|
|
|
import 'compile_step_factory.dart';
|
2015-03-13 13:24:56 -07:00
|
|
|
import 'recording_reflection_capabilities.dart';
|
2015-05-05 10:31:21 -07:00
|
|
|
import 'view_definition_creator.dart';
|
2015-03-13 13:24:56 -07:00
|
|
|
|
2015-03-13 13:55:49 -07:00
|
|
|
/// Reads the `.ng_deps.dart` file represented by `entryPoint` and parses any
|
2015-04-09 21:20:11 +02:00
|
|
|
/// Angular 2 `View` annotations it declares to generate `getter`s,
|
2015-03-13 13:55:49 -07:00
|
|
|
/// `setter`s, and `method`s that would otherwise be reflectively accessed.
|
|
|
|
///
|
2015-04-17 13:01:07 -07:00
|
|
|
/// This method assumes a {@link DomAdapter} has been registered.
|
2015-03-13 13:24:56 -07:00
|
|
|
Future<String> processTemplates(AssetReader reader, AssetId entryPoint) async {
|
2015-05-05 10:31:21 -07:00
|
|
|
var viewDefResults = await createViewDefinitions(reader, entryPoint);
|
2015-05-01 17:29:58 -07:00
|
|
|
var extractor = new _TemplateExtractor(new XhrImpl(reader, entryPoint));
|
2015-03-13 13:24:56 -07:00
|
|
|
|
|
|
|
var registrations = new StringBuffer();
|
2015-05-05 10:31:21 -07:00
|
|
|
for (var viewDef in viewDefResults.viewDefinitions.values) {
|
|
|
|
var values = await extractor.extractTemplates(viewDef);
|
2015-05-01 17:29:58 -07:00
|
|
|
if (values == null) continue;
|
2015-05-05 10:31:21 -07:00
|
|
|
var calls = _generateGetters(values.getterNames);
|
2015-05-01 17:29:58 -07:00
|
|
|
if (calls.isNotEmpty) {
|
|
|
|
registrations.write('..${REGISTER_GETTERS_METHOD_NAME}'
|
|
|
|
'({${calls.join(', ')}})');
|
|
|
|
}
|
2015-05-05 10:31:21 -07:00
|
|
|
calls = _generateSetters(values.setterNames);
|
2015-05-01 17:29:58 -07:00
|
|
|
if (calls.isNotEmpty) {
|
|
|
|
registrations.write('..${REGISTER_SETTERS_METHOD_NAME}'
|
|
|
|
'({${calls.join(', ')}})');
|
|
|
|
}
|
2015-05-05 10:31:21 -07:00
|
|
|
calls = _generateMethods(values.methodNames);
|
2015-05-01 17:29:58 -07:00
|
|
|
if (calls.isNotEmpty) {
|
|
|
|
registrations.write('..${REGISTER_METHODS_METHOD_NAME}'
|
|
|
|
'({${calls.join(', ')}})');
|
|
|
|
}
|
2015-03-19 09:16:01 -07:00
|
|
|
}
|
2015-03-13 13:24:56 -07:00
|
|
|
|
2015-05-05 10:31:21 -07:00
|
|
|
var code = viewDefResults.ngDeps.code;
|
2015-03-13 13:24:56 -07:00
|
|
|
if (registrations.length == 0) return code;
|
2015-05-05 10:31:21 -07:00
|
|
|
var codeInjectIdx =
|
|
|
|
viewDefResults.ngDeps.registeredTypes.last.registerMethod.end;
|
2015-03-13 13:24:56 -07:00
|
|
|
return '${code.substring(0, codeInjectIdx)}'
|
|
|
|
'${registrations}'
|
|
|
|
'${code.substring(codeInjectIdx)}';
|
|
|
|
}
|
|
|
|
|
2015-05-05 10:31:21 -07:00
|
|
|
Iterable<String> _generateGetters(Iterable<String> getterNames) {
|
2015-04-09 17:51:06 -07:00
|
|
|
return getterNames.map((getterName) {
|
|
|
|
if (!prop.isValid(getterName)) {
|
|
|
|
// TODO(kegluenq): Eagerly throw here once #1295 is addressed.
|
|
|
|
return prop.lazyInvalidGetter(getterName);
|
|
|
|
} else {
|
|
|
|
return ''' '${prop.sanitize(getterName)}': (o) => o.$getterName''';
|
|
|
|
}
|
|
|
|
});
|
2015-03-13 13:55:49 -07:00
|
|
|
}
|
|
|
|
|
2015-05-05 10:31:21 -07:00
|
|
|
Iterable<String> _generateSetters(Iterable<String> setterName) {
|
2015-04-09 17:51:06 -07:00
|
|
|
return setterName.map((setterName) {
|
|
|
|
if (!prop.isValid(setterName)) {
|
|
|
|
// TODO(kegluenq): Eagerly throw here once #1295 is addressed.
|
|
|
|
return prop.lazyInvalidSetter(setterName);
|
|
|
|
} else {
|
|
|
|
return ''' '${prop.sanitize(setterName)}': '''
|
|
|
|
''' (o, v) => o.$setterName = v ''';
|
|
|
|
}
|
|
|
|
});
|
2015-03-13 13:55:49 -07:00
|
|
|
}
|
|
|
|
|
2015-05-05 10:31:21 -07:00
|
|
|
Iterable<String> _generateMethods(Iterable<String> methodNames) {
|
2015-04-09 17:51:06 -07:00
|
|
|
return methodNames.map((methodName) {
|
|
|
|
if (!prop.isValid(methodName)) {
|
|
|
|
// TODO(kegluenq): Eagerly throw here once #1295 is addressed.
|
|
|
|
return prop.lazyInvalidMethod(methodName);
|
|
|
|
} else {
|
|
|
|
return ''' '${prop.sanitize(methodName)}': '''
|
|
|
|
'(o, List args) => Function.apply(o.$methodName, args) ';
|
|
|
|
}
|
|
|
|
});
|
2015-03-13 13:55:49 -07:00
|
|
|
}
|
|
|
|
|
2015-04-09 21:20:11 +02:00
|
|
|
/// Extracts `template` and `url` values from `View` annotations, reads
|
2015-03-19 09:16:01 -07:00
|
|
|
/// template code if necessary, and determines what values will be
|
|
|
|
/// reflectively accessed from that template.
|
|
|
|
class _TemplateExtractor {
|
2015-05-05 10:31:21 -07:00
|
|
|
final RenderCompiler _compiler;
|
2015-03-19 09:16:01 -07:00
|
|
|
|
2015-05-05 10:31:21 -07:00
|
|
|
_TemplateExtractor(XHR xhr) : _compiler = new DomCompiler(
|
|
|
|
new CompileStepFactory(new ng.Parser(new ng.Lexer())),
|
|
|
|
new TemplateLoader(xhr, new UrlResolver()));
|
2015-03-13 13:24:56 -07:00
|
|
|
|
2015-05-05 10:31:21 -07:00
|
|
|
Future<RecordingReflectionCapabilities> extractTemplates(
|
2015-05-01 17:29:58 -07:00
|
|
|
ViewDefinition viewDef) async {
|
|
|
|
// Check for "imperative views".
|
|
|
|
if (viewDef.template == null && viewDef.absUrl == null) return null;
|
|
|
|
|
2015-03-19 09:16:01 -07:00
|
|
|
var savedReflectionCapabilities = reflector.reflectionCapabilities;
|
2015-05-05 10:31:21 -07:00
|
|
|
var recordingCapabilities = new RecordingReflectionCapabilities();
|
2015-03-19 09:16:01 -07:00
|
|
|
reflector.reflectionCapabilities = recordingCapabilities;
|
2015-03-13 13:24:56 -07:00
|
|
|
|
2015-05-01 17:29:58 -07:00
|
|
|
// TODO(kegluneq): Rewrite url to inline `template` where possible.
|
2015-05-05 10:31:21 -07:00
|
|
|
await _compiler.compile(viewDef);
|
2015-03-19 09:16:01 -07:00
|
|
|
|
|
|
|
reflector.reflectionCapabilities = savedReflectionCapabilities;
|
|
|
|
return recordingCapabilities;
|
|
|
|
}
|
2015-03-13 13:24:56 -07:00
|
|
|
}
|