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 |
(...skipping 11 matching lines...) Expand all Loading... | |
22 var configuration = JSON.decode(message['configuration']); | 22 var configuration = JSON.decode(message['configuration']); |
23 var mode = new BarbackMode(message['mode']); | 23 var mode = new BarbackMode(message['mode']); |
24 return _initialize(library, configuration, mode). | 24 return _initialize(library, configuration, mode). |
25 map(serializeTransformerOrGroup).toList(); | 25 map(serializeTransformerOrGroup).toList(); |
26 }); | 26 }); |
27 }); | 27 }); |
28 } | 28 } |
29 | 29 |
30 /// Loads all the transformers and groups defined in [uri]. | 30 /// Loads all the transformers and groups defined in [uri]. |
31 /// | 31 /// |
32 /// Loads the library, finds any Transformer or TransformerGroup subclasses in | 32 /// Loads the library, finds any [Transformer] or [TransformerGroup] subclasses |
33 /// it, instantiates them with [configuration] and [mode], and returns them. | 33 /// in it, instantiates them with [configuration] and [mode], and returns them. |
34 Iterable _initialize(Uri uri, Map configuration, BarbackMode mode) { | 34 List _initialize(Uri uri, Map configuration, BarbackMode mode) { |
35 var mirrors = currentMirrorSystem(); | 35 var mirrors = currentMirrorSystem(); |
36 var transformerClass = reflectClass(Transformer); | 36 var transformerClass = reflectClass(Transformer); |
37 var groupClass = reflectClass(TransformerGroup); | 37 var groupClass = reflectClass(TransformerGroup); |
38 | 38 |
39 // TODO(nweiz): if no valid transformers are found, throw an error message | 39 var seen = new Set(); |
40 // describing candidates and why they were rejected. | 40 var transformers = []; |
41 return mirrors.libraries[uri].declarations.values.map((declaration) { | 41 |
42 if (declaration is! ClassMirror) return null; | 42 loadFromLibrary(library) { |
43 var classMirror = declaration; | 43 if (seen.contains(library)) return; |
44 if (classMirror.isPrivate) return null; | 44 seen.add(library); |
45 if (classMirror.isAbstract) return null; | 45 |
46 if (!classMirror.isSubtypeOf(transformerClass) && | 46 for (var dependency in library.libraryDependencies) { |
Bob Nystrom
2014/04/15 22:21:46
Add a little doc for this.
nweiz
2014/04/15 23:57:51
Done.
| |
47 !classMirror.isSubtypeOf(groupClass)) { | 47 if (!dependency.isExport) continue; |
48 return null; | 48 loadFromLibrary(dependency.targetLibrary); |
49 } | 49 } |
50 | 50 |
51 var constructor = _getConstructor(classMirror, 'asPlugin'); | 51 // TODO(nweiz): if no valid transformers are found, throw an error message |
52 if (constructor == null) return null; | 52 // describing candidates and why they were rejected. |
53 if (constructor.parameters.isEmpty) { | 53 transformers.addAll(library.declarations.values.map((declaration) { |
54 if (configuration.isNotEmpty) return null; | 54 if (declaration is! ClassMirror) return null; |
55 return classMirror.newInstance(const Symbol('asPlugin'), []).reflectee; | 55 var classMirror = declaration; |
56 } | 56 if (classMirror.isPrivate) return null; |
57 if (constructor.parameters.length != 1) return null; | 57 if (classMirror.isAbstract) return null; |
58 if (!classMirror.isSubtypeOf(transformerClass) && | |
59 !classMirror.isSubtypeOf(groupClass)) { | |
60 return null; | |
61 } | |
58 | 62 |
59 return classMirror.newInstance(const Symbol('asPlugin'), | 63 var constructor = _getConstructor(classMirror, 'asPlugin'); |
60 [new BarbackSettings(configuration, mode)]).reflectee; | 64 if (constructor == null) return null; |
61 }).where((classMirror) => classMirror != null); | 65 if (constructor.parameters.isEmpty) { |
66 if (configuration.isNotEmpty) return null; | |
67 return classMirror.newInstance(const Symbol('asPlugin'), []).reflectee; | |
68 } | |
69 if (constructor.parameters.length != 1) return null; | |
70 | |
71 return classMirror.newInstance(const Symbol('asPlugin'), | |
72 [new BarbackSettings(configuration, mode)]).reflectee; | |
73 }).where((classMirror) => classMirror != null)); | |
74 } | |
75 | |
76 loadFromLibrary(mirrors.libraries[uri]); | |
77 return transformers; | |
62 } | 78 } |
63 | 79 |
64 // TODO(nweiz): clean this up when issue 13248 is fixed. | 80 // TODO(nweiz): clean this up when issue 13248 is fixed. |
65 MethodMirror _getConstructor(ClassMirror classMirror, String constructor) { | 81 MethodMirror _getConstructor(ClassMirror classMirror, String constructor) { |
66 var name = new Symbol("${MirrorSystem.getName(classMirror.simpleName)}" | 82 var name = new Symbol("${MirrorSystem.getName(classMirror.simpleName)}" |
67 ".$constructor"); | 83 ".$constructor"); |
68 var candidate = classMirror.declarations[name]; | 84 var candidate = classMirror.declarations[name]; |
69 if (candidate is MethodMirror && candidate.isConstructor) return candidate; | 85 if (candidate is MethodMirror && candidate.isConstructor) return candidate; |
70 return null; | 86 return null; |
71 } | 87 } |
OLD | NEW |