| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 | 4 |
| 5 library pub.asset.transformer_isolate; | 5 library pub.asset.transformer_isolate; |
| 6 | 6 |
| 7 import 'dart:convert'; | 7 import 'dart:convert'; |
| 8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
| 9 import 'dart:mirrors'; | 9 import 'dart:mirrors'; |
| 10 | 10 |
| 11 import 'package:barback/barback.dart'; | 11 import 'package:barback/barback.dart'; |
| 12 | 12 |
| 13 import 'serialize.dart'; | 13 import 'serialize.dart'; |
| 14 | 14 |
| 15 /// The mirror system. |
| 16 final _mirrors = currentMirrorSystem(); |
| 17 |
| 18 /// The URI of this library. |
| 19 final _baseUri = _mirrors.findLibrary( |
| 20 const Symbol('pub.asset.transformer_isolate')).uri; |
| 21 |
| 15 /// Sets up the initial communication with the host isolate. | 22 /// Sets up the initial communication with the host isolate. |
| 16 void loadTransformers(SendPort replyTo) { | 23 void loadTransformers(SendPort replyTo) { |
| 17 var port = new ReceivePort(); | 24 var port = new ReceivePort(); |
| 18 replyTo.send(port.sendPort); | 25 replyTo.send(port.sendPort); |
| 19 port.listen((wrappedMessage) { | 26 port.listen((wrappedMessage) { |
| 20 // TODO(nweiz): When issue 19228 is fixed, spin up a separate isolate for | 27 // TODO(nweiz): When issue 19228 is fixed, spin up a separate isolate for |
| 21 // libraries loaded beyond the first so they can run in parallel. | 28 // libraries loaded beyond the first so they can run in parallel. |
| 22 respond(wrappedMessage, (message) { | 29 respond(wrappedMessage, (message) { |
| 23 var library = Uri.parse(message['library']); | |
| 24 var configuration = JSON.decode(message['configuration']); | 30 var configuration = JSON.decode(message['configuration']); |
| 25 var mode = new BarbackMode(message['mode']); | 31 var mode = new BarbackMode(message['mode']); |
| 26 return _initialize(library, configuration, mode). | 32 return _initialize(message['library'], configuration, mode). |
| 27 map(serializeTransformerLike).toList(); | 33 map(serializeTransformerLike).toList(); |
| 28 }); | 34 }); |
| 29 }); | 35 }); |
| 30 } | 36 } |
| 31 | 37 |
| 32 /// Loads all the transformers and groups defined in [uri]. | 38 /// Loads all the transformers and groups defined in [uri]. |
| 33 /// | 39 /// |
| 34 /// Loads the library, finds any [Transformer] or [TransformerGroup] subclasses | 40 /// Loads the library, finds any [Transformer] or [TransformerGroup] subclasses |
| 35 /// in it, instantiates them with [configuration] and [mode], and returns them. | 41 /// in it, instantiates them with [configuration] and [mode], and returns them. |
| 36 List _initialize(Uri uri, Map configuration, BarbackMode mode) { | 42 List _initialize(String uri, Map configuration, BarbackMode mode) { |
| 37 var mirrors = currentMirrorSystem(); | |
| 38 var transformerClass = reflectClass(Transformer); | 43 var transformerClass = reflectClass(Transformer); |
| 39 var aggregateClass = _aggregateTransformerClass; | 44 var aggregateClass = _aggregateTransformerClass; |
| 40 var groupClass = reflectClass(TransformerGroup); | 45 var groupClass = reflectClass(TransformerGroup); |
| 41 | 46 |
| 42 var seen = new Set(); | 47 var seen = new Set(); |
| 43 var transformers = []; | 48 var transformers = []; |
| 44 | 49 |
| 45 loadFromLibrary(library) { | 50 loadFromLibrary(library) { |
| 46 if (seen.contains(library)) return; | 51 if (seen.contains(library)) return; |
| 47 seen.add(library); | 52 seen.add(library); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 72 if (configuration.isNotEmpty) return null; | 77 if (configuration.isNotEmpty) return null; |
| 73 return classMirror.newInstance(const Symbol('asPlugin'), []).reflectee; | 78 return classMirror.newInstance(const Symbol('asPlugin'), []).reflectee; |
| 74 } | 79 } |
| 75 if (constructor.parameters.length != 1) return null; | 80 if (constructor.parameters.length != 1) return null; |
| 76 | 81 |
| 77 return classMirror.newInstance(const Symbol('asPlugin'), | 82 return classMirror.newInstance(const Symbol('asPlugin'), |
| 78 [new BarbackSettings(configuration, mode)]).reflectee; | 83 [new BarbackSettings(configuration, mode)]).reflectee; |
| 79 }).where((classMirror) => classMirror != null)); | 84 }).where((classMirror) => classMirror != null)); |
| 80 } | 85 } |
| 81 | 86 |
| 82 loadFromLibrary(mirrors.libraries[uri]); | 87 loadFromLibrary(_mirrors.libraries[_baseUri.resolve(uri)]); |
| 83 return transformers; | 88 return transformers; |
| 84 } | 89 } |
| 85 | 90 |
| 86 // TODO(nweiz): clean this up when issue 13248 is fixed. | 91 // TODO(nweiz): clean this up when issue 13248 is fixed. |
| 87 MethodMirror _getConstructor(ClassMirror classMirror, String constructor) { | 92 MethodMirror _getConstructor(ClassMirror classMirror, String constructor) { |
| 88 var name = new Symbol("${MirrorSystem.getName(classMirror.simpleName)}" | 93 var name = new Symbol("${MirrorSystem.getName(classMirror.simpleName)}" |
| 89 ".$constructor"); | 94 ".$constructor"); |
| 90 var candidate = classMirror.declarations[name]; | 95 var candidate = classMirror.declarations[name]; |
| 91 if (candidate is MethodMirror && candidate.isConstructor) return candidate; | 96 if (candidate is MethodMirror && candidate.isConstructor) return candidate; |
| 92 return null; | 97 return null; |
| 93 } | 98 } |
| 94 | 99 |
| 95 // Older barbacks don't support [AggregateTransformer], and calling | 100 // Older barbacks don't support [AggregateTransformer], and calling |
| 96 // [reflectClass] on an undefined class will throw an error, so we just define a | 101 // [reflectClass] on an undefined class will throw an error, so we just define a |
| 97 // null getter for them. | 102 // null getter for them. |
| 98 //# if barback >=0.14.1 | 103 //# if barback >=0.14.1 |
| 99 ClassMirror get _aggregateTransformerClass => | 104 ClassMirror get _aggregateTransformerClass => |
| 100 reflectClass(AggregateTransformer); | 105 reflectClass(AggregateTransformer); |
| 101 //# else | 106 //# else |
| 102 //> ClassMirror get _aggregateTransformerClass => null; | 107 //> ClassMirror get _aggregateTransformerClass => null; |
| 103 //# end | 108 //# end |
| OLD | NEW |