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

@ -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

@ -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,10 +120,8 @@ 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();
@ -136,7 +134,8 @@ void _testNgDeps(String name, String inputPath,
path.basename(inputPath).replaceFirst('.dart', '.ng_deps.dart'));
var expectedId = _assetIdForPath(expectedPath);
var annotationMatcher = new AnnotationMatcher()..addAll(customDescriptors);
var annotationMatcher = new AnnotationMatcher()
..addAll(customDescriptors);
var output = await createNgDeps(reader, inputId, annotationMatcher,
inlineViews: inlineViews);
if (output == null) {
@ -145,9 +144,10 @@ void _testNgDeps(String name, String inputPath,
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();
@ -53,8 +52,7 @@ void noChangeDetectorTests() {
});
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');
var output = await process(new AssetId('a', inputPath));
@ -62,8 +60,7 @@ void noChangeDetectorTests() {
});
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');
var output = await process(new AssetId('a', inputPath));
@ -87,8 +84,7 @@ void noChangeDetectorTests() {
});
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');