OLD | NEW |
(Empty) | |
| 1 library angular.tools.transformer.metadata_generator; |
| 2 |
| 3 import 'dart:async'; |
| 4 import 'package:analyzer/src/generated/element.dart'; |
| 5 import 'package:angular/tools/transformer/options.dart'; |
| 6 import 'package:barback/barback.dart'; |
| 7 import 'package:code_transformers/resolver.dart'; |
| 8 import 'package:path/path.dart' as path; |
| 9 |
| 10 import 'metadata_extractor.dart'; |
| 11 |
| 12 class MetadataGenerator extends Transformer with ResolverTransformer { |
| 13 final TransformOptions options; |
| 14 |
| 15 MetadataGenerator(this.options, Resolvers resolvers) { |
| 16 this.resolvers = resolvers; |
| 17 } |
| 18 |
| 19 void applyResolver(Transform transform, Resolver resolver) { |
| 20 var asset = transform.primaryInput; |
| 21 var id = asset.id; |
| 22 var outputFilename = '${path.url.basenameWithoutExtension(id.path)}' |
| 23 '_static_metadata.dart'; |
| 24 var outputPath = path.url.join(path.url.dirname(id.path), outputFilename); |
| 25 var outputId = new AssetId(id.package, outputPath); |
| 26 |
| 27 var extractor = new AnnotationExtractor(transform.logger, resolver, |
| 28 outputId); |
| 29 |
| 30 var outputBuffer = new StringBuffer(); |
| 31 _writeHeader(asset.id, outputBuffer); |
| 32 |
| 33 var annotatedTypes = resolver.libraries |
| 34 .where((lib) => !lib.isInSdk) |
| 35 .expand((lib) => lib.units) |
| 36 .expand((unit) => unit.types) |
| 37 .map(extractor.extractAnnotations) |
| 38 .where((annotations) => annotations != null).toList(); |
| 39 |
| 40 var libs = annotatedTypes.expand((type) => type.referencedLibraries) |
| 41 .toSet(); |
| 42 |
| 43 var importPrefixes = <LibraryElement, String>{}; |
| 44 var index = 0; |
| 45 for (var lib in libs) { |
| 46 if (lib.isDartCore) { |
| 47 importPrefixes[lib] = ''; |
| 48 continue; |
| 49 } |
| 50 |
| 51 var prefix = 'import_${index++}'; |
| 52 var url = resolver.getImportUri(lib, from: outputId); |
| 53 outputBuffer.write('import \'$url\' as $prefix;\n'); |
| 54 importPrefixes[lib] = '$prefix.'; |
| 55 } |
| 56 |
| 57 _writePreamble(outputBuffer); |
| 58 |
| 59 _writeClassPreamble(outputBuffer); |
| 60 for (var type in annotatedTypes) { |
| 61 type.writeClassAnnotations( |
| 62 outputBuffer, transform.logger, resolver, importPrefixes); |
| 63 } |
| 64 _writeClassEpilogue(outputBuffer); |
| 65 |
| 66 transform.addOutput( |
| 67 new Asset.fromString(outputId, outputBuffer.toString())); |
| 68 transform.addOutput(asset); |
| 69 } |
| 70 } |
| 71 |
| 72 void _writeHeader(AssetId id, StringSink sink) { |
| 73 var libPath = path.withoutExtension(id.path).replaceAll('/', '.'); |
| 74 sink.write(''' |
| 75 library ${id.package}.$libPath.generated_metadata; |
| 76 |
| 77 import 'package:angular/core/registry.dart' show MetadataExtractor; |
| 78 import 'package:di/di.dart' show Module; |
| 79 |
| 80 '''); |
| 81 } |
| 82 |
| 83 void _writePreamble(StringSink sink) { |
| 84 sink.write(''' |
| 85 Module get metadataModule => new Module() |
| 86 ..value(MetadataExtractor, new _StaticMetadataExtractor()); |
| 87 |
| 88 class _StaticMetadataExtractor implements MetadataExtractor { |
| 89 Iterable call(Type type) { |
| 90 var annotations = typeAnnotations[type]; |
| 91 if (annotations != null) { |
| 92 return annotations; |
| 93 } |
| 94 return []; |
| 95 } |
| 96 } |
| 97 |
| 98 '''); |
| 99 } |
| 100 |
| 101 void _writeClassPreamble(StringSink sink) { |
| 102 sink.write(''' |
| 103 final Map<Type, Object> typeAnnotations = { |
| 104 '''); |
| 105 } |
| 106 |
| 107 void _writeClassEpilogue(StringSink sink) { |
| 108 sink.write(''' |
| 109 }; |
| 110 '''); |
| 111 } |
| 112 |
| 113 void _writeFooter(StringSink sink) { |
| 114 sink.write(''' |
| 115 }; |
| 116 '''); |
| 117 } |
OLD | NEW |