fix(transformer): Loggers now are per zone and each transform runs in its own zone

This commit is contained in:
Jacob MacDonald 2015-07-23 09:06:23 -07:00
parent 09226cdd75
commit bd65b63c65
18 changed files with 91 additions and 160 deletions

View File

@ -15,7 +15,7 @@ Future<String> createNgSettersAndGetters(
String code = ngDeps.code;
var setters = _generateSetters(_createPropertiesMap(ngDeps));
var getters = _generateGetters( _createEventPropertiesList(ngDeps));
var getters = _generateGetters(_createEventPropertiesList(ngDeps));
if (setters.isEmpty && getters.isEmpty) return code;
var out = new StringBuffer();
@ -97,7 +97,7 @@ List<String> _generateGetters(List<String> eventProperties) {
/// Collapses all `events` in {@link ngDeps} into a list of corresponding
/// property names.
List<String> _createEventPropertiesList(NgDeps ngDeps) {
List<String> _createEventPropertiesList(NgDeps ngDeps) {
var visitor = new ExtractNamedExpressionVisitor('events');
var propertyNames = [];
ngDeps.registeredTypes.forEach((RegisteredType t) {

View File

@ -26,18 +26,12 @@ class BindGenerator extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var id = transform.primaryInput.id;
var reader = new AssetReader.fromTransform(transform);
var transformedCode = await createNgSettersAndGetters(reader, id);
transform.addOutput(new Asset.fromString(
id, formatter.format(transformedCode, uri: id.path)));
} catch (ex, stackTrace) {
log.logger.error('Creating ng setters/getters failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
}, errorMessage: 'Creating ng setters/getters failed.');
}
}

View File

@ -88,7 +88,7 @@ class AnnotationMatcher extends ClassMatcherBase {
throw 'Unable to locate descriptor for ${annotation.name} in ${assetId}';
}
return implements(descriptor, interfaces,
missingSuperClassWarning: 'Missing `custom_annotation` entry for `${descriptor.superClass}`.');
missingSuperClassWarning: 'Missing `custom_annotation` entry for `${descriptor.superClass}`.');
}
/// Checks if an [Annotation] node implements [Injectable].

View File

@ -5,24 +5,27 @@ import 'package:barback/barback.dart';
import 'package:code_transformers/messages/build_logger.dart';
import 'package:source_span/source_span.dart';
BuildLogger _logger;
typedef _SimpleCallback();
/// Prepares {@link logger} for use throughout the transformer.
void init(Transform t) {
_logger = new BuildLogger(t);
}
// The key used to store the logger on the current zone.
final _key = #loggingZonedLoggerKey;
/// Sets {@link logger} directly. Used for testing - in general use {@link init}.
void setLogger(BuildLogger logger) {
_logger = logger;
}
/// Executes {@link fn} inside a new {@link Zone} with its own logger.
dynamic initZoned(Transform t, _SimpleCallback fn, {String errorMessage: ''}) =>
setZoned(new BuildLogger(t), fn, errorMessage: errorMessage);
/// The logger the transformer should use for messaging.
dynamic setZoned(BuildLogger logger, _SimpleCallback fn,
{String errorMessage: ''}) => runZoned(fn,
zoneValues: {_key: logger}, onError: (e, stackTrace) {
logger.error('$errorMessage\n'
'Exception: $e\n'
'Stack Trace: $stackTrace');
});
/// The logger for the current {@link Zone}.
BuildLogger get logger {
if (_logger == null) {
_logger = new PrintLogger();
}
return _logger;
var current = Zone.current[_key] as BuildLogger;
return current == null ? new PrintLogger() : current;
}
class PrintLogger implements BuildLogger {

View File

@ -24,9 +24,7 @@ class DeferredRewriter extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var asset = transform.primaryInput;
var reader = new AssetReader.fromTransform(transform);
var transformedCode = await rewriteDeferredLibraries(reader, asset.id);
@ -34,11 +32,7 @@ class DeferredRewriter extends Transformer {
transform.addOutput(
new Asset.fromString(transform.primaryInput.id, transformedCode));
}
} catch (ex, stackTrace) {
log.logger.warning('Rewritting deferred libraries failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
}, errorMessage: 'Rewritting deferred libraries failed.');
}
}

