OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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.load_transformers; | 5 library pub.load_transformers; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 import 'dart:isolate'; | 9 import 'dart:isolate'; |
10 | 10 |
(...skipping 16 matching lines...) Expand all Loading... |
27 /// Load and return all transformers and groups from the library identified by | 27 /// Load and return all transformers and groups from the library identified by |
28 /// [id]. | 28 /// [id]. |
29 Future<Set> loadTransformers(BuildEnvironment environment, | 29 Future<Set> loadTransformers(BuildEnvironment environment, |
30 BarbackServer transformerServer, TransformerId id) { | 30 BarbackServer transformerServer, TransformerId id) { |
31 return id.getAssetId(environment.barback).then((assetId) { | 31 return id.getAssetId(environment.barback).then((assetId) { |
32 var path = assetId.path.replaceFirst('lib/', ''); | 32 var path = assetId.path.replaceFirst('lib/', ''); |
33 // TODO(nweiz): load from a "package:" URI when issue 12474 is fixed. | 33 // TODO(nweiz): load from a "package:" URI when issue 12474 is fixed. |
34 | 34 |
35 var baseUrl = transformerServer.url; | 35 var baseUrl = transformerServer.url; |
36 var uri = '$baseUrl/packages/${id.package}/$path'; | 36 var uri = '$baseUrl/packages/${id.package}/$path'; |
37 var code = 'import "$uri";\n' + | 37 var code = """ |
38 readAsset(p.join("dart", "transformer_isolate.dart")) | 38 import 'dart:isolate'; |
39 .replaceAll('<<URL_BASE>>', baseUrl); | 39 |
| 40 import '$uri'; |
| 41 |
| 42 import r'$baseUrl/packages/\$pub/transformer_isolate.dart'; |
| 43 |
| 44 void main(_, SendPort replyTo) => loadTransformers(replyTo); |
| 45 """; |
40 log.fine("Loading transformers from $assetId"); | 46 log.fine("Loading transformers from $assetId"); |
41 | 47 |
42 var port = new ReceivePort(); | 48 var port = new ReceivePort(); |
43 return dart.runInIsolate(code, port.sendPort) | 49 return dart.runInIsolate(code, port.sendPort) |
44 .then((_) => port.first) | 50 .then((_) => port.first) |
45 .then((sendPort) { | 51 .then((sendPort) { |
46 return _call(sendPort, { | 52 return _call(sendPort, { |
47 'library': uri, | 53 'library': uri, |
48 'mode': environment.mode.name, | 54 'mode': environment.mode.name, |
49 // TODO(nweiz): support non-JSON-encodable configuration maps. | 55 // TODO(nweiz): support non-JSON-encodable configuration maps. |
50 'configuration': JSON.encode(id.configuration) | 56 'configuration': JSON.encode(id.configuration) |
51 }).then((transformers) { | 57 }).then((transformers) { |
52 transformers = transformers.map( | 58 transformers = transformers.map( |
53 (transformer) => _deserializeTransformerOrGroup(transformer, id)) | 59 (transformer) => _deserializeTransformerOrGroup(transformer, id)) |
54 .toSet(); | 60 .toSet(); |
55 log.fine("Transformers from $assetId: $transformers"); | 61 log.fine("Transformers from $assetId: $transformers"); |
56 return transformers; | 62 return transformers; |
57 }); | 63 }); |
58 }).catchError((error, stackTrace) { | 64 }).catchError((error, stackTrace) { |
59 if (error is! dart.CrossIsolateException) throw error; | 65 if (error is! dart.CrossIsolateException) throw error; |
60 if (error.type != 'IsolateSpawnException') throw error; | 66 if (error.type != 'IsolateSpawnException') throw error; |
61 // TODO(nweiz): don't parse this as a string once issues 12617 and 12689 | 67 // TODO(nweiz): don't parse this as a string once issues 12617 and 12689 |
62 // are fixed. | 68 // are fixed. |
63 if (!error.message.split('\n')[1].startsWith('import "$uri";')) { | 69 if (!error.message.split('\n')[1].endsWith("import '$uri';")) { |
64 throw error; | 70 throw error; |
65 } | 71 } |
66 | 72 |
67 // If there was an IsolateSpawnException and the import that actually | 73 // If there was an IsolateSpawnException and the import that actually |
68 // failed was the one we were loading transformers from, throw an | 74 // failed was the one we were loading transformers from, throw an |
69 // application exception with a more user-friendly message. | 75 // application exception with a more user-friendly message. |
70 fail('Transformer library "package:${id.package}/$path" not found.', | 76 fail('Transformer library "package:${id.package}/$path" not found.', |
71 error, stackTrace); | 77 error, stackTrace); |
72 }); | 78 }); |
73 }); | 79 }); |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
141 if (message['type'] == 'getInput') { | 147 if (message['type'] == 'getInput') { |
142 return transform.getInput(_deserializeId(message['id'])) | 148 return transform.getInput(_deserializeId(message['id'])) |
143 .then((asset) => serializeAsset(asset)); | 149 .then((asset) => serializeAsset(asset)); |
144 } | 150 } |
145 | 151 |
146 if (message['type'] == 'addOutput') { | 152 if (message['type'] == 'addOutput') { |
147 transform.addOutput(deserializeAsset(message['output'])); | 153 transform.addOutput(deserializeAsset(message['output'])); |
148 return null; | 154 return null; |
149 } | 155 } |
150 | 156 |
| 157 if (message['type'] == 'consumePrimary') { |
| 158 transform.consumePrimary(); |
| 159 return null; |
| 160 } |
| 161 |
151 assert(message['type'] == 'log'); | 162 assert(message['type'] == 'log'); |
152 var method; | 163 var method; |
153 if (message['level'] == 'Info') { | 164 if (message['level'] == 'Info') { |
154 method = transform.logger.info; | 165 method = transform.logger.info; |
155 } else if (message['level'] == 'Fine') { | 166 } else if (message['level'] == 'Fine') { |
156 method = transform.logger.fine; | 167 method = transform.logger.fine; |
157 } else if (message['level'] == 'Warning') { | 168 } else if (message['level'] == 'Warning') { |
158 method = transform.logger.warning; | 169 method = transform.logger.warning; |
159 } else { | 170 } else { |
160 assert(message['level'] == 'Error'); | 171 assert(message['level'] == 'Error'); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 /// | 288 /// |
278 /// This handles [AssetNotFoundException]s specially, ensuring that their | 289 /// This handles [AssetNotFoundException]s specially, ensuring that their |
279 /// metadata is preserved. | 290 /// metadata is preserved. |
280 dart.CrossIsolateException _deserializeException(Map error) { | 291 dart.CrossIsolateException _deserializeException(Map error) { |
281 if (error['type'] == 'AssetNotFoundException') { | 292 if (error['type'] == 'AssetNotFoundException') { |
282 return new _CrossIsolateAssetNotFoundException.deserialize(error); | 293 return new _CrossIsolateAssetNotFoundException.deserialize(error); |
283 } else { | 294 } else { |
284 return new dart.CrossIsolateException.deserialize(error); | 295 return new dart.CrossIsolateException.deserialize(error); |
285 } | 296 } |
286 } | 297 } |
OLD | NEW |