| 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'; |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 235 found = true; | 235 found = true; |
| 236 }); | 236 }); |
| 237 return found; | 237 return found; |
| 238 } | 238 } |
| 239 | 239 |
| 240 String _buildNewEntryPoint(LibraryElement entryLib) { | 240 String _buildNewEntryPoint(LibraryElement entryLib) { |
| 241 var importsBuffer = new StringBuffer(); | 241 var importsBuffer = new StringBuffer(); |
| 242 var initializersBuffer = new StringBuffer(); | 242 var initializersBuffer = new StringBuffer(); |
| 243 var libraryPrefixes = new Map<LibraryElement, String>(); | 243 var libraryPrefixes = new Map<LibraryElement, String>(); |
| 244 | 244 |
| 245 // Import the static_loader and original entry point. | 245 // Import the static_loader, initializer, and original entry point. |
| 246 importsBuffer | 246 importsBuffer |
| 247 .writeln("import 'package:initialize/src/static_loader.dart';"); | 247 .writeln("import 'package:initialize/src/static_loader.dart';"); |
| 248 importsBuffer.writeln("import 'package:initialize/src/initializer.dart';"); |
| 248 libraryPrefixes[entryLib] = 'i0'; | 249 libraryPrefixes[entryLib] = 'i0'; |
| 249 | 250 |
| 250 initializersBuffer.writeln(' initializers.addAll(['); | 251 initializersBuffer.writeln(' initializers.addAll(['); |
| 251 while (_initQueue.isNotEmpty) { | 252 while (_initQueue.isNotEmpty) { |
| 252 var next = _initQueue.removeFirst(); | 253 var next = _initQueue.removeFirst(); |
| 253 | 254 |
| 254 libraryPrefixes.putIfAbsent( | 255 libraryPrefixes.putIfAbsent( |
| 255 next.element.library, () => 'i${libraryPrefixes.length}'); | 256 next.element.library, () => 'i${libraryPrefixes.length}'); |
| 256 libraryPrefixes.putIfAbsent( | 257 libraryPrefixes.putIfAbsent( |
| 257 next.annotation.element.library, () => 'i${libraryPrefixes.length}'); | 258 next.annotation.element.library, () => 'i${libraryPrefixes.length}'); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 } | 294 } |
| 294 | 295 |
| 295 _writeInitializer(_InitializerData data, | 296 _writeInitializer(_InitializerData data, |
| 296 Map<LibraryElement, String> libraryPrefixes, StringBuffer buffer) { | 297 Map<LibraryElement, String> libraryPrefixes, StringBuffer buffer) { |
| 297 final annotationElement = data.annotation.element; | 298 final annotationElement = data.annotation.element; |
| 298 final element = data.element; | 299 final element = data.element; |
| 299 | 300 |
| 300 final metaPrefix = libraryPrefixes[annotationElement.library]; | 301 final metaPrefix = libraryPrefixes[annotationElement.library]; |
| 301 var elementString; | 302 var elementString; |
| 302 if (element is LibraryElement) { | 303 if (element is LibraryElement) { |
| 303 elementString = '#${element.name}'; | 304 var segments = element.source.uri.pathSegments; |
| 305 var package = segments[0]; |
| 306 var libraryPath; |
| 307 var packageString; |
| 308 if (_newEntryPoint.package == package && |
| 309 _newEntryPoint.path.startsWith('${segments[1]}/')) { |
| 310 // reset `package` to null, we will do a relative path in this case. |
| 311 packageString = 'null'; |
| 312 libraryPath = path.url.relative( |
| 313 path.url.joinAll(segments.getRange(1, segments.length)), |
| 314 from: path.url.dirname(path.url.join(_newEntryPoint.path))); |
| 315 } else if (segments[1] == 'lib') { |
| 316 packageString = "'$package'"; |
| 317 libraryPath = path.url.joinAll(segments.getRange(2, segments.length)); |
| 318 } else { |
| 319 _logger.error('Unable to import `${element.source.uri.path}` from ' |
| 320 '${_newEntryPoint.path}.'); |
| 321 } |
| 322 |
| 323 elementString = "const LibraryIdentifier(" |
| 324 "#${element.name}, $packageString, '$libraryPath')"; |
| 304 } else if (element is ClassElement || element is FunctionElement) { | 325 } else if (element is ClassElement || element is FunctionElement) { |
| 305 elementString = | 326 elementString = |
| 306 '${libraryPrefixes[data.element.library]}.${element.name}'; | 327 '${libraryPrefixes[data.element.library]}.${element.name}'; |
| 307 } else { | 328 } else { |
| 308 _logger.error('Initializers can only be applied to top level functins, ' | 329 _logger.error('Initializers can only be applied to top level functins, ' |
| 309 'libraries, and classes.'); | 330 'libraries, and classes.'); |
| 310 } | 331 } |
| 311 | 332 |
| 312 if (annotationElement is ConstructorElement) { | 333 if (annotationElement is ConstructorElement) { |
| 313 var node = data.element.node; | 334 var node = data.element.node; |
| (...skipping 202 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 516 })).map((import) => import.importedLibrary); | 537 })).map((import) => import.importedLibrary); |
| 517 } | 538 } |
| 518 | 539 |
| 519 // Element/ElementAnnotation pair. | 540 // Element/ElementAnnotation pair. |
| 520 class _InitializerData { | 541 class _InitializerData { |
| 521 final Element element; | 542 final Element element; |
| 522 final ElementAnnotation annotation; | 543 final ElementAnnotation annotation; |
| 523 | 544 |
| 524 _InitializerData(this.element, this.annotation); | 545 _InitializerData(this.element, this.annotation); |
| 525 } | 546 } |
| OLD | NEW |