View File

@ -22,9 +22,7 @@ class DirectiveLinker extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var reader = new AssetReader.fromTransform(transform);
var assetId = transform.primaryInput.id;
var assetPath = assetId.path;
@ -33,12 +31,7 @@ class DirectiveLinker extends Transformer {
var formattedCode = formatter.format(transformedCode, uri: assetPath);
transform.addOutput(new Asset.fromString(assetId, formattedCode));
}
} catch (ex, stackTrace) {
log.logger.error('Linking ng directives failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
return null;
}, errorMessage: 'Linking ng directives failed.');
}
}
@ -52,18 +45,11 @@ class EmptyNgDepsRemover extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var reader = new AssetReader.fromTransform(transform);
if (!(await isNecessary(reader, transform.primaryInput.id))) {
transform.consumePrimary();
}
} catch (ex, stackTrace) {
log.logger.error('Removing unnecessary ng deps failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
return null;
}, errorMessage: 'Removing unnecessary ng deps failed.');
}
}

View File

@ -25,9 +25,7 @@ class DirectiveMetadataExtractor extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var reader = new AssetReader.fromTransform(transform);
var fromAssetId = transform.primaryInput.id;
@ -40,12 +38,7 @@ class DirectiveMetadataExtractor extends Transformer {
transform.addOutput(new Asset.fromString(
_outputAssetId(fromAssetId), _encoder.convert(jsonMap)));
}
} catch (ex, stackTrace) {
log.logger.error('Extracting ng metadata failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
return null;
}, errorMessage: 'Extracting ng metadata failed.');
}
}

View File

@ -29,9 +29,7 @@ class DirectiveProcessor extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var asset = transform.primaryInput;
var reader = new AssetReader.fromTransform(transform);
var ngDepsSrc = await createNgDeps(
@ -46,10 +44,6 @@ class DirectiveProcessor extends Transformer {
}
transform.addOutput(new Asset.fromString(ngDepsAssetId, ngDepsSrc));
}
} catch (ex, stackTrace) {
log.logger.warning('Processing ng directives failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
}, errorMessage: 'Processing ng directives failed.');
}
}

View File

