feat(dart/transform): Track timing of transform tasks

This commit is contained in:
Tim Blasi 2015-10-06 17:02:51 -07:00
parent aee176115b
commit 07572652ff
17 changed files with 246 additions and 183 deletions

View File

@ -51,6 +51,24 @@ BuildLogger get logger {
return current == null ? new PrintLogger() : current; return current == null ? new PrintLogger() : current;
} }
/// Writes a log entry at `LogLevel.FINE` granularity with the time taken by
/// `asyncOperation`.
///
/// Returns the result of executing `asyncOperation`.
Future logElapsedAsync(Future asyncOperation(),
{String operationName: 'unknown', AssetId assetId}) async {
final timer = new Stopwatch()..start();
final result = await asyncOperation();
timer.stop();
final buf =
new StringBuffer('[$operationName] took ${timer.elapsedMilliseconds} ms');
if (assetId != null) {
buf.write(' on $assetId');
}
logger.fine(buf.toString(), asset: assetId);
return result;
}
class PrintLogger implements BuildLogger { class PrintLogger implements BuildLogger {
@override @override
final String detailsUri = ''; final String detailsUri = '';

View File

@ -30,40 +30,42 @@ class Rewriter {
var node = parseCompilationUnit(code); var node = parseCompilationUnit(code);
if (node == null) return null; if (node == null) return null;
var visitor = new _FindDeferredLibraries(_reader, _entryPoint); return logElapsedAsync(() async {
node.accept(visitor); var visitor = new _FindDeferredLibraries(_reader, _entryPoint);
// Look to see if we found any deferred libraries node.accept(visitor);
if (!visitor.hasDeferredLibrariesToRewrite()) return null; // Look to see if we found any deferred libraries
// Remove any libraries that don't need angular codegen. if (!visitor.hasDeferredLibrariesToRewrite()) return null;
await visitor.cull(); // Remove any libraries that don't need angular codegen.
// Check again if there are any deferred libraries. await visitor.cull();
if (!visitor.hasDeferredLibrariesToRewrite()) return null; // Check again if there are any deferred libraries.
if (!visitor.hasDeferredLibrariesToRewrite()) return null;
var compare = (AstNode a, AstNode b) => a.offset - b.offset; var compare = (AstNode a, AstNode b) => a.offset - b.offset;
visitor.deferredImports.sort(compare); visitor.deferredImports.sort(compare);
visitor.loadLibraryInvocations.sort(compare); visitor.loadLibraryInvocations.sort(compare);
var buf = new StringBuffer(); var buf = new StringBuffer();
var idx = var idx =
visitor.deferredImports.fold(0, (int lastIdx, ImportDirective node) { visitor.deferredImports.fold(0, (int lastIdx, ImportDirective node) {
buf.write(code.substring(lastIdx, node.offset)); buf.write(code.substring(lastIdx, node.offset));
var import = code.substring(node.offset, node.end); var import = code.substring(node.offset, node.end);
buf.write(import.replaceFirst('.dart', DEPS_EXTENSION)); buf.write(import.replaceFirst('.dart', DEPS_EXTENSION));
return node.end; return node.end;
}); });
idx = visitor.loadLibraryInvocations.fold(idx, idx = visitor.loadLibraryInvocations.fold(idx,
(int lastIdx, MethodInvocation node) { (int lastIdx, MethodInvocation node) {
buf.write(code.substring(lastIdx, node.offset)); buf.write(code.substring(lastIdx, node.offset));
var value = node.realTarget as SimpleIdentifier; var value = node.realTarget as SimpleIdentifier;
var prefix = value.name; var prefix = value.name;
// Chain a future that initializes the reflector. // Chain a future that initializes the reflector.
buf.write('$prefix.loadLibrary().then((_) {$prefix.initReflector();})'); buf.write('$prefix.loadLibrary().then((_) {$prefix.initReflector();})');
return node.end; return node.end;
}); });
if (idx < code.length) buf.write(code.substring(idx)); if (idx < code.length) buf.write(code.substring(idx));
return '$buf'; return '$buf';
}, operationName: 'rewriteDeferredLibraries', assetId: _entryPoint);
} }
} }

View File

