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