@ -45,8 +45,7 @@ class Rewriter {
///
/// This breaks our dependency on dart:mirrors, which enables smaller code
/// size and better performance.
class _RewriterVisitor extends Object
with RecursiveAstVisitor<Object> {
class _RewriterVisitor extends Object with RecursiveAstVisitor<Object> {
final Rewriter _rewriter;
final buf = new StringBuffer();
final reflectionCapabilityAssignments = [];

View File

@ -30,9 +30,7 @@ class ReflectionRemover extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
var newEntryPoints = options.entryPoints.map((entryPoint) {
return new AssetId(transform.primaryInput.id.package, entryPoint)
.changeExtension(DEPS_EXTENSION);
@ -54,10 +52,6 @@ class ReflectionRemover extends Transformer {
mirrorMode: mirrorMode, writeStaticInit: writeStaticInit);
transform.addOutput(
new Asset.fromString(transform.primaryInput.id, transformedCode));
} catch (ex, stackTrace) {
log.logger.error('Removing reflection failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
}, errorMessage: 'Removing reflection failed.');
}
}

View File

@ -28,19 +28,13 @@ class TemplateCompiler extends Transformer {
@override
Future apply(Transform transform) async {
log.init(transform);
try {
await log.initZoned(transform, () async {
Html5LibDomAdapter.makeCurrent();
var id = transform.primaryInput.id;
var reader = new AssetReader.fromTransform(transform);
var transformedCode = formatter.format(await processTemplates(reader, id,
generateChangeDetectors: options.generateChangeDetectors));
transform.addOutput(new Asset.fromString(id, transformedCode));
} catch (ex, stackTrace) {
log.logger.error('Parsing ng templates failed.\n'
'Exception: $ex\n'
'Stack Trace: $stackTrace');
}
}, errorMessage: 'Parsing ng templates failed.');
}
}

View File

@ -20,7 +20,6 @@ void allTests() {
TestAssetReader reader = null;
beforeEach(() {
setLogger(new PrintLogger());
reader = new TestAssetReader();
});

View File

@ -2,12 +2,7 @@ library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show
Component,
Directive,
View,
NgElement,
LifecycleEvent;
show Component, Directive, View, NgElement, LifecycleEvent;
var _visited = false;
void initReflector(reflector) {

View File

@ -2,12 +2,7 @@ library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show
Component,
Directive,
View,
NgElement,
LifecycleEvent;
show Component, Directive, View, NgElement, LifecycleEvent;
var _visited = false;
void initReflector(reflector) {

View File

@ -2,12 +2,7 @@ library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show
Component,
Directive,
View,
NgElement,
LifecycleEvent;
show Component, Directive, View, NgElement, LifecycleEvent;
var _visited = false;
void initReflector(reflector) {

View File

@ -2,12 +2,7 @@ library examples.hello_world.index_common_dart.ng_deps.dart;
import 'hello.dart';
import 'package:angular2/angular2.dart'
show
Component,
Directive,
View,
NgElement,
LifecycleEvent;
show Component, Directive, View, NgElement, LifecycleEvent;
var _visited = false;
void initReflector(reflector) {
@ -19,8 +14,13 @@ void initReflector(reflector) {
'parameters': const [const []],
'annotations': const [
const Component(
lifecycle: [LifecycleEvent.onChange, LifecycleEvent.onDestroy, LifecycleEvent.onInit,
LifecycleEvent.onCheck, LifecycleEvent.onAllChangesDone])
lifecycle: [
LifecycleEvent.onChange,
LifecycleEvent.onDestroy,
LifecycleEvent.onInit,
LifecycleEvent.onCheck,
LifecycleEvent.onAllChangesDone
])
]
});
}

View File

@ -120,34 +120,34 @@ void _testNgDeps(String name, String inputPath,
bool isolate: false}) {
var testFn = isolate ? iit : it;
testFn(name, () async {
if (expectedLogs != null) {
log.setLogger(new RecordingLogger());
}
var logger = new RecordingLogger();
await log.setZoned(logger, () async {
var inputId = _assetIdForPath(inputPath);
if (reader == null) {
reader = new TestAssetReader();
}
if (assetId != null) {
reader.addAsset(assetId, await reader.readAsString(inputId));
inputId = assetId;
}
var expectedPath = path.join(path.dirname(inputPath), 'expected',
path.basename(inputPath).replaceFirst('.dart', '.ng_deps.dart'));
var expectedId = _assetIdForPath(expectedPath);
var inputId = _assetIdForPath(inputPath);
if (reader == null) {
reader = new TestAssetReader();
}
if (assetId != null) {
reader.addAsset(assetId, await reader.readAsString(inputId));
inputId = assetId;
}
var expectedPath = path.join(path.dirname(inputPath), 'expected',
path.basename(inputPath).replaceFirst('.dart', '.ng_deps.dart'));
var expectedId = _assetIdForPath(expectedPath);
var annotationMatcher = new AnnotationMatcher()..addAll(customDescriptors);
var output = await createNgDeps(reader, inputId, annotationMatcher,
inlineViews: inlineViews);
if (output == null) {
expect(await reader.hasInput(expectedId)).toBeFalse();
} else {
var input = await reader.readAsString(expectedId);
expect(formatter.format(output)).toEqual(formatter.format(input));
}
var annotationMatcher = new AnnotationMatcher()
..addAll(customDescriptors);
var output = await createNgDeps(reader, inputId, annotationMatcher,
inlineViews: inlineViews);
if (output == null) {
expect(await reader.hasInput(expectedId)).toBeFalse();
} else {
var input = await reader.readAsString(expectedId);
expect(formatter.format(output)).toEqual(formatter.format(input));
}
});
if (expectedLogs != null) {
expect((log.logger as RecordingLogger).logs, expectedLogs);
expect(logger.logs, expectedLogs);
}
});
}

View File

@ -18,7 +18,6 @@ main() => allTests();
void allTests() {
Html5LibDomAdapter.makeCurrent();
beforeEach(() => setLogger(new PrintLogger()));
describe('registrations', () {
noChangeDetectorTests();
@ -34,7 +33,7 @@ void changeDetectorTests() {
// when https://github.com/angular/angular/issues/3019 is solved.
it('shouldn always notifyBinding for template variables', () async {
var inputPath = 'template_compiler/ng_for_files/hello.ng_deps.dart';
var output = await(process(new AssetId('a', inputPath)));
var output = await (process(new AssetId('a', inputPath)));
expect(output).toContain('notifyOnBinding');
});
}
@ -45,27 +44,25 @@ void noChangeDetectorTests() {
it('should parse simple expressions in inline templates.', () async {
var inputPath =
'template_compiler/inline_expression_files/hello.ng_deps.dart';
'template_compiler/inline_expression_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/inline_expression_files/expected/hello.ng_deps.dart');
'template_compiler/inline_expression_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
});
it('should parse simple methods in inline templates.', () async {
var inputPath =
'template_compiler/inline_method_files/hello.ng_deps.dart';
var inputPath = 'template_compiler/inline_method_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/inline_method_files/expected/hello.ng_deps.dart');
'template_compiler/inline_method_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
});
it('should parse simple expressions in linked templates.', () async {
var inputPath =
'template_compiler/url_expression_files/hello.ng_deps.dart';
var inputPath = 'template_compiler/url_expression_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/url_expression_files/expected/hello.ng_deps.dart');
'template_compiler/url_expression_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
});
@ -73,7 +70,7 @@ void noChangeDetectorTests() {
it('should parse simple methods in linked templates.', () async {
var inputPath = 'template_compiler/url_method_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/url_method_files/expected/hello.ng_deps.dart');
'template_compiler/url_method_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
});
@ -81,33 +78,32 @@ void noChangeDetectorTests() {
it('should not generated duplicate getters/setters', () async {
var inputPath = 'template_compiler/duplicate_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/duplicate_files/expected/hello.ng_deps.dart');
'template_compiler/duplicate_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
});
it('should parse `View` directives with a single dependency.', () async {
var inputPath =
'template_compiler/one_directive_files/hello.ng_deps.dart';
var inputPath = 'template_compiler/one_directive_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/one_directive_files/expected/hello.ng_deps.dart');
'template_compiler/one_directive_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
});
it('should parse `View` directives with a single prefixed dependency.',
() async {
() async {
var inputPath = 'template_compiler/with_prefix_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/with_prefix_files/expected/hello.ng_deps.dart');
'template_compiler/with_prefix_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
inputPath = 'template_compiler/with_prefix_files/goodbye.ng_deps.dart';
expected = readFile(
'template_compiler/with_prefix_files/expected/goodbye.ng_deps.dart');
'template_compiler/with_prefix_files/expected/goodbye.ng_deps.dart');
output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
@ -115,9 +111,9 @@ void noChangeDetectorTests() {
it('should parse angular directives with a prefix', () async {
var inputPath =
'template_compiler/with_prefix_files/ng2_prefix.ng_deps.dart';
'template_compiler/with_prefix_files/ng2_prefix.ng_deps.dart';
var expected = readFile(
'template_compiler/with_prefix_files/expected/ng2_prefix.ng_deps.dart');
'template_compiler/with_prefix_files/expected/ng2_prefix.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
@ -125,9 +121,9 @@ void noChangeDetectorTests() {
it('should create the same output for multiple calls.', () async {
var inputPath =
'template_compiler/inline_expression_files/hello.ng_deps.dart';
'template_compiler/inline_expression_files/hello.ng_deps.dart';
var expected = readFile(
'template_compiler/inline_expression_files/expected/hello.ng_deps.dart');
'template_compiler/inline_expression_files/expected/hello.ng_deps.dart');
var output = await process(new AssetId('a', inputPath));
_formatThenExpectEquals(output, expected);
output = await process(new AssetId('a', inputPath));