@ -20,42 +20,43 @@ import 'package:barback/barback.dart';
/// `isNgDeps` to `true` to signify that it is a dependency on which we need to /// `isNgDeps` to `true` to signify that it is a dependency on which we need to
/// call `initReflector`. /// call `initReflector`.
Future<NgDepsModel> linkNgDeps(NgDepsModel ngDepsModel, AssetReader reader, Future<NgDepsModel> linkNgDeps(NgDepsModel ngDepsModel, AssetReader reader,
AssetId entryPoint, UrlResolver resolver) async { AssetId assetId, UrlResolver resolver) async {
if (ngDepsModel == null) return null; if (ngDepsModel == null) return null;
var linkedDepsMap = return logElapsedAsync(() async {
await _processNgImports(ngDepsModel, reader, entryPoint, resolver); var linkedDepsMap =
await _processNgImports(ngDepsModel, reader, assetId, resolver);
if (linkedDepsMap.isEmpty) { if (linkedDepsMap.isEmpty) {
// We are not calling `initReflector` on any other libraries, but we still // We are not calling `initReflector` on any other libraries, but we still
// return the model to ensure it is written to code. // return the model to ensure it is written to code.
// TODO(kegluneq): Continue using the protobuf format after this phase. // TODO(kegluneq): Continue using the protobuf format after this phase.
return ngDepsModel;
}
for (var i = ngDepsModel.imports.length - 1; i >= 0; --i) {
var import = ngDepsModel.imports[i];
if (linkedDepsMap.containsKey(import.uri)) {
var linkedModel = new ImportModel()
..isNgDeps = true
..uri = toDepsExtension(import.uri)
..prefix = 'i$i';
// TODO(kegluneq): Preserve combinators?
ngDepsModel.imports.insert(i + 1, linkedModel);
}
}
for (var i = 0, iLen = ngDepsModel.exports.length; i < iLen; ++i) {
var export = ngDepsModel.exports[i];
if (linkedDepsMap.containsKey(export.uri)) {
var linkedModel = new ImportModel()
..isNgDeps = true
..uri = toDepsExtension(export.uri)
..prefix = 'i${ngDepsModel.imports.length}';
// TODO(kegluneq): Preserve combinators?
ngDepsModel.imports.add(linkedModel);
}
}
return ngDepsModel; return ngDepsModel;
} }, operationName: 'linkNgDeps', assetId: assetId);
for (var i = ngDepsModel.imports.length - 1; i >= 0; --i) {
var import = ngDepsModel.imports[i];
if (linkedDepsMap.containsKey(import.uri)) {
var linkedModel = new ImportModel()
..isNgDeps = true
..uri = toDepsExtension(import.uri)
..prefix = 'i$i';
// TODO(kegluneq): Preserve combinators?
ngDepsModel.imports.insert(i + 1, linkedModel);
}
}
for (var i = 0, iLen = ngDepsModel.exports.length; i < iLen; ++i) {
var export = ngDepsModel.exports[i];
if (linkedDepsMap.containsKey(export.uri)) {
var linkedModel = new ImportModel()
..isNgDeps = true
..uri = toDepsExtension(export.uri)
..prefix = 'i${ngDepsModel.imports.length}';
// TODO(kegluneq): Preserve combinators?
ngDepsModel.imports.add(linkedModel);
}
}
return ngDepsModel;
} }
bool _isNotDartDirective(dynamic model) => !isDartCoreUri(model.uri); bool _isNotDartDirective(dynamic model) => !isDartCoreUri(model.uri);

View File

