Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 library angular2.transformer; | |
| 5 | |
| 6 import 'dart:async'; | |
| 7 import 'package:barback/barback.dart'; | |
| 8 import 'package:code_transformers/resolver.dart'; | |
| 9 | |
| 10 import 'annotation_processor.dart'; | |
| 11 import 'codegen.dart' as codegen; | |
| 12 import 'html_transform.dart'; | |
| 13 import 'options.dart'; | |
| 14 import 'resolvers.dart'; | |
| 15 import 'traversal.dart'; | |
| 16 | |
| 17 export 'options.dart'; | |
| 18 | |
| 19 /// Removes the mirror-based initialization logic and replaces it with static | |
| 20 /// logic. | |
| 21 class AngularTransformer extends Transformer { | |
| 22 final Resolvers _resolvers; | |
| 23 final TransformerOptions options; | |
| 24 | |
| 25 AngularTransformer(this.options) : _resolvers = createResolvers(); | |
| 26 | |
| 27 static const _entryPointParam = 'entry_point'; | |
|
jakemac
2015/02/17 23:46:46
not sure that these are necessary, they only appea
tjblasi
2015/02/18 21:18:25
They're probably going to change in the future --
| |
| 28 static const _newEntryPointParam = 'new_entry_point'; | |
| 29 static const _htmlEntryPointParam = 'html_entry_point'; | |
| 30 | |
| 31 factory AngularTransformer.asPlugin(BarbackSettings settings) { | |
| 32 var entryPoint = settings.configuration[_entryPointParam]; | |
| 33 var newEntryPoint = settings.configuration[_newEntryPointParam]; | |
| 34 if (newEntryPoint == null) { | |
| 35 newEntryPoint = entryPoint.replaceFirst('.dart', '.bootstrap.dart'); | |
| 36 } | |
| 37 var htmlEntryPoint = settings.configuration[_htmlEntryPointParam]; | |
| 38 return new AngularTransformer( | |
| 39 new TransformerOptions(entryPoint, newEntryPoint, htmlEntryPoint)); | |
| 40 } | |
| 41 | |
| 42 bool isPrimary(AssetId id) => | |
| 43 options.entryPoint == id.path || options.htmlEntryPoint == id.path; | |
| 44 | |
| 45 Future apply(Transform transform) { | |
| 46 if (transform.primaryInput.id.path == options.entryPoint) { | |
| 47 return _buildBootstrapFile(transform); | |
| 48 } else if (transform.primaryInput.id.path == options.htmlEntryPoint) { | |
| 49 return transformHtmlEntryPoint(options, transform); | |
| 50 } | |
| 51 return null; | |
| 52 } | |
| 53 | |
| 54 Future _buildBootstrapFile(Transform transform) { | |
| 55 var newEntryPointId = | |
| 56 new AssetId(transform.primaryInput.id.package, options.newEntryPoint); | |
| 57 return transform.hasInput(newEntryPointId).then((exists) { | |
| 58 if (exists) { | |
| 59 transform.logger | |
| 60 .error('New entry point file $newEntryPointId already exists.'); | |
| 61 } else { | |
| 62 return _resolvers.get(transform).then((resolver) { | |
| 63 new _BootstrapFileBuilder(resolver, transform, | |
| 64 transform.primaryInput.id, newEntryPointId).run(); | |
| 65 resolver.release(); | |
| 66 }); | |
| 67 } | |
| 68 }); | |
| 69 } | |
| 70 } | |
| 71 | |
| 72 class _BootstrapFileBuilder { | |
| 73 final Resolver _resolver; | |
| 74 final Transform _transform; | |
| 75 final AssetId _entryPoint; | |
| 76 final AssetId _newEntryPoint; | |
| 77 | |
| 78 AnnotationMatcher _directiveInfo; | |
| 79 | |
| 80 _BootstrapFileBuilder(Resolver resolver, Transform transform, | |
| 81 this._entryPoint, this._newEntryPoint) | |
| 82 : _resolver = resolver, | |
| 83 _transform = transform, | |
| 84 _directiveInfo = new AnnotationMatcher(resolver | |
| 85 .getLibrary(new AssetId( | |
| 86 'angular2', 'lib/src/core/annotations/annotations.dart')) | |
| 87 .getType('Directive')); | |
| 88 | |
| 89 /// Adds the new entry point file to the transform. Should only be ran once. | |
| 90 void run() { | |
| 91 var entryLib = _resolver.getLibrary(_entryPoint); | |
| 92 | |
| 93 new ImportTraversal(_directiveInfo).traverse(entryLib); | |
| 94 | |
| 95 var context = new codegen.Context(logger: _transform.logger); | |
| 96 _directiveInfo.initQueue | |
| 97 .forEach((entry) => context.directiveRegistry.register(entry)); | |
| 98 | |
| 99 _transform.addOutput(new Asset.fromString(_newEntryPoint, codegen | |
| 100 .codegenEntryPoint(context, | |
| 101 entryPoint: entryLib, newEntryPoint: _newEntryPoint))); | |
| 102 } | |
| 103 } | |
| OLD | NEW |