| 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 initialize.transformer; | 4 library initialize.transformer; |
| 5 | 5 |
| 6 import 'dart:async'; | 6 import 'dart:async'; |
| 7 import 'dart:collection' show Queue; | 7 import 'dart:collection' show Queue; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/element.dart'; | 9 import 'package:analyzer/src/generated/element.dart'; |
| 10 import 'package:barback/barback.dart'; | 10 import 'package:barback/barback.dart'; |
| 11 import 'package:code_transformers/resolver.dart'; | 11 import 'package:code_transformers/resolver.dart'; |
| 12 import 'package:path/path.dart' as path; | 12 import 'package:path/path.dart' as path; |
| 13 | 13 |
| 14 /// Removes the mirror-based initialization logic and replaces it with static | 14 /// Removes the mirror-based initialization logic and replaces it with static |
| 15 /// logic. | 15 /// logic. |
| 16 class InitializeTransformer extends Transformer { | 16 class InitializeTransformer extends Transformer { |
| 17 final BarbackSettings _settings; | |
| 18 final Resolvers _resolvers; | 17 final Resolvers _resolvers; |
| 18 final String _entryPoint; |
| 19 final String _newEntryPoint; |
| 19 | 20 |
| 20 InitializeTransformer.asPlugin(this._settings) | 21 InitializeTransformer(this._entryPoint, this._newEntryPoint) |
| 21 : _resolvers = new Resolvers.fromMock({ | 22 : _resolvers = new Resolvers.fromMock({ |
| 22 // The list of types below is derived from: | 23 // The list of types below is derived from: |
| 23 // * types that are used internally by the resolver (see | 24 // * types that are used internally by the resolver (see |
| 24 // _initializeFrom in resolver.dart). | 25 // _initializeFrom in resolver.dart). |
| 25 // TODO(jakemac): Move this into code_transformers so it can be shared. | 26 // TODO(jakemac): Move this into code_transformers so it can be shared. |
| 26 'dart:core': ''' | 27 'dart:core': ''' |
| 27 library dart.core; | 28 library dart.core; |
| 28 class Object {} | 29 class Object {} |
| 29 class Function {} | 30 class Function {} |
| 30 class StackTrace {} | 31 class StackTrace {} |
| (...skipping 20 matching lines...) Expand all Loading... |
| 51 | 52 |
| 52 class List<V> extends Object {} | 53 class List<V> extends Object {} |
| 53 class Map<K, V> extends Object {} | 54 class Map<K, V> extends Object {} |
| 54 ''', | 55 ''', |
| 55 'dart:html': ''' | 56 'dart:html': ''' |
| 56 library dart.html; | 57 library dart.html; |
| 57 class HtmlElement {} | 58 class HtmlElement {} |
| 58 ''', | 59 ''', |
| 59 }); | 60 }); |
| 60 | 61 |
| 61 String get _entryPoint => _settings.configuration['entryPoint']; | 62 factory InitializeTransformer.asPlugin(BarbackSettings settings) { |
| 62 String get _newEntryPoint { | 63 var entryPoint = settings.configuration['entryPoint']; |
| 63 var val = _settings.configuration['newEntryPoint']; | 64 var newEntryPoint = settings.configuration['newEntryPoint']; |
| 64 if (val == null) val = _entryPoint.replaceFirst('.dart', '.bootstrap.dart'); | 65 if (newEntryPoint == null) { |
| 65 return val; | 66 newEntryPoint = entryPoint.replaceFirst('.dart', '.bootstrap.dart'); |
| 67 } |
| 68 return new InitializeTransformer(entryPoint, newEntryPoint); |
| 66 } | 69 } |
| 67 | 70 |
| 68 bool isPrimary(AssetId id) => _entryPoint == id.path; | 71 bool isPrimary(AssetId id) => _entryPoint == id.path; |
| 69 | 72 |
| 70 Future apply(Transform transform) { | 73 Future apply(Transform transform) { |
| 71 var newEntryPointId = | 74 var newEntryPointId = |
| 72 new AssetId(transform.primaryInput.id.package, _newEntryPoint); | 75 new AssetId(transform.primaryInput.id.package, _newEntryPoint); |
| 73 return transform.hasInput(newEntryPointId).then((exists) { | 76 return transform.hasInput(newEntryPointId).then((exists) { |
| 74 if (exists) { | 77 if (exists) { |
| 75 transform.logger | 78 transform.logger |
| (...skipping 22 matching lines...) Expand all Loading... |
| 98 /// Queue for intialization annotations. | 101 /// Queue for intialization annotations. |
| 99 final _initQueue = new Queue<_InitializerData>(); | 102 final _initQueue = new Queue<_InitializerData>(); |
| 100 /// All the annotations we have seen for each element | 103 /// All the annotations we have seen for each element |
| 101 final _seenAnnotations = new Map<Element, Set<ElementAnnotation>>(); | 104 final _seenAnnotations = new Map<Element, Set<ElementAnnotation>>(); |
| 102 | 105 |
| 103 TransformLogger _logger; | 106 TransformLogger _logger; |
| 104 | 107 |
| 105 _BootstrapFileBuilder( | 108 _BootstrapFileBuilder( |
| 106 this._resolver, this._transform, this._entryPoint, this._newEntryPoint) { | 109 this._resolver, this._transform, this._entryPoint, this._newEntryPoint) { |
| 107 _logger = _transform.logger; | 110 _logger = _transform.logger; |
| 108 _initializeLibrary = _resolver | 111 _initializeLibrary = |
| 109 .getLibrary(new AssetId('initialize', 'lib/initialize.dart')); | 112 _resolver.getLibrary(new AssetId('initialize', 'lib/initialize.dart')); |
| 110 _initializer = _initializeLibrary.getType('Initializer'); | 113 _initializer = _initializeLibrary.getType('Initializer'); |
| 111 } | 114 } |
| 112 | 115 |
| 113 /// Adds the new entry point file to the transform. Should only be ran once. | 116 /// Adds the new entry point file to the transform. Should only be ran once. |
| 114 void run() { | 117 void run() { |
| 115 var entryLib = _resolver.getLibrary(_entryPoint); | 118 var entryLib = _resolver.getLibrary(_entryPoint); |
| 116 _readLibraries(entryLib); | 119 _readLibraries(entryLib); |
| 117 | 120 |
| 118 _transform.addOutput( | 121 _transform.addOutput( |
| 119 new Asset.fromString(_newEntryPoint, _buildNewEntryPoint(entryLib))); | 122 new Asset.fromString(_newEntryPoint, _buildNewEntryPoint(entryLib))); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 176 }); | 179 }); |
| 177 return found; | 180 return found; |
| 178 } | 181 } |
| 179 | 182 |
| 180 String _buildNewEntryPoint(LibraryElement entryLib) { | 183 String _buildNewEntryPoint(LibraryElement entryLib) { |
| 181 var importsBuffer = new StringBuffer(); | 184 var importsBuffer = new StringBuffer(); |
| 182 var initializersBuffer = new StringBuffer(); | 185 var initializersBuffer = new StringBuffer(); |
| 183 var libraryPrefixes = new Map<LibraryElement, String>(); | 186 var libraryPrefixes = new Map<LibraryElement, String>(); |
| 184 | 187 |
| 185 // Import the static_loader and original entry point. | 188 // Import the static_loader and original entry point. |
| 186 importsBuffer.writeln( | 189 importsBuffer |
| 187 "import 'package:initialize/src/static_loader.dart';"); | 190 .writeln("import 'package:initialize/src/static_loader.dart';"); |
| 188 _maybeWriteImport(entryLib, libraryPrefixes, importsBuffer); | 191 _maybeWriteImport(entryLib, libraryPrefixes, importsBuffer); |
| 189 | 192 |
| 190 initializersBuffer.writeln(' initializers.addAll(['); | 193 initializersBuffer.writeln(' initializers.addAll(['); |
| 191 while (_initQueue.isNotEmpty) { | 194 while (_initQueue.isNotEmpty) { |
| 192 var next = _initQueue.removeFirst(); | 195 var next = _initQueue.removeFirst(); |
| 193 | 196 |
| 194 _maybeWriteImport(next.element.library, libraryPrefixes, importsBuffer); | 197 _maybeWriteImport(next.element.library, libraryPrefixes, importsBuffer); |
| 195 _maybeWriteImport( | 198 _maybeWriteImport( |
| 196 next.annotation.element.library, libraryPrefixes, importsBuffer); | 199 next.annotation.element.library, libraryPrefixes, importsBuffer); |
| 197 | 200 |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 })).map((import) => import.importedLibrary); | 374 })).map((import) => import.importedLibrary); |
| 372 } | 375 } |
| 373 | 376 |
| 374 // Element/ElementAnnotation pair. | 377 // Element/ElementAnnotation pair. |
| 375 class _InitializerData { | 378 class _InitializerData { |
| 376 final Element element; | 379 final Element element; |
| 377 final ElementAnnotation annotation; | 380 final ElementAnnotation annotation; |
| 378 | 381 |
| 379 _InitializerData(this.element, this.annotation); | 382 _InitializerData(this.element, this.annotation); |
| 380 } | 383 } |
| OLD | NEW |