@ -27,14 +27,16 @@ import 'ng_deps_linker.dart';
/// Returns an empty [NgMeta] if there are no `Directive`-annotated classes or /// Returns an empty [NgMeta] if there are no `Directive`-annotated classes or
/// `DirectiveAlias` annotated constants in `entryPoint`. /// `DirectiveAlias` annotated constants in `entryPoint`.
Future<NgMeta> linkDirectiveMetadata( Future<NgMeta> linkDirectiveMetadata(
AssetReader reader, AssetId entryPoint) async { AssetReader reader, AssetId assetId) async {
var ngMeta = await _readNgMeta(reader, entryPoint); var ngMeta = await _readNgMeta(reader, assetId);
if (ngMeta == null || ngMeta.isEmpty) return null; if (ngMeta == null || ngMeta.isEmpty) return null;
await Future.wait([ await Future.wait([
linkNgDeps(ngMeta.ngDeps, reader, entryPoint, _urlResolver), linkNgDeps(ngMeta.ngDeps, reader, assetId, _urlResolver),
_linkDirectiveMetadataRecursive( logElapsedAsync(() async {
ngMeta, reader, entryPoint, new Set<String>()) await _linkRecursive(ngMeta, reader, assetId, new Set<String>());
return ngMeta;
}, operationName: 'linkDirectiveMetadata', assetId: assetId)
]); ]);
return ngMeta; return ngMeta;
} }
@ -50,8 +52,8 @@ Future<NgMeta> _readNgMeta(AssetReader reader, AssetId ngMetaAssetId) async {
final _urlResolver = const TransformerUrlResolver(); final _urlResolver = const TransformerUrlResolver();
Future<NgMeta> _linkDirectiveMetadataRecursive(NgMeta ngMeta, Future _linkRecursive(NgMeta ngMeta, AssetReader reader, AssetId assetId,
AssetReader reader, AssetId assetId, Set<String> seen) async { Set<String> seen) async {
if (ngMeta == null || if (ngMeta == null ||
ngMeta.ngDeps == null || ngMeta.ngDeps == null ||
ngMeta.ngDeps.exports == null) { ngMeta.ngDeps.exports == null) {
@ -59,30 +61,23 @@ Future<NgMeta> _linkDirectiveMetadataRecursive(NgMeta ngMeta,
} }
var assetUri = toAssetUri(assetId); var assetUri = toAssetUri(assetId);
return Future return Future.wait(ngMeta.ngDeps.exports
.wait(ngMeta.ngDeps.exports .where((export) => !isDartCoreUri(export.uri))
.where((export) => !isDartCoreUri(export.uri)) .map((export) =>
.map((export) => _urlResolver.resolve(assetUri, toMetaExtension(export.uri)))
_urlResolver.resolve(assetUri, toMetaExtension(export.uri))) .where((uri) => !seen.contains(uri))
.where((uri) => !seen.contains(uri)) .map((uri) async {
.map((uri) async {
seen.add(uri); seen.add(uri);
try { try {
final exportAssetId = fromUri(uri); final exportAssetId = fromUri(uri);
if (await reader.hasInput(exportAssetId)) { final exportNgMeta = await _readNgMeta(reader, exportAssetId);
var exportNgMetaJson = await reader.readAsString(exportAssetId); if (exportNgMeta != null) {
if (exportNgMetaJson == null) return null; await _linkRecursive(exportNgMeta, reader, exportAssetId, seen);
var exportNgMeta = new NgMeta.fromJson(JSON.decode(exportNgMetaJson)); ngMeta.addAll(exportNgMeta);
await _linkDirectiveMetadataRecursive(
exportNgMeta, reader, exportAssetId, seen);
if (exportNgMeta != null) {
ngMeta.addAll(exportNgMeta);
}
} }
} catch (err, st) { } catch (err, st) {
// Log and continue. // Log and continue.
logger.warning('Failed to fetch $uri. Message: $err.\n$st'); logger.warning('Failed to fetch $uri. Message: $err.\n$st');
} }
})) }));
.then((_) => ngMeta);
} }

View File

@ -37,7 +37,9 @@ Future<String> inlineParts(AssetReader reader, AssetId assetId) async {
// parent, so it does not need its own `.ng_deps.dart` file. // parent, so it does not need its own `.ng_deps.dart` file.
if (directivesVisitor.isPart) return null; if (directivesVisitor.isPart) return null;
return _getAllDeclarations(reader, assetId, code, directivesVisitor); return logElapsedAsync(() {
return _getAllDeclarations(reader, assetId, code, directivesVisitor);
}, operationName: 'inlineParts', assetId: assetId);
} }
/// Processes `visitor.parts`, reading and appending their contents to the /// Processes `visitor.parts`, reading and appending their contents to the

View File

@ -17,7 +17,7 @@ import 'package:angular2/src/core/compiler/template_compiler.dart';
import 'inliner.dart'; import 'inliner.dart';
/// Generates an instance of [NgMeta] describing the file at `assetId`. /// Generates an instance of [NgMeta] describing the file at `assetId`.
Future<NgMeta> createNgDeps(AssetReader reader, AssetId assetId, Future<NgMeta> createNgMeta(AssetReader reader, AssetId assetId,
AnnotationMatcher annotationMatcher) async { AnnotationMatcher annotationMatcher) async {
// TODO(kegluneq): Shortcut if we can determine that there are no // TODO(kegluneq): Shortcut if we can determine that there are no
// [Directive]s present, taking into account `export`s. // [Directive]s present, taking into account `export`s.
@ -26,18 +26,22 @@ Future<NgMeta> createNgDeps(AssetReader reader, AssetId assetId,
var parsedCode = var parsedCode =
parseCompilationUnit(codeWithParts, name: '${assetId.path} and parts'); parseCompilationUnit(codeWithParts, name: '${assetId.path} and parts');
var ngDepsVisitor = new NgDepsVisitor(assetId, annotationMatcher); final ngDepsVisitor = await logElapsedAsync(() async {
parsedCode.accept(ngDepsVisitor); var ngDepsVisitor = new NgDepsVisitor(assetId, annotationMatcher);
parsedCode.accept(ngDepsVisitor);
return ngDepsVisitor;
}, operationName: 'createNgDeps', assetId: assetId);
var ngMeta = new NgMeta(ngDeps: ngDepsVisitor.model); return logElapsedAsync(() async {
var ngMeta = new NgMeta(ngDeps: ngDepsVisitor.model);
var templateCompiler = createTemplateCompiler(reader); var templateCompiler = createTemplateCompiler(reader);
var ngMetaVisitor = new _NgMetaVisitor( var ngMetaVisitor = new _NgMetaVisitor(ngMeta, assetId, annotationMatcher,
ngMeta, assetId, annotationMatcher, _interfaceMatcher, templateCompiler); _interfaceMatcher, templateCompiler);
parsedCode.accept(ngMetaVisitor); parsedCode.accept(ngMetaVisitor);
await ngMetaVisitor.whenDone(); await ngMetaVisitor.whenDone();
return ngMeta;
return ngMeta; }, operationName: 'createNgMeta', assetId: assetId);
} }
// TODO(kegluneq): Allow the caller to provide an InterfaceMatcher. // TODO(kegluneq): Allow the caller to provide an InterfaceMatcher.

View File

@ -45,7 +45,7 @@ class DirectiveProcessor extends Transformer implements DeclaringTransformer {
var primaryId = transform.primaryInput.id; var primaryId = transform.primaryInput.id;
var reader = new AssetReader.fromTransform(transform); var reader = new AssetReader.fromTransform(transform);
var ngMeta = var ngMeta =
await createNgDeps(reader, primaryId, options.annotationMatcher); await createNgMeta(reader, primaryId, options.annotationMatcher);
if (ngMeta == null || ngMeta.isEmpty) { if (ngMeta == null || ngMeta.isEmpty) {
return; return;
} }

View File

@ -4,6 +4,7 @@ import 'dart:async';
import 'package:angular2/src/transform/common/asset_reader.dart'; import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/code/source_module.dart'; import 'package:angular2/src/transform/common/code/source_module.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/names.dart'; import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/ng_compiler.dart'; import 'package:angular2/src/transform/common/ng_compiler.dart';
import 'package:angular2/src/core/compiler/source_module.dart'; import 'package:angular2/src/core/compiler/source_module.dart';
@ -21,11 +22,11 @@ Future<Iterable<Asset>> processStylesheet(
final stylesheetUrl = '${stylesheetId.package}|${stylesheetId.path}'; final stylesheetUrl = '${stylesheetId.package}|${stylesheetId.path}';
final templateCompiler = createTemplateCompiler(reader); final templateCompiler = createTemplateCompiler(reader);
final cssText = await reader.readAsString(stylesheetId); final cssText = await reader.readAsString(stylesheetId);
final sourceModules = return logElapsedAsync(() async {
templateCompiler.compileStylesheetCodeGen(stylesheetUrl, cssText); final sourceModules =
templateCompiler.compileStylesheetCodeGen(stylesheetUrl, cssText);
return sourceModules.map((SourceModule module) => new Asset.fromString( return sourceModules.map((SourceModule module) => new Asset.fromString(
new AssetId.parse('${module.moduleUrl}'), new AssetId.parse('${module.moduleUrl}'), writeSourceModule(module)));
writeSourceModule(module))); }, operationName: 'processStylesheet', assetId: stylesheetId);
} }

View File

@ -19,9 +19,11 @@ import 'package:code_transformers/assets.dart';
/// ///
/// The returned value wraps the [NgDeps] at `entryPoint` as well as these /// The returned value wraps the [NgDeps] at `entryPoint` as well as these
/// created objects. /// created objects.
Future<CompileDataResults> createCompileData(AssetReader reader, Future<CompileDataResults> createCompileData(
AssetId entryPoint) async { AssetReader reader, AssetId assetId) async {
return new _CompileDataCreator(reader, entryPoint).createCompileData(); return logElapsedAsync(() {
return new _CompileDataCreator(reader, assetId).createCompileData();
}, operationName: 'createCompileData', assetId: assetId);
} }
class CompileDataResults { class CompileDataResults {

View File

@ -10,6 +10,7 @@ import 'package:angular2/src/core/facade/lang.dart';
import 'package:angular2/src/core/reflection/reflection.dart'; import 'package:angular2/src/core/reflection/reflection.dart';
import 'package:angular2/src/transform/common/asset_reader.dart'; import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/code/source_module.dart'; import 'package:angular2/src/transform/common/code/source_module.dart';
import 'package:angular2/src/transform/common/logging.dart';
import 'package:angular2/src/transform/common/names.dart'; import 'package:angular2/src/transform/common/names.dart';
import 'package:angular2/src/transform/common/ng_compiler.dart'; import 'package:angular2/src/transform/common/ng_compiler.dart';
import 'package:angular2/src/transform/common/ng_deps.dart'; import 'package:angular2/src/transform/common/ng_deps.dart';
@ -21,14 +22,14 @@ import 'reflection/processor.dart' as reg;
import 'reflection/reflection_capabilities.dart'; import 'reflection/reflection_capabilities.dart';
import 'compile_data_creator.dart'; import 'compile_data_creator.dart';
/// Reads the `.ng_deps.dart` file represented by `entryPoint` and parses any /// Reads the `.ng_deps.dart` file represented by `assetId` and parses any
/// Angular 2 `View` annotations it declares to generate `getter`s, /// Angular 2 `View` annotations it declares to generate `getter`s,
/// `setter`s, and `method`s that would otherwise be reflectively accessed. /// `setter`s, and `method`s that would otherwise be reflectively accessed.
/// ///
/// This method assumes a {@link DomAdapter} has been registered. /// This method assumes a {@link DomAdapter} has been registered.
Future<Outputs> processTemplates(AssetReader reader, AssetId entryPoint, Future<Outputs> processTemplates(AssetReader reader, AssetId assetId,
{bool reflectPropertiesAsAttributes: false}) async { {bool reflectPropertiesAsAttributes: false}) async {
var viewDefResults = await createCompileData(reader, entryPoint); var viewDefResults = await createCompileData(reader, assetId);
var codegen = null; var codegen = null;
if (viewDefResults.directiveMetadatas.isNotEmpty) { if (viewDefResults.directiveMetadatas.isNotEmpty) {
var processor = new reg.Processor(); var processor = new reg.Processor();
@ -44,16 +45,18 @@ Future<Outputs> processTemplates(AssetReader reader, AssetId entryPoint,
var compileData = var compileData =
viewDefResults.viewDefinitions.values.toList(growable: false); viewDefResults.viewDefinitions.values.toList(growable: false);
if (compileData.isEmpty) { if (compileData.isEmpty) {
return new Outputs(entryPoint, ngDeps, codegen, null, null); return new Outputs(assetId, ngDeps, codegen, null, null);
} }
var savedReflectionCapabilities = reflector.reflectionCapabilities; var savedReflectionCapabilities = reflector.reflectionCapabilities;
reflector.reflectionCapabilities = const NullReflectionCapabilities(); reflector.reflectionCapabilities = const NullReflectionCapabilities();
var compiledTemplates = templateCompiler.compileTemplatesCodeGen(compileData); final compiledTemplates = await logElapsedAsync(() async {
return templateCompiler.compileTemplatesCodeGen(compileData);
}, operationName: 'compileTemplatesCodegen', assetId: assetId);
reflector.reflectionCapabilities = savedReflectionCapabilities; reflector.reflectionCapabilities = savedReflectionCapabilities;
return new Outputs(entryPoint, ngDeps, codegen, return new Outputs(assetId, ngDeps, codegen, viewDefResults.viewDefinitions,
viewDefResults.viewDefinitions, compiledTemplates); compiledTemplates);
} }
AssetId templatesAssetId(AssetId ngDepsAssetId) => AssetId templatesAssetId(AssetId ngDepsAssetId) =>
@ -87,7 +90,8 @@ class Outputs {
Map<RegisteredType, Map<RegisteredType,
NormalizedComponentWithViewDirectives> compileDataMap) { NormalizedComponentWithViewDirectives> compileDataMap) {
var code = ngDeps.code; var code = ngDeps.code;
if (accessors == null && (compileDataMap == null || compileDataMap.isEmpty)) return code; if (accessors == null &&
(compileDataMap == null || compileDataMap.isEmpty)) return code;
if (ngDeps.registeredTypes.isEmpty) return code; if (ngDeps.registeredTypes.isEmpty) return code;
var beginRegistrationsIdx = var beginRegistrationsIdx =
@ -106,7 +110,8 @@ class Outputs {
buf = new StringBuffer('${code.substring(0, beginRegistrationsIdx)}'); buf = new StringBuffer('${code.substring(0, beginRegistrationsIdx)}');
} }
for (var registeredType in ngDeps.registeredTypes) { for (var registeredType in ngDeps.registeredTypes) {
if (compileDataMap != null && compileDataMap.containsKey(registeredType)) { if (compileDataMap != null &&
compileDataMap.containsKey(registeredType)) {
// We generated a template for this type, so add the generated // We generated a template for this type, so add the generated
// `CompiledTemplate` value as the final annotation in the list. // `CompiledTemplate` value as the final annotation in the list.
var annotations = registeredType.annotations as ListLiteral; var annotations = registeredType.annotations as ListLiteral;

View File

@ -0,0 +1,33 @@
library angular2.test.transform.common.read_file;
import 'package:barback/barback.dart';
import 'package:code_transformers/messages/build_logger.dart';
import 'package:source_span/source_span.dart';
class RecordingLogger implements BuildLogger {
@override
final String detailsUri = '';
@override
final bool convertErrorsToWarnings = false;
bool hasErrors = false;
List<String> logs = [];
void _record(prefix, msg) => logs.add('$prefix: $msg');
void info(msg, {AssetId asset, SourceSpan span}) => _record('INFO', msg);
void fine(msg, {AssetId asset, SourceSpan span}) => _record('FINE', msg);
void warning(msg, {AssetId asset, SourceSpan span}) => _record('WARN', msg);
void error(msg, {AssetId asset, SourceSpan span}) {
hasErrors = true;
_record('ERROR', msg);
}
Future writeOutput() => throw new UnimplementedError();
Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) =>
throw new UnimplementedError();
}

View File

@ -2,14 +2,12 @@ library angular2.test.transform.deferred_rewriter.all_tests;
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
import 'package:angular2/src/transform/deferred_rewriter/transformer.dart'; import 'package:angular2/src/transform/deferred_rewriter/transformer.dart';
import 'package:angular2/src/transform/common/annotation_matcher.dart';
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/logging.dart' as log;
import 'package:code_transformers/messages/build_logger.dart';
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'package:guinness/guinness.dart'; import 'package:guinness/guinness.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
import '../common/read_file.dart'; import '../common/read_file.dart';
import '../common/recording_logger.dart';
var formatter = new DartFormatter(); var formatter = new DartFormatter();
@ -36,21 +34,23 @@ void allTests() {
} }
void _testRewriteDeferredLibraries(String name, String inputPath) { void _testRewriteDeferredLibraries(String name, String inputPath) {
it(name, () async { it(name, () {
var inputId = _assetIdForPath(inputPath); return log.setZoned(new RecordingLogger(), () async {
var reader = new TestAssetReader(); var inputId = _assetIdForPath(inputPath);
var expectedPath = path.join( var reader = new TestAssetReader();
path.dirname(inputPath), 'expected', path.basename(inputPath)); var expectedPath = path.join(
var expectedId = _assetIdForPath(expectedPath); path.dirname(inputPath), 'expected', path.basename(inputPath));
var expectedId = _assetIdForPath(expectedPath);
var output = await rewriteDeferredLibraries(reader, inputId); var output = await rewriteDeferredLibraries(reader, inputId);
var input = await reader.readAsString(expectedId); var input = await reader.readAsString(expectedId);
if (input == null) { if (input == null) {
// Null input signals no output. Ensure that is true. // Null input signals no output. Ensure that is true.
expect(output).toBeNull(); expect(output).toBeNull();
} else { } else {
expect(formatter.format(output)).toEqual(formatter.format(input)); expect(formatter.format(output)).toEqual(formatter.format(input));
} }
});
}); });
} }

View File

@ -1,7 +1,10 @@
library angular2.test.transform.directive_metadata_linker.all_tests; library angular2.test.transform.directive_metadata_linker.all_tests;
import 'dart:async';
import 'dart:convert'; import 'dart:convert';
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/model/import_export_model.pb.dart'; import 'package:angular2/src/transform/common/model/import_export_model.pb.dart';
import 'package:angular2/src/transform/directive_metadata_linker/ng_meta_linker.dart'; import 'package:angular2/src/transform/directive_metadata_linker/ng_meta_linker.dart';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
@ -10,6 +13,7 @@ import 'package:guinness/guinness.dart';
import '../common/ng_meta_helper.dart'; import '../common/ng_meta_helper.dart';
import '../common/read_file.dart'; import '../common/read_file.dart';
import '../common/recording_logger.dart';
var formatter = new DartFormatter(); var formatter = new DartFormatter();
@ -71,7 +75,7 @@ void allTests() {
fooNgMeta.ngDeps.exports.add(new ExportModel()..uri = 'bar.dart'); fooNgMeta.ngDeps.exports.add(new ExportModel()..uri = 'bar.dart');
updateReader(); updateReader();
var extracted = await linkDirectiveMetadata(reader, fooAssetId); var extracted = await _testLink(reader, fooAssetId);
expect(extracted.types).toContain('FooComponent'); expect(extracted.types).toContain('FooComponent');
expect(extracted.types).toContain('BarComponent'); expect(extracted.types).toContain('BarComponent');
@ -85,7 +89,7 @@ void allTests() {
barNgMeta.ngDeps.exports.add(new ExportModel()..uri = 'baz.dart'); barNgMeta.ngDeps.exports.add(new ExportModel()..uri = 'baz.dart');
updateReader(); updateReader();
var extracted = await linkDirectiveMetadata(reader, fooAssetId); var extracted = await _testLink(reader, fooAssetId);
expect(extracted.types).toContain('FooComponent'); expect(extracted.types).toContain('FooComponent');
expect(extracted.types).toContain('BarComponent'); expect(extracted.types).toContain('BarComponent');
expect(extracted.types).toContain('BazComponent'); expect(extracted.types).toContain('BazComponent');
@ -101,7 +105,7 @@ void allTests() {
bazNgMeta.ngDeps.exports.add(new ExportModel()..uri = 'foo.dart'); bazNgMeta.ngDeps.exports.add(new ExportModel()..uri = 'foo.dart');
updateReader(); updateReader();
var extracted = await linkDirectiveMetadata(reader, bazAssetId); var extracted = await _testLink(reader, bazAssetId);
expect(extracted.types).toContain('FooComponent'); expect(extracted.types).toContain('FooComponent');
expect(extracted.types).toContain('BarComponent'); expect(extracted.types).toContain('BarComponent');
expect(extracted.types).toContain('BazComponent'); expect(extracted.types).toContain('BazComponent');
@ -116,7 +120,7 @@ void allTests() {
reader.addAsset(new AssetId('bar', 'lib/bar.ng_meta.json'), reader.addAsset(new AssetId('bar', 'lib/bar.ng_meta.json'),
JSON.encode(barNgMeta.toJson())); JSON.encode(barNgMeta.toJson()));
var extracted = await linkDirectiveMetadata(reader, fooAssetId); var extracted = await _testLink(reader, fooAssetId);
expect(extracted.types).toContain('FooComponent'); expect(extracted.types).toContain('FooComponent');
expect(extracted.types).toContain('BarComponent'); expect(extracted.types).toContain('BarComponent');
@ -136,7 +140,7 @@ void allTests() {
barNgMeta.ngDeps.libraryUri = 'test.bar'; barNgMeta.ngDeps.libraryUri = 'test.bar';
updateReader(); updateReader();
var linked = (await linkDirectiveMetadata(reader, fooAssetId)).ngDeps; var linked = (await _testLink(reader, fooAssetId)).ngDeps;
expect(linked).toBeNotNull(); expect(linked).toBeNotNull();
var linkedImport = var linkedImport =
linked.imports.firstWhere((i) => i.uri.endsWith('bar.ng_deps.dart')); linked.imports.firstWhere((i) => i.uri.endsWith('bar.ng_deps.dart'));
@ -152,7 +156,7 @@ void allTests() {
barNgMeta.ngDeps.libraryUri = 'test.bar'; barNgMeta.ngDeps.libraryUri = 'test.bar';
updateReader(); updateReader();
var linked = (await linkDirectiveMetadata(reader, fooAssetId)).ngDeps; var linked = (await _testLink(reader, fooAssetId)).ngDeps;
expect(linked).toBeNotNull(); expect(linked).toBeNotNull();
var linkedImport = var linkedImport =
linked.imports.firstWhere((i) => i.uri.endsWith('bar.ng_deps.dart')); linked.imports.firstWhere((i) => i.uri.endsWith('bar.ng_deps.dart'));
@ -162,3 +166,8 @@ void allTests() {
}); });
}); });
} }
Future<NgMeta> _testLink(AssetReader reader, AssetId assetId) {
return log.setZoned(
new RecordingLogger(), () => linkDirectiveMetadata(reader, assetId));
}

View File

@ -1,9 +1,7 @@
library angular2.test.transform.directive_processor.all_tests; library angular2.test.transform.directive_processor.all_tests;
import 'dart:async'; import 'dart:async';
import 'dart:convert';
import 'package:barback/barback.dart';
import 'package:angular2/src/core/change_detection/change_detection.dart'; import 'package:angular2/src/core/change_detection/change_detection.dart';
import 'package:angular2/src/core/linker/interfaces.dart' show LifecycleHooks; import 'package:angular2/src/core/linker/interfaces.dart' show LifecycleHooks;
import 'package:angular2/src/core/dom/html_adapter.dart'; import 'package:angular2/src/core/dom/html_adapter.dart';
@ -13,13 +11,13 @@ import 'package:angular2/src/transform/common/code/ng_deps_code.dart';
import 'package:angular2/src/transform/common/asset_reader.dart'; 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/logging.dart' as log;
import 'package:angular2/src/transform/common/model/reflection_info_model.pb.dart'; import 'package:angular2/src/transform/common/model/reflection_info_model.pb.dart';
import 'package:angular2/src/transform/common/model/ng_deps_model.pb.dart';
import 'package:angular2/src/transform/common/ng_meta.dart'; import 'package:angular2/src/transform/common/ng_meta.dart';
import 'package:barback/barback.dart';
import 'package:code_transformers/messages/build_logger.dart'; import 'package:code_transformers/messages/build_logger.dart';
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'package:guinness/guinness.dart'; import 'package:guinness/guinness.dart';
import 'package:source_span/source_span.dart';
import '../common/read_file.dart'; import '../common/read_file.dart';
import '../common/recording_logger.dart';
var formatter = new DartFormatter(); var formatter = new DartFormatter();
@ -442,37 +440,9 @@ Future<NgMeta> _testCreateModel(String inputPath,
} }
var annotationMatcher = new AnnotationMatcher()..addAll(customDescriptors); var annotationMatcher = new AnnotationMatcher()..addAll(customDescriptors);
return createNgDeps(reader, inputId, annotationMatcher); return createNgMeta(reader, inputId, annotationMatcher);
}); });
} }
AssetId _assetIdForPath(String path) => AssetId _assetIdForPath(String path) =>
new AssetId('angular2', 'test/transform/directive_processor/$path'); new AssetId('angular2', 'test/transform/directive_processor/$path');
class RecordingLogger implements BuildLogger {
@override
final String detailsUri = '';
@override
final bool convertErrorsToWarnings = false;
bool hasErrors = false;
List<String> logs = [];
void _record(prefix, msg) => logs.add('$prefix: $msg');
void info(msg, {AssetId asset, SourceSpan span}) => _record('INFO', msg);
void fine(msg, {AssetId asset, SourceSpan span}) => _record('FINE', msg);
void warning(msg, {AssetId asset, SourceSpan span}) => _record('WARN', msg);
void error(msg, {AssetId asset, SourceSpan span}) {
hasErrors = true;
_record('ERROR', msg);
}
Future writeOutput() => throw new UnimplementedError();
Future addLogFilesFromAsset(AssetId id, [int nextNumber = 1]) =>
throw new UnimplementedError();
}

