| OLD | NEW |
| (Empty) |
| 1 library angular.transformer; | |
| 2 | |
| 3 import 'dart:async'; | |
| 4 import 'dart:io'; | |
| 5 import 'package:angular/tools/transformer/expression_generator.dart'; | |
| 6 import 'package:angular/tools/transformer/metadata_generator.dart'; | |
| 7 import 'package:angular/tools/transformer/static_angular_generator.dart'; | |
| 8 import 'package:angular/tools/transformer/html_dart_references_generator.dart'; | |
| 9 import 'package:angular/tools/transformer/options.dart'; | |
| 10 import 'package:barback/barback.dart'; | |
| 11 import 'package:code_transformers/resolver.dart'; | |
| 12 import 'package:di/transformer/injector_generator.dart' as di; | |
| 13 import 'package:di/transformer/options.dart' as di; | |
| 14 import 'package:path/path.dart' as path; | |
| 15 | |
| 16 | |
| 17 /** | |
| 18 * The Angular transformer, which internally runs several phases that will: | |
| 19 * | |
| 20 * * Extract all expressions for evaluation at runtime without using Mirrors. | |
| 21 * * Extract all classes being dependency injected into a static injector. | |
| 22 * * Extract all metadata for cached reflection. | |
| 23 */ | |
| 24 class AngularTransformerGroup implements TransformerGroup { | |
| 25 final Iterable<Iterable> phases; | |
| 26 | |
| 27 AngularTransformerGroup(TransformOptions options) | |
| 28 : phases = _createPhases(options); | |
| 29 | |
| 30 AngularTransformerGroup.asPlugin(BarbackSettings settings) | |
| 31 : this(_parseSettings(settings.configuration)); | |
| 32 } | |
| 33 | |
| 34 TransformOptions _parseSettings(Map args) { | |
| 35 // Default angular annotations for injectable types | |
| 36 var annotations = [ | |
| 37 'angular.core.annotation_src.Injectable', | |
| 38 'angular.core.annotation_src.Decorator', | |
| 39 'angular.core.annotation_src.Controller', | |
| 40 'angular.core.annotation_src.Component', | |
| 41 'angular.core.annotation_src.Formatter']; | |
| 42 annotations.addAll(_readStringListValue(args, 'injectable_annotations')); | |
| 43 | |
| 44 // List of types which are otherwise not indicated as being injectable. | |
| 45 var injectedTypes = [ | |
| 46 'perf_api.Profiler', | |
| 47 ]; | |
| 48 injectedTypes.addAll(_readStringListValue(args, 'injected_types')); | |
| 49 | |
| 50 var sdkDir = _readStringValue(args, 'dart_sdk', required: false); | |
| 51 if (sdkDir == null) { | |
| 52 // Assume the Pub executable is always coming from the SDK. | |
| 53 sdkDir = path.dirname(path.dirname(Platform.executable)); | |
| 54 } | |
| 55 | |
| 56 var diOptions = new di.TransformOptions( | |
| 57 injectableAnnotations: annotations, | |
| 58 injectedTypes: injectedTypes, | |
| 59 sdkDirectory: sdkDir); | |
| 60 | |
| 61 return new TransformOptions( | |
| 62 htmlFiles: _readStringListValue(args, 'html_files'), | |
| 63 sdkDirectory: sdkDir, | |
| 64 templateUriRewrites: _readStringMapValue(args, 'template_uri_rewrites'), | |
| 65 diOptions: diOptions); | |
| 66 } | |
| 67 | |
| 68 _readStringValue(Map args, String name, {bool required: true}) { | |
| 69 var value = args[name]; | |
| 70 if (value == null) { | |
| 71 if (required) { | |
| 72 print('Angular transformer parameter "$name" ' | |
| 73 'has no value in pubspec.yaml.'); | |
| 74 } | |
| 75 return null; | |
| 76 } | |
| 77 if (value is! String) { | |
| 78 print('Angular transformer parameter "$name" value ' | |
| 79 'is not a string in pubspec.yaml.'); | |
| 80 return null; | |
| 81 } | |
| 82 return value; | |
| 83 } | |
| 84 | |
| 85 _readStringListValue(Map args, String name) { | |
| 86 var value = args[name]; | |
| 87 if (value == null) return []; | |
| 88 var results = []; | |
| 89 bool error; | |
| 90 if (value is List) { | |
| 91 results = value; | |
| 92 error = value.any((e) => e is! String); | |
| 93 } else if (value is String) { | |
| 94 results = [value]; | |
| 95 error = false; | |
| 96 } else { | |
| 97 error = true; | |
| 98 } | |
| 99 if (error) { | |
| 100 print('Angular transformer parameter "$name" ' | |
| 101 'has an invalid value in pubspec.yaml.'); | |
| 102 } | |
| 103 return results; | |
| 104 } | |
| 105 | |
| 106 Map<String, String> _readStringMapValue(Map args, String name) { | |
| 107 var value = args[name]; | |
| 108 if (value == null) return {}; | |
| 109 if (value is! Map) { | |
| 110 print('Angular transformer parameter "$name" ' | |
| 111 'is expected to be a map parameter.'); | |
| 112 return {}; | |
| 113 } | |
| 114 if (value.keys.any((e) => e is! String) || | |
| 115 value.values.any((e) => e is! String)) { | |
| 116 print('Angular transformer parameter "$name" ' | |
| 117 'is expected to be a map of strings.'); | |
| 118 return {}; | |
| 119 } | |
| 120 return value; | |
| 121 } | |
| 122 | |
| 123 List<List<Transformer>> _createPhases(TransformOptions options) { | |
| 124 var resolvers = new Resolvers(options.sdkDirectory); | |
| 125 return [ | |
| 126 [new HtmlDartReferencesGenerator(options)], | |
| 127 [new _SerialTransformer([ | |
| 128 new ExpressionGenerator(options, resolvers), | |
| 129 new di.InjectorGenerator(options.diOptions, resolvers), | |
| 130 new MetadataGenerator(options, resolvers), | |
| 131 new StaticAngularGenerator(options, resolvers) | |
| 132 ])] | |
| 133 ]; | |
| 134 } | |
| 135 | |
| 136 /// Helper which runs a group of transformers serially and ensures that | |
| 137 /// transformers with shared data are always applied in a specific order. | |
| 138 /// | |
| 139 /// Transformers which communicate only via assets do not need this additional | |
| 140 /// synchronization. | |
| 141 /// | |
| 142 /// This is used by Angular to ensure ordering of references to the cached | |
| 143 /// resolvers. | |
| 144 class _SerialTransformer extends Transformer { | |
| 145 final Iterable<Transformer> _transformers; | |
| 146 _SerialTransformer(this._transformers); | |
| 147 | |
| 148 Future<bool> isPrimary(Asset input) => | |
| 149 Future.wait(_transformers.map((t) => t.isPrimary(input))) | |
| 150 .then((l) => l.any((result) => result)); | |
| 151 | |
| 152 Future apply(Transform transform) { | |
| 153 return Future.forEach(_transformers, (t) { | |
| 154 return new Future.value(t.isPrimary(transform.primaryInput)) | |
| 155 .then((isPrimary) { | |
| 156 if (isPrimary) return t.apply(transform); | |
| 157 }); | |
| 158 }); | |
| 159 } | |
| 160 } | |
| OLD | NEW |