Chromium Code Reviews| 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 |
| 11 import '../../../asset/dart/serialize.dart'; | 11 import '../../../asset/dart/serialize.dart'; |
| 12 import '../barback.dart'; | 12 import '../barback.dart'; |
| 13 import '../dart.dart' as dart; | 13 import '../dart.dart' as dart; |
| 14 import '../log.dart' as log; | 14 import '../log.dart' as log; |
| 15 import '../utils.dart'; | 15 import '../utils.dart'; |
| 16 import 'asset_environment.dart'; | 16 import 'asset_environment.dart'; |
| 17 import 'foreign_transformer.dart'; | 17 import 'foreign_transformer.dart'; |
| 18 import 'barback_server.dart'; | 18 import 'barback_server.dart'; |
| 19 | 19 |
| 20 /// Load and return all transformers and groups from the library identified by | 20 /// Load and return all transformers and groups from the libraries identified by |
| 21 /// [id]. | 21 /// [ids]. |
|
Bob Nystrom
2014/06/11 20:33:36
Document the return value.
nweiz
2014/06/12 20:11:17
Done.
| |
| 22 Future<Set> loadTransformers(AssetEnvironment environment, | 22 Future<Map<TransformerId, Set>> loadTransformers(AssetEnvironment environment, |
| 23 BarbackServer transformerServer, TransformerId id) { | 23 BarbackServer transformerServer, List<TransformerId> ids) { |
| 24 return id.getAssetId(environment.barback).then((assetId) { | 24 return listToMapAsync(ids, (id) => id, (id) { |
|
Bob Nystrom
2014/06/11 20:33:35
I think this would be easier to read if these were
nweiz
2014/06/12 20:11:17
Will do in a separate CL.
| |
| 25 var path = assetId.path.replaceFirst('lib/', ''); | 25 return id.getAssetId(environment.barback); |
|
Bob Nystrom
2014/06/11 20:33:35
=>
nweiz
2014/06/12 20:11:17
I had it as that originally, but that followed by
| |
| 26 // TODO(nweiz): load from a "package:" URI when issue 12474 is fixed. | 26 }).then((idsToAssetIds) { |
| 27 var baseUrl = transformerServer.url; | |
| 28 var idsToUrls = mapMap(idsToAssetIds, (id, _) => id, (id, assetId) { | |
| 29 var path = assetId.path.replaceFirst('lib/', ''); | |
| 30 // TODO(nweiz): load from a "package:" URI when issue 12474 is fixed. | |
| 31 return baseUrl.resolve('packages/${id.package}/$path'); | |
| 32 }); | |
| 27 | 33 |
| 28 var baseUrl = transformerServer.url; | 34 var code = new StringBuffer(); |
| 29 var uri = baseUrl.resolve('packages/${id.package}/$path'); | 35 code.writeln("import 'dart:isolate';"); |
| 30 var code = """ | |
| 31 import 'dart:isolate'; | |
| 32 | 36 |
| 33 import '$uri'; | 37 for (var url in idsToUrls.values) { |
| 38 code.writeln("import '$url';"); | |
| 39 } | |
| 34 | 40 |
| 35 import r'$baseUrl/packages/\$pub/transformer_isolate.dart'; | 41 code.writeln("import r'$baseUrl/packages/\$pub/transformer_isolate.dart';"); |
| 42 code.writeln( | |
| 43 "void main(_, SendPort replyTo) => loadTransformers(replyTo);"); | |
| 36 | 44 |
| 37 void main(_, SendPort replyTo) => loadTransformers(replyTo); | 45 log.fine("Loading transformers from $ids"); |
| 38 """; | |
| 39 log.fine("Loading transformers from $assetId"); | |
| 40 | 46 |
| 41 var port = new ReceivePort(); | 47 var port = new ReceivePort(); |
| 42 return dart.runInIsolate(code, port.sendPort) | 48 return dart.runInIsolate(code.toString(), port.sendPort) |
| 43 .then((_) => port.first) | 49 .then((_) => port.first) |
| 44 .then((sendPort) { | 50 .then((sendPort) { |
| 45 return call(sendPort, { | 51 return mapMapAsync(idsToAssetIds, (id, _) => id, (id, assetId) { |
| 46 'library': uri.toString(), | 52 return call(sendPort, { |
| 47 'mode': environment.mode.name, | 53 'library': idsToUrls[id].toString(), |
| 48 // TODO(nweiz): support non-JSON-encodable configuration maps. | 54 'mode': environment.mode.name, |
| 49 'configuration': JSON.encode(id.configuration) | 55 // TODO(nweiz): support non-JSON-encodable configuration maps. |
| 50 }).then((transformers) { | 56 'configuration': JSON.encode(id.configuration) |
| 51 transformers = transformers.map( | 57 }).then((transformers) { |
| 52 (transformer) => deserializeTransformerLike(transformer, id)) | 58 transformers = transformers.map( |
| 53 .toSet(); | 59 (transformer) => deserializeTransformerLike(transformer, id)) |
| 54 log.fine("Transformers from $assetId: $transformers"); | 60 .toSet(); |
| 55 return transformers; | 61 log.fine("Transformers from $assetId: $transformers"); |
| 62 return transformers; | |
| 63 }); | |
| 56 }); | 64 }); |
| 57 }).catchError((error, stackTrace) { | 65 }).catchError((error, stackTrace) { |
| 58 if (error is! CrossIsolateException) throw error; | 66 if (error is! CrossIsolateException) throw error; |
| 59 if (error.type != 'IsolateSpawnException') throw error; | 67 if (error.type != 'IsolateSpawnException') throw error; |
| 68 | |
| 60 // TODO(nweiz): don't parse this as a string once issues 12617 and 12689 | 69 // TODO(nweiz): don't parse this as a string once issues 12617 and 12689 |
| 61 // are fixed. | 70 // are fixed. |
| 62 if (!error.message.split('\n')[1].startsWith("Failure getting $uri:")) { | 71 var firstErrorLine = error.message.split('\n')[1]; |
| 63 throw error; | 72 var missingTransformers = idsToUrls.keys.where((id) => |
| 64 } | 73 firstErrorLine.startsWith("Failure getting ${idsToUrls[id]}:")); |
|
Bob Nystrom
2014/06/11 20:33:36
Will this ever match more than one transformer?
nweiz
2014/06/12 20:11:17
I guess not. Changed.
| |
| 74 if (missingTransformers.isEmpty) throw error; | |
| 75 | |
| 76 var packageUris = toSentence(missingTransformers.map((id) => | |
| 77 '"${idToPackageUri(idsToAssetIds[id])}"')); | |
| 78 var libraries = pluralize('library', missingTransformers.length, | |
| 79 plural: 'libraries'); | |
| 65 | 80 |
| 66 // If there was an IsolateSpawnException and the import that actually | 81 // If there was an IsolateSpawnException and the import that actually |
| 67 // failed was the one we were loading transformers from, throw an | 82 // failed was the one we were loading transformers from, throw an |
| 68 // application exception with a more user-friendly message. | 83 // application exception with a more user-friendly message. |
| 69 fail('Transformer library "package:${id.package}/$path" not found.', | 84 fail('Transformer $libraries $packageUris not found.', |
| 70 error, stackTrace); | 85 error, stackTrace); |
| 71 }); | 86 }); |
| 72 }); | 87 }); |
| 73 } | 88 } |
| OLD | NEW |