Index: pkg/barback/lib/src/transform.dart |
diff --git a/pkg/barback/lib/src/transform.dart b/pkg/barback/lib/src/transform.dart |
index 1695388d3d98839fa09a2f0bc534f68a707c2a29..25c260af53a809be0af5c067fb6af2f9d3de7a0a 100644 |
--- a/pkg/barback/lib/src/transform.dart |
+++ b/pkg/barback/lib/src/transform.dart |
@@ -4,10 +4,16 @@ |
library barback.transform; |
+import 'dart:async'; |
+import 'dart:convert'; |
+ |
import 'asset.dart'; |
+import 'asset_id.dart'; |
import 'asset_set.dart'; |
import 'base_transform.dart'; |
+import 'errors.dart'; |
import 'transform_node.dart'; |
+import 'utils.dart'; |
/// While a [Transformer] represents a *kind* of transformation, this defines |
/// one specific usage of it on a set of files. |
@@ -17,10 +23,76 @@ import 'transform_node.dart'; |
/// the transformation. It lets the [Transformer] access inputs and generate |
/// outputs. |
class Transform extends BaseTransform { |
+ final TransformNode _node; |
+ |
final _outputs = new AssetSet(); |
+ /// Gets the primary input asset. |
+ /// |
+ /// While a transformation can use multiple input assets, one must be a |
+ /// special "primary" asset. This will be the "entrypoint" or "main" input |
+ /// file for a transformation. |
+ /// |
+ /// For example, with a dart2js transform, the primary input would be the |
+ /// entrypoint Dart file. All of the other Dart files that that imports |
+ /// would be secondary inputs. |
+ /// |
+ /// This method may fail at runtime with an [AssetNotFoundException] if called |
+ /// asynchronously after the transform begins running. The primary input may |
+ /// become unavailable while this transformer is running due to asset changes |
+ /// earlier in the graph. You can ignore the error if this happens: the |
+ /// transformer will be re-run automatically for you. |
+ Asset get primaryInput { |
+ if (!_node.primary.state.isAvailable) { |
+ throw new AssetNotFoundException(_node.primary.id); |
+ } |
+ |
+ return _node.primary.asset; |
+ } |
+ |
Transform._(TransformNode node) |
- : super(node); |
+ : _node = node, |
+ super(node); |
+ |
+ /// Gets the asset for an input [id]. |
+ /// |
+ /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
+ Future<Asset> getInput(AssetId id) => _node.getInput(id); |
+ |
+ /// A convenience method to the contents of the input with [id] as a string. |
+ /// |
+ /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
+ /// |
+ /// If the asset was created from a [String] the original string is always |
+ /// returned and [encoding] is ignored. Otherwise, the binary data of the |
+ /// asset is decoded using [encoding], which defaults to [UTF8]. |
+ /// |
+ /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
+ Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
+ if (encoding == null) encoding = UTF8; |
+ return getInput(id).then((input) => input.readAsString(encoding: encoding)); |
+ } |
+ |
+ /// A convenience method to the contents of the input with [id]. |
+ /// |
+ /// This is equivalent to calling [getInput] followed by [Asset.read]. |
+ /// |
+ /// If the asset was created from a [String], this returns its UTF-8 encoding. |
+ /// |
+ /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
+ Stream<List<int>> readInput(AssetId id) => |
+ futureStream(getInput(id).then((input) => input.read())); |
+ |
+ /// A convenience method to return whether or not an asset exists. |
+ /// |
+ /// This is equivalent to calling [getInput] and catching an |
+ /// [AssetNotFoundException]. |
+ Future<bool> hasInput(AssetId id) { |
+ return getInput(id).then((_) => true).catchError((error) { |
+ if (error is AssetNotFoundException && error.id == id) return false; |
+ throw error; |
+ }); |
+ } |
/// Stores [output] as the output created by this transformation. |
/// |