View File

@ -1,6 +1,9 @@
library angular2.test.transform.inliner_for_test.all_tests; library angular2.test.transform.inliner_for_test.all_tests;
import 'dart:async';
import 'package:angular2/src/transform/common/asset_reader.dart'; import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart' as log;
import 'package:angular2/src/transform/inliner_for_test.dart'; import 'package:angular2/src/transform/inliner_for_test.dart';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
import 'package:code_transformers/tests.dart'; import 'package:code_transformers/tests.dart';
@ -8,6 +11,7 @@ import 'package:guinness/guinness.dart';
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import '../common/read_file.dart'; import '../common/read_file.dart';
import '../common/recording_logger.dart';
main() { main() {
allTests(); allTests();
@ -23,7 +27,7 @@ void allTests() {
}); });
it('should inline `templateUrl` values', () async { it('should inline `templateUrl` values', () async {
var output = await inline( var output = await _testInline(
absoluteReader, _assetId('url_expression_files/hello.dart')); absoluteReader, _assetId('url_expression_files/hello.dart'));
expect(output).toBeNotNull(); expect(output).toBeNotNull();
expect(() => formatter.format(output)).not.toThrow(); expect(() => formatter.format(output)).not.toThrow();
@ -42,7 +46,7 @@ void allTests() {
readFile( readFile(
'inliner_for_test/absolute_url_expression_files/template.css')); 'inliner_for_test/absolute_url_expression_files/template.css'));
var output = await inline( var output = await _testInline(
absoluteReader, _assetId('absolute_url_expression_files/hello.dart')); absoluteReader, _assetId('absolute_url_expression_files/hello.dart'));
expect(output).toBeNotNull(); expect(output).toBeNotNull();
@ -58,7 +62,7 @@ void allTests() {
absoluteReader absoluteReader
..addAsset(new AssetId('other_package', 'lib/template.html'), '') ..addAsset(new AssetId('other_package', 'lib/template.html'), '')
..addAsset(new AssetId('other_package', 'lib/template.css'), ''); ..addAsset(new AssetId('other_package', 'lib/template.css'), '');
var output = await inline( var output = await _testInline(
absoluteReader, _assetId('multiple_style_urls_files/hello.dart')); absoluteReader, _assetId('multiple_style_urls_files/hello.dart'));
expect(output).toBeNotNull(); expect(output).toBeNotNull();
@ -70,7 +74,7 @@ void allTests() {
}); });
it('should inline `templateUrl`s expressed as adjacent strings.', () async { it('should inline `templateUrl`s expressed as adjacent strings.', () async {
var output = await inline( var output = await _testInline(
absoluteReader, _assetId('split_url_expression_files/hello.dart')); absoluteReader, _assetId('split_url_expression_files/hello.dart'));
expect(output).toBeNotNull(); expect(output).toBeNotNull();
@ -83,6 +87,10 @@ void allTests() {
_runMultiStylesEndToEndTest(); _runMultiStylesEndToEndTest();
} }
Future<String> _testInline(AssetReader reader, AssetId assetId) {
return log.setZoned(new RecordingLogger(), () => inline(reader, assetId));
}
AssetId _assetId(String path) => new AssetId('a', 'inliner_for_test/$path'); AssetId _assetId(String path) => new AssetId('a', 'inliner_for_test/$path');
void _runAbsoluteUrlEndToEndTest() { void _runAbsoluteUrlEndToEndTest() {

View File

@ -8,6 +8,8 @@ import 'package:angular2/src/transform/stylesheet_compiler/transformer.dart';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
import 'package:guinness/guinness.dart'; import 'package:guinness/guinness.dart';
import '../common/recording_logger.dart';
const SIMPLE_CSS = ''' const SIMPLE_CSS = '''
.foo { .foo {
width: 10px; width: 10px;
@ -64,6 +66,9 @@ allTests() {
class FakeTransform implements Transform { class FakeTransform implements Transform {
final outputs = <Asset>[]; final outputs = <Asset>[];
Asset primaryInput; Asset primaryInput;
final _logger = new RecordingLogger();
get logger => _logger;
addOutput(Asset output) { addOutput(Asset output) {
this.outputs.add(output); this.outputs.add(output);

View File

@ -6,7 +6,7 @@ import 'dart:convert';
import 'package:barback/barback.dart'; import 'package:barback/barback.dart';
import 'package:angular2/src/core/dom/html_adapter.dart'; import 'package:angular2/src/core/dom/html_adapter.dart';
import 'package:angular2/src/transform/common/asset_reader.dart'; import 'package:angular2/src/transform/common/asset_reader.dart';
import 'package:angular2/src/transform/common/logging.dart'; import 'package:angular2/src/transform/common/logging.dart' as log;
import 'package:angular2/src/transform/template_compiler/generator.dart'; import 'package:angular2/src/transform/template_compiler/generator.dart';
import 'package:dart_style/dart_style.dart'; import 'package:dart_style/dart_style.dart';
import 'package:path/path.dart' as path; import 'package:path/path.dart' as path;
@ -14,6 +14,7 @@ import 'package:guinness/guinness.dart';
import '../common/compile_directive_metadata/ng_for.ng_meta.dart' as ngMeta; import '../common/compile_directive_metadata/ng_for.ng_meta.dart' as ngMeta;
import '../common/read_file.dart'; import '../common/read_file.dart';
import '../common/recording_logger.dart';
var formatter = new DartFormatter(); var formatter = new DartFormatter();
AssetReader reader; AssetReader reader;
@ -37,7 +38,10 @@ void allTests() {
} }
void changeDetectorTests() { void changeDetectorTests() {
Future<Outputs> process(AssetId assetId) => processTemplates(reader, assetId); Future<Outputs> process(AssetId assetId) {
return log.setZoned(
new RecordingLogger(), () => processTemplates(reader, assetId));
}
// TODO(tbosch): This is just a temporary test that makes sure that the dart server and // TODO(tbosch): This is just a temporary test that makes sure that the dart server and
// dart browser is in sync. Change this to "not contains notifyBinding" // dart browser is in sync. Change this to "not contains notifyBinding"
@ -68,8 +72,12 @@ void changeDetectorTests() {
} }
void noChangeDetectorTests() { void noChangeDetectorTests() {
Future<String> process(AssetId assetId) => Future<String> process(AssetId assetId) {
processTemplates(reader, assetId).then((outputs) => outputs.ngDepsCode); return log.setZoned(
new RecordingLogger(),
() => processTemplates(reader, assetId)
.then((outputs) => outputs.ngDepsCode));
}
it('should parse simple expressions in inline templates.', () async { it('should parse simple expressions in inline templates.', () async {
var inputPath = var inputPath =