| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library web_components.build.html_import_recorder_inliner; | 4 library web_components.build.html_import_recorder_inliner; |
| 5 | 5 |
| 6 import 'package:analyzer/analyzer.dart'; | |
| 7 import 'package:analyzer/src/generated/ast.dart'; | |
| 8 import 'package:analyzer/src/generated/element.dart'; | 6 import 'package:analyzer/src/generated/element.dart'; |
| 9 import 'package:barback/barback.dart'; | |
| 10 import 'package:initialize/transformer.dart'; | 7 import 'package:initialize/transformer.dart'; |
| 11 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
| 12 import '../src/normalize_path.dart'; | 9 import '../src/normalize_path.dart'; |
| 13 | 10 |
| 14 /// [InitializerPlugin] for @HtmlImport annotations. This records all paths | 11 /// [InitializerPlugin] for @HtmlImport annotations. This records all paths |
| 15 /// seen, normalizes them relative to the entry point, and records the values in | 12 /// seen, normalizes them relative to the entry point, and records the values in |
| 16 /// [importPaths]. | 13 /// [importPaths]. |
| 17 /// | 14 /// |
| 18 /// Note: This does nothing with the paths on its own, a separate step needs to | 15 /// Note: This does nothing with the paths on its own, a separate step needs to |
| 19 /// add the imports to the entry point document. | 16 /// add the imports to the entry point document. |
| 20 class HtmlImportAnnotationRecorder implements InitializerPlugin { | 17 class HtmlImportAnnotationRecorder implements InitializerPlugin { |
| 21 /// All the normalized import paths that were seen. | 18 /// All the normalized import paths that were seen. |
| 22 final Set<String> importPaths = new Set<String>(); | 19 final Set<String> importPaths = new Set<String>(); |
| 23 | 20 |
| 24 TransformLogger _logger; | |
| 25 | |
| 26 HtmlImportAnnotationRecorder(); | 21 HtmlImportAnnotationRecorder(); |
| 27 | 22 |
| 28 /// Applies to anything named `HtmlImport` which annotates a library. | 23 /// Applies to anything named `HtmlImport` which annotates a library. |
| 29 bool shouldApply(InitializerPluginData pluginData) { | 24 bool shouldApply(InitializerPluginData pluginData) { |
| 30 var annotationElement = pluginData.initializer.annotationNode.element; | 25 var annotationElement = pluginData.initializer.annotationNode.element; |
| 31 var logger = pluginData.logger; | 26 var logger = pluginData.logger; |
| 32 DartType type; | 27 DartType type; |
| 33 if (annotationElement is ConstructorElement) { | 28 if (annotationElement is ConstructorElement) { |
| 34 type = annotationElement.returnType; | 29 type = annotationElement.returnType; |
| 35 } else if (annotationElement is PropertyAccessorElement) { | 30 } else if (annotationElement is PropertyAccessorElement) { |
| 36 type = annotationElement.variable.propagatedType; | 31 type = annotationElement.variable.propagatedType; |
| 37 if (type == null) { | 32 if (type == null) { |
| 38 type = pluginData.resolver.evaluateConstant(annotationElement.library, | 33 type = pluginData.resolver |
| 39 pluginData.initializer.annotationNode.name).value.type; | 34 .evaluateConstant(annotationElement.library, |
| 35 pluginData.initializer.annotationNode.name) |
| 36 .value |
| 37 .type; |
| 40 } | 38 } |
| 41 } else { | 39 } else { |
| 42 logger.error('Unsupported annotation type. Only constructors and ' | 40 logger.error('Unsupported annotation type. Only constructors and ' |
| 43 'properties are supported as initializers.'); | 41 'properties are supported as initializers.'); |
| 44 return false; | 42 return false; |
| 45 } | 43 } |
| 46 if (type.name != 'HtmlImport') return false; | 44 if (type.name != 'HtmlImport') return false; |
| 47 if (pluginData.initializer.targetElement is! LibraryElement) { | 45 if (pluginData.initializer.targetElement is! LibraryElement) { |
| 48 logger.error('Invalid HtmlImport annotation on non-library element.'); | 46 logger.error('Invalid HtmlImport annotation on non-library element.'); |
| 49 return false; | 47 return false; |
| 50 } | 48 } |
| 51 return true; | 49 return true; |
| 52 } | 50 } |
| 53 | 51 |
| 54 /// Records the normalized url and returns [null] so that no [InitEntry] will | 52 /// Records the normalized url and returns [null] so that no [InitEntry] will |
| 55 /// be created. | 53 /// be created. |
| 56 String apply(InitializerPluginData pluginData) { | 54 String apply(InitializerPluginData pluginData) { |
| 57 var bootstrapId = pluginData.bootstrapId; | 55 var bootstrapId = pluginData.bootstrapId; |
| 58 var logger = pluginData.logger; | 56 var logger = pluginData.logger; |
| 59 var annotation = pluginData.initializer.annotationNode; | 57 var annotation = pluginData.initializer.annotationNode; |
| 60 var annotationElement = pluginData.initializer.annotationElement; | 58 var annotationElement = pluginData.initializer.annotationElement; |
| 61 var element = pluginData.initializer.targetElement as LibraryElement; | 59 var element = pluginData.initializer.targetElement as LibraryElement; |
| 62 var resolver = pluginData.resolver; | 60 var resolver = pluginData.resolver; |
| 63 var libraryDirective = | |
| 64 pluginData.initializer.targetNode.parent.parent as LibraryDirective; | |
| 65 | 61 |
| 66 var originalImportPath; | 62 var originalImportPath; |
| 67 if (annotationElement.element is PropertyAccessorElement) { | 63 if (annotationElement.element is PropertyAccessorElement) { |
| 68 originalImportPath = resolver.evaluateConstant( | 64 originalImportPath = resolver |
| 69 element.library, annotation.name).value.fields[ | 65 .evaluateConstant(element.library, annotation.name) |
| 70 'filePath'].stringValue; | 66 .value |
| 67 .fields['filePath'] |
| 68 .toStringValue(); |
| 71 } else { | 69 } else { |
| 72 assert(annotationElement.element is ConstructorElement); | 70 assert(annotationElement.element is ConstructorElement); |
| 73 originalImportPath = resolver.evaluateConstant(element.library, | 71 originalImportPath = resolver |
| 74 annotation.arguments.arguments.first).value.stringValue; | 72 .evaluateConstant( |
| 73 element.library, annotation.arguments.arguments.first) |
| 74 .value |
| 75 .toStringValue(); |
| 75 } | 76 } |
| 76 | 77 |
| 77 var libPath; | 78 var libPath; |
| 78 var segments = element.source.uri.pathSegments; | 79 var segments = element.source.uri.pathSegments; |
| 79 var package = segments[0]; | 80 var package = segments[0]; |
| 80 if (bootstrapId.package == package && | 81 if (bootstrapId.package == package && |
| 81 bootstrapId.path.startsWith('${segments[1]}/')) { | 82 bootstrapId.path.startsWith('${segments[1]}/')) { |
| 82 package = null; | 83 package = null; |
| 83 libPath = path.url.relative( | 84 libPath = path.url.relative( |
| 84 path.url.joinAll(segments.getRange(1, segments.length)), | 85 path.url.joinAll(segments.getRange(1, segments.length)), |
| 85 from: path.url.dirname(path.url.join(bootstrapId.path))); | 86 from: path.url.dirname(path.url.join(bootstrapId.path))); |
| 86 } else if (segments[1] == 'lib') { | 87 } else if (segments[1] == 'lib') { |
| 87 libPath = path.url.joinAll(segments.getRange(2, segments.length)); | 88 libPath = path.url.joinAll(segments.getRange(2, segments.length)); |
| 88 } else { | 89 } else { |
| 89 logger.error('Unable to import `${element.source.uri.path}` from ' | 90 logger.error('Unable to import `${element.source.uri.path}` from ' |
| 90 '${bootstrapId}.'); | 91 '${bootstrapId}.'); |
| 91 return null; | 92 return null; |
| 92 } | 93 } |
| 93 | 94 |
| 94 importPaths | 95 importPaths |
| 95 .add(normalizeHtmlImportPath(originalImportPath, package, libPath)); | 96 .add(normalizeHtmlImportPath(originalImportPath, package, libPath)); |
| 96 | 97 |
| 97 // Don't emit an InitEntry. | 98 // Don't emit an InitEntry. |
| 98 return null; | 99 return null; |
| 99 } | 100 } |
| 100 } | 101 } |
| OLD | NEW |