Index: pkg/barback/test/transformer/rewrite.dart |
diff --git a/pkg/barback/test/transformer/rewrite.dart b/pkg/barback/test/transformer/rewrite.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7073795d3d565eea13384b35a7ef484b9a3ceb8f |
--- /dev/null |
+++ b/pkg/barback/test/transformer/rewrite.dart |
@@ -0,0 +1,41 @@ |
+// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
+// for details. All rights reserved. Use of this source code is governed by a |
+// BSD-style license that can be found in the LICENSE file. |
+ |
+import 'dart:async'; |
+ |
+import 'package:barback/barback.dart'; |
+ |
+import 'mock.dart'; |
+ |
+/// A [Transformer] that takes assets ending with one extension and generates |
+/// assets with a given extension. |
+/// |
+/// Appends the output extension to the contents of the input file. |
+class RewriteTransformer extends MockTransformer { |
+ final String from; |
+ final String to; |
+ |
+ /// Creates a transformer that rewrites assets whose extension is [from] to |
+ /// one whose extension is [to]. |
+ /// |
+ /// [to] may be a space-separated list in which case multiple outputs will be |
+ /// created for each input. |
+ RewriteTransformer(this.from, this.to); |
+ |
+ Future<bool> doIsPrimary(Asset asset) => |
+ new Future.value(asset.id.extension == ".$from"); |
+ |
+ Future doApply(Transform transform) { |
+ return getPrimary(transform).then((input) { |
+ return Future.wait(to.split(" ").map((extension) { |
+ var id = transform.primaryId.changeExtension(".$extension"); |
+ return input.readAsString().then((content) { |
+ transform.addOutput(new Asset.fromString(id, "$content.$extension")); |
+ }); |
+ })); |
+ }); |
+ } |
+ |
+ String toString() => "$from->$to"; |
+} |