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 21 matching lines...) Expand all Loading... |
32 }); | 32 }); |
33 }); | 33 }); |
34 } | 34 } |
35 | 35 |
36 /// Loads all the transformers and groups defined in [uri]. | 36 /// Loads all the transformers and groups defined in [uri]. |
37 /// | 37 /// |
38 /// Loads the library, finds any [Transformer] or [TransformerGroup] subclasses | 38 /// Loads the library, finds any [Transformer] or [TransformerGroup] subclasses |
39 /// in it, instantiates them with [configuration] and [mode], and returns them. | 39 /// in it, instantiates them with [configuration] and [mode], and returns them. |
40 List _initialize(String uri, Map configuration, BarbackMode mode) { | 40 List _initialize(String uri, Map configuration, BarbackMode mode) { |
41 var transformerClass = reflectClass(Transformer); | 41 var transformerClass = reflectClass(Transformer); |
42 var aggregateClass = _aggregateTransformerClass; | 42 var aggregateClass = reflectClass(AggregateTransformer); |
43 var groupClass = reflectClass(TransformerGroup); | 43 var groupClass = reflectClass(TransformerGroup); |
44 | 44 |
45 var seen = new Set(); | 45 var seen = new Set(); |
46 var transformers = []; | 46 var transformers = []; |
47 | 47 |
48 loadFromLibrary(library) { | 48 loadFromLibrary(library) { |
49 if (seen.contains(library)) return; | 49 if (seen.contains(library)) return; |
50 seen.add(library); | 50 seen.add(library); |
51 | 51 |
52 // Load transformers from libraries exported by [library]. | 52 // Load transformers from libraries exported by [library]. |
53 for (var dependency in library.libraryDependencies) { | 53 for (var dependency in library.libraryDependencies) { |
54 if (!dependency.isExport) continue; | 54 if (!dependency.isExport) continue; |
55 loadFromLibrary(dependency.targetLibrary); | 55 loadFromLibrary(dependency.targetLibrary); |
56 } | 56 } |
57 | 57 |
58 // TODO(nweiz): if no valid transformers are found, throw an error message | 58 // TODO(nweiz): if no valid transformers are found, throw an error message |
59 // describing candidates and why they were rejected. | 59 // describing candidates and why they were rejected. |
60 transformers.addAll(library.declarations.values.map((declaration) { | 60 transformers.addAll(library.declarations.values.map((declaration) { |
61 if (declaration is! ClassMirror) return null; | 61 if (declaration is! ClassMirror) return null; |
62 var classMirror = declaration; | 62 var classMirror = declaration; |
63 if (classMirror.isPrivate) return null; | 63 if (classMirror.isPrivate) return null; |
64 if (classMirror.isAbstract) return null; | 64 if (classMirror.isAbstract) return null; |
65 if (!classMirror.isSubtypeOf(transformerClass) && | 65 if (!classMirror.isSubtypeOf(transformerClass) && |
66 !classMirror.isSubtypeOf(groupClass) && | 66 !classMirror.isSubtypeOf(groupClass) && |
67 (aggregateClass == null || | 67 !classMirror.isSubtypeOf(aggregateClass)) { |
68 !classMirror.isSubtypeOf(aggregateClass))) { | |
69 return null; | 68 return null; |
70 } | 69 } |
71 | 70 |
72 var constructor = _getConstructor(classMirror, 'asPlugin'); | 71 var constructor = _getConstructor(classMirror, 'asPlugin'); |
73 if (constructor == null) return null; | 72 if (constructor == null) return null; |
74 if (constructor.parameters.isEmpty) { | 73 if (constructor.parameters.isEmpty) { |
75 if (configuration.isNotEmpty) return null; | 74 if (configuration.isNotEmpty) return null; |
76 return classMirror.newInstance(const Symbol('asPlugin'), []).reflectee; | 75 return classMirror.newInstance(const Symbol('asPlugin'), []).reflectee; |
77 } | 76 } |
78 if (constructor.parameters.length != 1) return null; | 77 if (constructor.parameters.length != 1) return null; |
(...skipping 14 matching lines...) Expand all Loading... |
93 } | 92 } |
94 | 93 |
95 // TODO(nweiz): clean this up when issue 13248 is fixed. | 94 // TODO(nweiz): clean this up when issue 13248 is fixed. |
96 MethodMirror _getConstructor(ClassMirror classMirror, String constructor) { | 95 MethodMirror _getConstructor(ClassMirror classMirror, String constructor) { |
97 var name = new Symbol("${MirrorSystem.getName(classMirror.simpleName)}" | 96 var name = new Symbol("${MirrorSystem.getName(classMirror.simpleName)}" |
98 ".$constructor"); | 97 ".$constructor"); |
99 var candidate = classMirror.declarations[name]; | 98 var candidate = classMirror.declarations[name]; |
100 if (candidate is MethodMirror && candidate.isConstructor) return candidate; | 99 if (candidate is MethodMirror && candidate.isConstructor) return candidate; |
101 return null; | 100 return null; |
102 } | 101 } |
103 | |
104 // Older barbacks don't support [AggregateTransformer], and calling | |
105 // [reflectClass] on an undefined class will throw an error, so we just define a | |
106 // null getter for them. | |
107 //# if barback >=0.14.1 | |
108 ClassMirror get _aggregateTransformerClass => | |
109 reflectClass(AggregateTransformer); | |
110 //# else | |
111 //> ClassMirror get _aggregateTransformerClass => null; | |
112 //# end | |
OLD | NEW |