| 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 library barback.transform; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'asset.dart'; |
| 10 import 'asset_id.dart'; |
| 11 import 'asset_node.dart'; |
| 12 import 'errors.dart'; |
| 13 import 'transform_node.dart'; |
| 14 |
| 15 /// Creates a [Transform] by forwarding to the private constructor. |
| 16 /// |
| 17 /// Lets [TransformNode] create [Transforms] without giving a [Transform] |
| 18 /// itself a public constructor, which would be visible to external users. |
| 19 /// Unlike the [Transform] class, this function is not exported by barback.dart. |
| 20 Transform createTransform(TransformNode node, Set<AssetNode> inputs, |
| 21 Map<AssetId, Asset> outputs) => |
| 22 new Transform._(node, inputs, outputs); |
| 23 |
| 24 /// While a [Transformer] represents a *kind* of transformation, this defines |
| 25 /// one specific usage of it on a set of files. |
| 26 /// |
| 27 /// This ephemeral object exists only during an actual transform application to |
| 28 /// facilitate communication between the [Transformer] and the code hosting |
| 29 /// the transformation. It lets the [Transformer] access inputs and generate |
| 30 /// outputs. |
| 31 class Transform { |
| 32 final TransformNode _node; |
| 33 |
| 34 final Set<AssetNode> _inputs; |
| 35 final Map<AssetId, Asset> _outputs; |
| 36 |
| 37 /// Gets the ID of the primary input for this transformation. |
| 38 /// |
| 39 /// While a transformation can use multiple input assets, one must be a |
| 40 /// special "primary" asset. This will be the "entrypoint" or "main" input |
| 41 /// file for a transformation. |
| 42 /// |
| 43 /// For example, with a dart2js transform, the primary input would be the |
| 44 /// entrypoint Dart file. All of the other Dart files that that imports |
| 45 /// would be secondary inputs. |
| 46 AssetId get primaryId => _node.primary.id; |
| 47 |
| 48 /// Gets the asset for the primary input. |
| 49 Future<Asset> get primaryInput => getInput(primaryId); |
| 50 |
| 51 Transform._(this._node, this._inputs, this._outputs); |
| 52 |
| 53 /// Gets the asset for for an input [id]. |
| 54 /// |
| 55 /// If an input with that ID cannot be found, throws an |
| 56 /// [AssetNotFoundException]. |
| 57 Future<Asset> getInput(AssetId id) { |
| 58 return new Future(() { |
| 59 var node = _node.phase.inputs[id]; |
| 60 // TODO(rnystrom): Need to handle passthrough where an asset from a |
| 61 // previous phase can be found. |
| 62 |
| 63 // Throw if the input isn't found. This ensures the transformer's apply |
| 64 // is exited. We'll then catch this and report it through the proper |
| 65 // results stream. |
| 66 if (node == null) throw new MissingInputException(id); |
| 67 |
| 68 // Keep track of which assets this transform depends on. |
| 69 _inputs.add(node); |
| 70 return node.asset; |
| 71 }); |
| 72 } |
| 73 |
| 74 /// Stores [output] as the output created by this transformation for asset |
| 75 /// [id]. A transformation can output as many assets as it wants. |
| 76 void addOutput(AssetId id, Asset output) { |
| 77 _outputs[id] = output; |
| 78 } |
| 79 } |
| OLD | NEW |