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