OLD | NEW |
(Empty) | |
| 1 /** |
| 2 * Transformer which generates type factories for static injection. |
| 3 * |
| 4 * Types which are considered injectable can be annotated in the following ways: |
| 5 * |
| 6 * * Use the @inject annotation on a class from `package:inject/inject.dart` |
| 7 * @inject |
| 8 * class Engine {} |
| 9 * |
| 10 * or on the constructor: |
| 11 * |
| 12 * class Engine { |
| 13 * @inject |
| 14 * Engine(); |
| 15 * } |
| 16 * |
| 17 * * Define a custom annotation in pubspec.yaml |
| 18 * |
| 19 * transformers: |
| 20 * - di: |
| 21 * injectable_annotations: |
| 22 * - library_name.ClassName |
| 23 * - library_name.constInstance |
| 24 * |
| 25 * Annotate constructors or classes with those annotations |
| 26 * |
| 27 * @ClassName() |
| 28 * class Engine {} |
| 29 * |
| 30 * class Engine { |
| 31 * @constInstance |
| 32 * Engine(); |
| 33 * } |
| 34 * |
| 35 * * Use package:di's Injectables |
| 36 * |
| 37 * @Injectables(const [Engine]) |
| 38 * library my_lib; |
| 39 * |
| 40 * import 'package:di/annotations.dart'; |
| 41 * |
| 42 * class Engine {} |
| 43 * |
| 44 * * Specify injected types via pubspec.yaml |
| 45 * |
| 46 * transformers: |
| 47 * - di: |
| 48 * injected_types: |
| 49 * - library_name.Engine |
| 50 */ |
| 51 library di.transformer; |
| 52 |
| 53 import 'dart:io'; |
| 54 import 'package:barback/barback.dart'; |
| 55 import 'package:code_transformers/resolver.dart'; |
| 56 import 'package:di/transformer/injector_generator.dart'; |
| 57 import 'package:di/transformer/options.dart'; |
| 58 import 'package:path/path.dart' as path; |
| 59 |
| 60 |
| 61 /** |
| 62 * The transformer, which will extract all classes being dependency injected |
| 63 * into a static injector. |
| 64 */ |
| 65 class DependencyInjectorTransformerGroup implements TransformerGroup { |
| 66 final Iterable<Iterable> phases; |
| 67 |
| 68 DependencyInjectorTransformerGroup(TransformOptions options) |
| 69 : phases = _createPhases(options); |
| 70 |
| 71 DependencyInjectorTransformerGroup.asPlugin(BarbackSettings settings) |
| 72 : this(_parseSettings(settings.configuration)); |
| 73 } |
| 74 |
| 75 TransformOptions _parseSettings(Map args) { |
| 76 var annotations = _readStringListValue(args, 'injectable_annotations'); |
| 77 var injectedTypes = _readStringListValue(args, 'injected_types'); |
| 78 |
| 79 var sdkDir = _readStringValue(args, 'dart_sdk', required: false); |
| 80 if (sdkDir == null) { |
| 81 // Assume the Pub executable is always coming from the SDK. |
| 82 sdkDir = path.dirname(path.dirname(Platform.executable)); |
| 83 } |
| 84 |
| 85 return new TransformOptions( |
| 86 injectableAnnotations: annotations, |
| 87 injectedTypes: injectedTypes, |
| 88 sdkDirectory: sdkDir); |
| 89 } |
| 90 |
| 91 _readStringValue(Map args, String name, {bool required: true}) { |
| 92 var value = args[name]; |
| 93 if (value == null) { |
| 94 if (required) { |
| 95 print('di transformer "$name" has no value.'); |
| 96 } |
| 97 return null; |
| 98 } |
| 99 if (value is! String) { |
| 100 print('di transformer "$name" value is not a string.'); |
| 101 return null; |
| 102 } |
| 103 return value; |
| 104 } |
| 105 |
| 106 _readStringListValue(Map args, String name) { |
| 107 var value = args[name]; |
| 108 if (value == null) return []; |
| 109 var results = []; |
| 110 bool error; |
| 111 if (value is List) { |
| 112 results = value; |
| 113 error = value.any((e) => e is! String); |
| 114 } else if (value is String) { |
| 115 results = [value]; |
| 116 error = false; |
| 117 } else { |
| 118 error = true; |
| 119 } |
| 120 if (error) { |
| 121 print('Invalid value for "$name" in di transformer .'); |
| 122 } |
| 123 return results; |
| 124 } |
| 125 |
| 126 List<List<Transformer>> _createPhases(TransformOptions options) => |
| 127 [[new InjectorGenerator(options, new Resolvers(options.sdkDirectory))]]; |
OLD | NEW |