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 |
| 9 import 'mock.dart'; |
| 10 |
| 11 /// A [Transformer] that takes assets ending with one extension and generates |
| 12 /// assets with a given extension. |
| 13 /// |
| 14 /// Appends the output extension to the contents of the input file. |
| 15 class RewriteTransformer extends MockTransformer { |
| 16 final String from; |
| 17 final String to; |
| 18 |
| 19 /// Creates a transformer that rewrites assets whose extension is [from] to |
| 20 /// one whose extension is [to]. |
| 21 /// |
| 22 /// [to] may be a space-separated list in which case multiple outputs will be |
| 23 /// created for each input. |
| 24 RewriteTransformer(this.from, this.to); |
| 25 |
| 26 Future<bool> doIsPrimary(Asset asset) => |
| 27 new Future.value(asset.id.extension == ".$from"); |
| 28 |
| 29 Future doApply(Transform transform) { |
| 30 return getPrimary(transform).then((input) { |
| 31 return Future.wait(to.split(" ").map((extension) { |
| 32 var id = transform.primaryId.changeExtension(".$extension"); |
| 33 return input.readAsString().then((content) { |
| 34 transform.addOutput(new Asset.fromString(id, "$content.$extension")); |
| 35 }); |
| 36 })); |
| 37 }); |
| 38 } |
| 39 |
| 40 String toString() => "$from->$to"; |
| 41 } |
OLD | NEW |