This repository has been archived by the owner on Dec 6, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 57
/
generator.dart
579 lines (510 loc) · 21.5 KB
/
generator.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/**
* Generates Factory & paramKeys maps into a file by crawling source files and
* finding all constructors that are annotated for injection. Does the same thing as
* transformer.dart for when transformers are not available, except without modifications
* to the main function and DI. As such, the user needs to import the generated file, and run
* `Module.DEFAULT_REFLECTOR = new GeneratedTypeFactories(typeFactories, parameterKeys)`
* imported, before any modules are initialized. Import 'di_static.dart' instead of 'di.dart'
* to avoid mirrors, and import 'reflector_static.dart' for GeneratedTypeFactories.
* See transformer.dart for more info.
*/
library di.generator;
import 'package:analyzer/src/generated/java_io.dart';
import 'package:analyzer/src/generated/source_io.dart';
import 'package:analyzer/src/generated/ast.dart';
import 'package:analyzer/src/generated/sdk.dart' show DartSdk;
import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk;
import 'package:analyzer/src/generated/element.dart';
import 'package:analyzer/src/generated/engine.dart';
import 'package:path/path.dart' as path;
import 'dart:io';
const String PACKAGE_PREFIX = 'package:';
const String DART_PACKAGE_PREFIX = 'dart:';
const List<String> _DEFAULT_INJECTABLE_ANNOTATIONS = const ['di.annotations.Injectable'];
main(List<String> args) {
if (args.length < 4) {
print('Usage: generator path_to_sdk file_to_resolve annotations output [package_roots+]');
exit(0);
}
var pathToSdk = args[0];
var entryPoint = args[1];
var classAnnotations = args[2].split(',')..addAll(_DEFAULT_INJECTABLE_ANNOTATIONS);
var output = args[3];
var packageRoots = (args.length < 5)
? [path.fromUri(Platform.packageRoot ?? '')]
: args.sublist(4);
print('pathToSdk: $pathToSdk');
print('entryPoint: $entryPoint');
print('classAnnotations: ${classAnnotations.join(', ')}');
print('output: $output');
print('packageRoots: $packageRoots');
var code = generateCode(entryPoint, classAnnotations, pathToSdk, packageRoots, output);
code.forEach((chunk, code) {
String fileName = output;
if (chunk.library != null) {
var lastDot = fileName.lastIndexOf('.');
fileName = fileName.substring(0, lastDot) + '-' + chunk.library.name + fileName.substring(lastDot);
}
new File(fileName).writeAsStringSync(code);
});
}
Map<Chunk, String> generateCode(String entryPoint, List<String> classAnnotations,
String pathToSdk, List<String> packageRoots, String outputFilename) {
var c = new SourceCrawler(pathToSdk, packageRoots);
List<String> imports = <String>[];
Map<Chunk, List<ClassElement>> typeFactoryTypes = <Chunk, List<ClassElement>>{};
Map<String, String> typeToImport = new Map<String, String>();
Map<String, InterfaceType> typeMappings = {};
c.crawl(entryPoint, (CompilationUnitElement compilationUnit, SourceFile source) {
new CompilationUnitVisitor(c.context, source, classAnnotations, imports, typeToImport,
typeFactoryTypes, typeMappings, outputFilename).visit(compilationUnit, source);
});
return printLibraryCode(typeToImport, imports, typeFactoryTypes, typeMappings);
}
Map<Chunk, String> printLibraryCode(Map<String, String> typeToImport, List<String> imports,
Map<Chunk, List<ClassElement>> typeFactoryTypes,
Map<String, InterfaceType> typeMapping) {
Map<Chunk, StringBuffer> factories = <Chunk, StringBuffer>{};
Map<Chunk, StringBuffer> keys = <Chunk, StringBuffer>{};
Map<Chunk, StringBuffer> paramLists = <Chunk, StringBuffer>{};
Map<Chunk, String> result = <Chunk, String>{};
typeFactoryTypes.forEach((Chunk chunk, List<ClassElement> classes) {
List<String> requiredImports = <String>[];
String resolveClassIdentifier(InterfaceType type, [List typeArgs,
bool usedToGenerateConstructor = false]) {
final TYPE_LITERAL = 'TypeLiteral';
if (type.element.library.isDartCore) {
//workaround for https://github.com/angular/di.dart/issues/183
final canUseTypeLiteral = typeMapping.containsKey(TYPE_LITERAL) &&
!usedToGenerateConstructor;
if (type.typeParameters.isNotEmpty && canUseTypeLiteral) {
return 'new ${resolveClassIdentifier(typeMapping[TYPE_LITERAL])}<$type>().type';
}
return type.name;
}
String import = typeToImport[getCanonicalName(type)];
if (!requiredImports.contains(import)) {
requiredImports.add(import);
}
String prefix = _calculateImportPrefix(import, imports);
return '$prefix.${type.name}';
}
factories[chunk] = new StringBuffer();
keys[chunk] = new StringBuffer();
paramLists[chunk] = new StringBuffer();
process_classes(classes, keys[chunk], factories[chunk], paramLists[chunk],
resolveClassIdentifier);
StringBuffer code = new StringBuffer();
String libSuffix = chunk.library == null ? '' : '.${chunk.library.name}';
code.write('library di.generated.type_factories$libSuffix;\n');
requiredImports.forEach((import) {
String prefix = _calculateImportPrefix(import, imports);
code.write ('import "$import" as $prefix;\n');
});
code..write('import "package:di/key.dart" show Key;\n')
..write(keys[chunk])
..write('Map<Type, Function> typeFactories = {\n${factories[chunk]}};\n')
..write('Map<Type, List<Key>> parameterKeys = {\n${paramLists[chunk]}};\n')
..write('main() {}\n');
result[chunk] = code.toString();
});
return result;
}
typedef String IdentifierResolver(InterfaceType type, [List typeArgs, bool usedToGenerateConstructor]);
/**
* Takes classes and writes to StringBuffers the corresponding keys, factories,
* and paramLists needed for static injection.
*
* resolveClassIdentifier is a function passed in to be called to resolve imports
*/
void process_classes(Iterable<ClassElement> classes, StringBuffer keys,
StringBuffer factories, StringBuffer paramLists,
IdentifierResolver resolveClassIdentifier) {
Map<String, String> toBeAdded = new Map<String, String>();
Set<String> addedKeys = new Set();
classes.forEach((ClassElement clazz) {
StringBuffer factory = new StringBuffer();
StringBuffer paramList = new StringBuffer();
List<String> factoryKeys = new List<String>();
bool skip = false;
var className = getUniqueName(clazz.type);
var classType = resolveClassIdentifier(clazz.type, [], true);
if (addedKeys.add(className)) {
toBeAdded[className] =
'final Key _KEY_$className = new Key($classType);\n';
}
factoryKeys.add(className);
ConstructorElement constr = clazz.constructors.firstWhere((c) => c.name.isEmpty,
orElse: () {
throw 'Unable to find default constructor for $clazz in ${clazz.source}';
});
var args = new List.generate(constr.parameters.length, (i) => 'a$i').join(', ');
factory.write('$classType: ($args) => new $classType($args),\n');
paramList.write('$classType: ');
if (constr.parameters.isEmpty){
paramList.write('const [');
} else {
paramList.write('[');
paramList.write(constr.parameters.map((param) {
if (param.type.element is! ClassElement) {
throw 'Unable to resolve type for constructor parameter '
'"${param.name}" for type "$clazz" in ${clazz.source}';
}
var annotations = new Iterable.empty();
if (param.metadata.isNotEmpty) {
annotations = param.metadata.map((item) => (item.element as FunctionTypedElement).returnType.name);
}
String keyName = annotations.isNotEmpty ?
'${param.type.name}_${annotations.first}' :
param.type.name;
(param.type as ParameterizedType).typeArguments.forEach((arg) => keyName = '${keyName}_${arg.name}');
String output = '_KEY_${keyName}';
if (addedKeys.add(keyName)){
var annotationParam = "";
if (param.metadata.isNotEmpty) {
var p = resolveClassIdentifier((param.metadata.first.element as FunctionTypedElement).returnType);
annotationParam = ", $p";
}
var clsId = resolveClassIdentifier(param.type, (param.type as ParameterizedType).typeArguments);
toBeAdded[keyName]='final Key _KEY_${keyName} = new Key($clsId$annotationParam);\n';
}
return output;
}).join(', '));
}
paramList.write('],\n');
if (!skip) {
factoryKeys.forEach((key) {
var keyString = toBeAdded.remove(key);
if (keyString != null) {
keys.write(keyString);
}
});
factories.write(factory);
paramLists.write(paramList);
}
});
keys.writeAll(toBeAdded.values);
toBeAdded.clear();
}
String _calculateImportPrefix(String import, List<String> imports) =>
'import_${imports.indexOf(import)}';
class CompilationUnitVisitor {
List<String> imports;
Map<String, String> typeToImport;
Map<Chunk, List<ClassElement>> typeFactoryTypes;
List<String> classAnnotations;
SourceFile source;
AnalysisContext context;
String outputFilename;
Map<String, InterfaceType> typeMappings;
CompilationUnitVisitor(this.context, this.source,
this.classAnnotations, this.imports, this.typeToImport,
this.typeFactoryTypes, this.typeMappings, this.outputFilename);
visit(CompilationUnitElement compilationUnit, SourceFile source) {
if (typeFactoryTypes[source.chunk] == null) {
typeFactoryTypes[source.chunk] = <ClassElement>[];
}
visitLibrary(compilationUnit.enclosingElement, source);
List<ClassElement> types = <ClassElement>[];
types.addAll(compilationUnit.types);
for (CompilationUnitElement part in compilationUnit.enclosingElement.parts) {
types.addAll(part.types);
}
types.forEach((clazz) => visitClassElement(clazz, source));
}
visitLibrary(LibraryElement libElement, SourceFile source) {
CompilationUnit resolvedUnit = context.resolveCompilationUnit(libElement.source, libElement);
resolvedUnit.directives.forEach((Directive directive) {
if (directive is LibraryDirective) {
LibraryDirective library = directive;
int annotationIdx = 0;
library.metadata.forEach((Annotation ann) {
if (ann.element is ConstructorElement &&
getQualifiedName(
(ann.element as ConstructorElement).enclosingElement.type) ==
'di.annotations.Injectables') {
ListLiteral listLiteral = library.metadata[annotationIdx].arguments.arguments.first;
for (Expression expr in listLiteral.elements) {
Element element = (expr as SimpleIdentifier).bestElement;
if (element == null || element is! ClassElement) {
throw 'Unable to resolve type "$expr" from @Injectables '
'in ${library.element.source}';
}
if (!typeFactoryTypes[source.chunk].contains(element)) {
typeFactoryTypes[source.chunk].add(element as ClassElement);
}
}
}
annotationIdx++;
});
}
});
}
visitClassElement(ClassElement classElement, SourceFile source) {
if (classElement.isPrivate) return; // ignore private classes.
var importUri = source.entryPointImport;
if (Uri.parse(importUri).scheme == '') {
importUri = path.relative(importUri, from: path.dirname(outputFilename));
}
typeToImport[getCanonicalName(classElement.type)] = importUri;
if (classElement.name == 'TypeLiteral' && classElement.library.name == 'di.type_literal') {
typeMappings.putIfAbsent(classElement.name, () => classElement.type);
}
if (!imports.contains(importUri)) {
imports.add(importUri);
}
for (ElementAnnotation ann in classElement.metadata) {
if (ann.element is ConstructorElement) {
ClassElement classEl = ann.element.enclosingElement;
List<DartType> types = [classEl.type]..addAll(classEl.allSupertypes);
if (types.any((DartType t) => classAnnotations.contains(getQualifiedName(t)))) {
// The class is injectable when the metadata is either:
// - an instance of any `classAnnotations` types,
// - a subtype of any `classAnnotations` types.
if (typeFactoryTypes[source.chunk] == null) {
typeFactoryTypes[source.chunk] = <ClassElement>[];
}
if (!typeFactoryTypes[source.chunk].contains(classElement)) {
typeFactoryTypes[source.chunk].add(classElement);
}
}
}
}
}
}
String getQualifiedName(InterfaceType type) {
var lib = type.element.library.displayName;
var name = type.name;
return lib == null ? name : '$lib.$name';
}
String getUniqueName(InterfaceType type) {
var lib = type.element.library.displayName;
var name = type.name;
String qualName = lib == null ? name : '$lib.$name';
return qualName.replaceAll('.', '_');
}
String getCanonicalName(InterfaceType type) {
var source = type.element.source.toString();
var name = type.name;
return '$source:$name';
}
typedef CompilationUnitCrawler(CompilationUnitElement compilationUnit, SourceFile source);
class SourceCrawler {
final List<String> packageRoots;
final String sdkPath;
AnalysisContext context = AnalysisEngine.instance.createAnalysisContext();
SourceCrawler(this.sdkPath, this.packageRoots);
void crawl(String entryPoint, CompilationUnitCrawler _visitor,
{bool preserveComments : false}) {
JavaSystemIO.setProperty("com.google.dart.sdk", sdkPath);
DartSdk sdk = DirectoryBasedDartSdk.defaultSdk;
AnalysisOptionsImpl contextOptions = new AnalysisOptionsImpl();
contextOptions.cacheSize = 256;
contextOptions.preserveComments = preserveComments;
contextOptions.analyzeFunctionBodies = false;
context.analysisOptions = contextOptions;
sdk.context.analysisOptions = contextOptions;
var packageUriResolver = new PackageUriResolver(packageRoots.map(
(pr) => new JavaFile.fromUri(new Uri.file(pr))).toList());
context.sourceFactory = new SourceFactory([
new DartUriResolver(sdk),
new FileUriResolver(),
packageUriResolver
]);
var entryPointFile;
var entryPointImport;
if (entryPoint.startsWith(PACKAGE_PREFIX)) {
entryPointFile = new JavaFile(packageUriResolver
.resolveAbsolute(Uri.parse(entryPoint)).toString());
entryPointImport = entryPoint;
} else {
entryPointFile = new JavaFile(entryPoint);
entryPointImport = entryPointFile.getAbsolutePath();
}
Source source = new FileBasedSource(entryPointFile);
ChangeSet changeSet = new ChangeSet();
changeSet.addedSource(source);
context.applyChanges(changeSet);
LibraryElement rootLib = context.computeLibraryElement(source);
CompilationUnit resolvedUnit =
context.resolveCompilationUnit(source, rootLib);
var sourceFile = new SourceFile(
entryPointFile.getAbsolutePath(),
entryPointImport,
resolvedUnit,
resolvedUnit.element,
new Chunk()); // root chunk
List<SourceFile> toVisit = <SourceFile>[sourceFile];
List<SourceFile> deferred = <SourceFile>[sourceFile];
while (deferred.isNotEmpty) {
toVisit.add(deferred.removeAt(0));
while (toVisit.isNotEmpty) {
SourceFile currentFile = toVisit.removeAt(0);
currentFile.chunk.addVisited(currentFile);
_visitor(currentFile.compilationUnitElement, currentFile);
var visitor = new CrawlerVisitor(currentFile, context);
visitor.accept(currentFile.compilationUnit);
visitor.toVisit.forEach((SourceFile todo) {
if (!toVisit.contains(todo) && !currentFile.chunk.alreadyVisited(todo)) {
toVisit.add(todo);
}
});
visitor.deferred.forEach((SourceFile todo) {
if (!deferred.contains(todo) && !currentFile.chunk.alreadyVisited(todo)) {
deferred.add(todo);
}
});
}
}
}
}
class CrawlerVisitor {
List<SourceFile> toVisit = <SourceFile>[];
List<SourceFile> deferred = <SourceFile>[];
SourceFile currentFile;
AnalysisContext context;
String currentDir;
CrawlerVisitor(this.currentFile, this.context);
void accept(CompilationUnit cu) {
cu.directives.forEach((Directive directive) {
if (directive.element == null) return; // unresolvable, ignore
if (directive is ImportDirective) {
ImportElement import = directive.element;
visitImportElement(
new Library(import, import.uri, cu, import.importedLibrary.name),
import.importedLibrary.source);
}
if (directive is ExportDirective) {
ExportElement export = directive.element;
visitImportElement(
new Library(export, export.uri, cu, export.exportedLibrary.name),
export.exportedLibrary.source);
}
});
}
void visitImportElement(Library library, Source source) {
String uri = library.uri;
if (uri == null) return; // dart:core
String systemImport;
bool isSystem = false;
if (uri.startsWith(DART_PACKAGE_PREFIX)) {
isSystem = true;
systemImport = uri;
} else if (currentFile.entryPointImport.startsWith(DART_PACKAGE_PREFIX)) {
isSystem = true;
systemImport = currentFile.entryPointImport;
}
// check if it's some internal hidden library
if (isSystem && systemImport.substring(DART_PACKAGE_PREFIX.length).startsWith('_')) {
return;
}
var nextCompilationUnit = context
.resolveCompilationUnit(source, context.computeLibraryElement(source));
SourceFile sourceFile;
if (uri.startsWith(PACKAGE_PREFIX)) {
sourceFile = new SourceFile(source.toString(), uri,
nextCompilationUnit, nextCompilationUnit.element, currentFile.chunk);
} else { // relative import.
var newImport;
if (isSystem) {
newImport = systemImport; // original uri
} else {
// relative import
String import = currentFile.entryPointImport;
import = import.replaceAll('\\', '/'); // if at all needed, on Windows
import = import.substring(0, import.lastIndexOf('/'));
var currentDir = new File(currentFile.canonicalPath).parent.path;
currentDir = currentDir.replaceAll('\\', '/'); // if at all needed, on Windows
if (uri.startsWith('../')) {
while (uri.startsWith('../')) {
uri = uri.substring('../'.length);
import = import.substring(0, import.lastIndexOf('/'));
currentDir = currentDir.substring(0, currentDir.lastIndexOf('/'));
}
}
newImport = '$import/$uri';
}
sourceFile = new SourceFile(
source.toString(), newImport,
nextCompilationUnit, nextCompilationUnit.element, currentFile.chunk);
}
if (library.isDeferred || isDeferredImport(library)) {
var childChunk = currentFile.chunk.createChild(library);
deferred.add(new SourceFile(source.toString(), sourceFile.entryPointImport,
nextCompilationUnit, nextCompilationUnit.element, childChunk));
} else {
toVisit.add(sourceFile);
}
}
}
// TODO(cbracken) eliminate once support for old-style is removed in SDK 1.7.0
bool isDeferredImport(Library library) {
var isDeferred = false;
library.element.metadata.forEach((ElementAnnotation annotation) {
if (annotation.element is PropertyAccessorElement) {
PropertyAccessorElement pa = annotation.element;
library.compilationUnit.declarations.forEach((CompilationUnitMember member) {
if (member is TopLevelVariableDeclaration && member.variables.isConst) {
TopLevelVariableDeclaration topLevel = member;
topLevel.variables.variables.forEach((VariableDeclaration varDecl) {
if (varDecl.initializer is InstanceCreationExpression &&
(varDecl.initializer as InstanceCreationExpression).isConst &&
(varDecl.initializer as InstanceCreationExpression).staticElement is ConstructorElement &&
varDecl.name.name == pa.name) {
ConstructorElement constr = (varDecl.initializer as InstanceCreationExpression).staticElement;
if (constr.enclosingElement.library.name == 'dart.async' &&
constr.enclosingElement.type.name == 'DeferredLibrary') {
isDeferred = true;
}
}
});
}
});
}
});
return isDeferred;
}
class Library {
final Element element;
final String uri;
final CompilationUnit compilationUnit;
final String name;
Library(this.element, this.uri, this.compilationUnit, this.name);
bool get isDeferred => element is ImportElement && (element as ImportElement).isDeferred;
String toString() => 'Library[$name]';
}
class Chunk {
final Chunk parent;
Library library;
List<SourceFile> _visited = <SourceFile>[];
addVisited(SourceFile file) {
_visited.add(file);
}
bool alreadyVisited(SourceFile file) {
var cursor = this;
while (cursor != null) {
if (cursor._visited.contains(file)) {
return true;
}
cursor = cursor.parent;
}
return false;
}
Chunk([this.parent, this.library]);
Chunk createChild(Library library) => new Chunk(this, library);
String toString() => 'Chunk[$library]';
}
class SourceFile {
String canonicalPath;
String entryPointImport;
CompilationUnit compilationUnit;
CompilationUnitElement compilationUnitElement;
Chunk chunk;
SourceFile(this.canonicalPath, this.entryPointImport, this.compilationUnit,
this.compilationUnitElement, this.chunk);
operator ==(o) {
if (o is String) return o == canonicalPath;
if (o is! SourceFile) return false;
return o.canonicalPath == canonicalPath;
}
}