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