OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import 'dart:async'; |
| 6 |
| 7 import 'package:barback/barback.dart'; |
| 8 import 'package:barback/src/utils.dart'; |
| 9 |
| 10 import 'mock.dart'; |
| 11 |
| 12 /// A transformer that uses the contents of a file to define the other inputs. |
| 13 /// |
| 14 /// Outputs a file with the same name as the primary but with an "out" |
| 15 /// extension containing the concatenated contents of all non-primary inputs. |
| 16 class ManyToOneTransformer extends MockTransformer { |
| 17 final String extension; |
| 18 |
| 19 /// Creates a transformer that consumes assets with [extension]. |
| 20 /// |
| 21 /// That file contains a comma-separated list of paths and it will input |
| 22 /// files at each of those paths. |
| 23 ManyToOneTransformer(this.extension); |
| 24 |
| 25 Future<bool> doIsPrimary(Asset asset) => |
| 26 new Future.value(asset.id.extension == ".$extension"); |
| 27 |
| 28 Future doApply(Transform transform) { |
| 29 return getPrimary(transform) |
| 30 .then((primary) => primary.readAsString()) |
| 31 .then((contents) { |
| 32 // Get all of the included inputs. |
| 33 return Future.wait(contents.split(",").map((path) { |
| 34 var id = new AssetId(transform.primaryId.package, path); |
| 35 return getInput(transform, id).then((input) => input.readAsString()); |
| 36 })); |
| 37 }).then((outputs) { |
| 38 var id = transform.primaryId.changeExtension(".out"); |
| 39 transform.addOutput(new Asset.fromString(id, outputs.join())); |
| 40 }); |
| 41 } |
| 42 |
| 43 String toString() => "many->1 $extension"; |
| 44 } |
OLD | NEW |