| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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.transformer.transform; | 5 library barback.transformer.transform; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; | 8 import 'dart:convert'; |
| 9 | 9 |
| 10 import '../asset/asset.dart'; | 10 import '../asset/asset.dart'; |
| 11 import '../asset/asset_id.dart'; | 11 import '../asset/asset_id.dart'; |
| 12 import '../asset/asset_set.dart'; | |
| 13 import '../errors.dart'; | 12 import '../errors.dart'; |
| 14 import '../graph/transform_node.dart'; | 13 import 'aggregate_transform.dart'; |
| 15 import '../utils.dart'; | 14 import 'transform_logger.dart'; |
| 16 import 'base_transform.dart'; | 15 |
| 16 /// Creates a new [Transform] wrapping an [AggregateTransform]. |
| 17 /// |
| 18 /// Although barback internally works in terms of [AggregateTransformer]s, most |
| 19 /// transformers only work on individual primary inputs in isolation. We want to |
| 20 /// allow those transformers to implement the more user-friendly [Transformer] |
| 21 /// interface which takes the more user-friendly [Transform] object. This method |
| 22 /// wraps the more general [AggregateTransform] to return a [Transform] instead. |
| 23 Future<Transform> newTransform(AggregateTransform aggregate) { |
| 24 // A wrapped [Transformer] will assign each primary input a unique transform |
| 25 // key, so we can safely get the first asset emitted. We don't want to wait |
| 26 // for the stream to close, since that requires barback to prove that no more |
| 27 // new assets will be generated. |
| 28 return aggregate.primaryInputs.first.then((primaryInput) => |
| 29 new Transform._(aggregate, primaryInput)); |
| 30 } |
| 17 | 31 |
| 18 /// While a [Transformer] represents a *kind* of transformation, this defines | 32 /// While a [Transformer] represents a *kind* of transformation, this defines |
| 19 /// one specific usage of it on a set of files. | 33 /// one specific usage of it on a set of files. |
| 20 /// | 34 /// |
| 21 /// This ephemeral object exists only during an actual transform application to | 35 /// This ephemeral object exists only during an actual transform application to |
| 22 /// facilitate communication between the [Transformer] and the code hosting | 36 /// facilitate communication between the [Transformer] and the code hosting |
| 23 /// the transformation. It lets the [Transformer] access inputs and generate | 37 /// the transformation. It lets the [Transformer] access inputs and generate |
| 24 /// outputs. | 38 /// outputs. |
| 25 class Transform extends BaseTransform { | 39 class Transform { |
| 26 final TransformNode _node; | 40 /// The underlying aggregate transform. |
| 27 | 41 final AggregateTransform _aggregate; |
| 28 final _outputs = new AssetSet(); | |
| 29 | 42 |
| 30 /// Gets the primary input asset. | 43 /// Gets the primary input asset. |
| 31 /// | 44 /// |
| 32 /// While a transformation can use multiple input assets, one must be a | 45 /// While a transformation can use multiple input assets, one must be a |
| 33 /// special "primary" asset. This will be the "entrypoint" or "main" input | 46 /// special "primary" asset. This will be the "entrypoint" or "main" input |
| 34 /// file for a transformation. | 47 /// file for a transformation. |
| 35 /// | 48 /// |
| 36 /// For example, with a dart2js transform, the primary input would be the | 49 /// For example, with a dart2js transform, the primary input would be the |
| 37 /// entrypoint Dart file. All of the other Dart files that that imports | 50 /// entrypoint Dart file. All of the other Dart files that that imports |
| 38 /// would be secondary inputs. | 51 /// would be secondary inputs. |
| 39 /// | 52 final Asset primaryInput; |
| 40 /// This method may fail at runtime with an [AssetNotFoundException] if called | |
| 41 /// asynchronously after the transform begins running. The primary input may | |
| 42 /// become unavailable while this transformer is running due to asset changes | |
| 43 /// earlier in the graph. You can ignore the error if this happens: the | |
| 44 /// transformer will be re-run automatically for you. | |
| 45 Asset get primaryInput { | |
| 46 if (!_node.primary.state.isAvailable) { | |
| 47 throw new AssetNotFoundException(_node.primary.id); | |
| 48 } | |
| 49 | 53 |
| 50 return _node.primary.asset; | 54 /// A logger so that the [Transformer] can report build details. |
| 51 } | 55 TransformLogger get logger => _aggregate.logger; |
| 52 | 56 |
| 53 Transform._(TransformNode node) | 57 Transform._(this._aggregate, this.primaryInput); |
| 54 : _node = node, | |
| 55 super(node); | |
| 56 | 58 |
| 57 /// Gets the asset for an input [id]. | 59 /// Gets the asset for an input [id]. |
| 58 /// | 60 /// |
| 59 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 61 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 60 Future<Asset> getInput(AssetId id) { | 62 Future<Asset> getInput(AssetId id) => _aggregate.getInput(id); |
| 61 if (id == _node.primary.id) return syncFuture(() => primaryInput); | |
| 62 return _node.getInput(id); | |
| 63 } | |
| 64 | 63 |
| 65 /// A convenience method to the contents of the input with [id] as a string. | 64 /// A convenience method to the contents of the input with [id] as a string. |
| 66 /// | 65 /// |
| 67 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. | 66 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
| 68 /// | 67 /// |
| 69 /// If the asset was created from a [String] the original string is always | 68 /// If the asset was created from a [String] the original string is always |
| 70 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 69 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 71 /// asset is decoded using [encoding], which defaults to [UTF8]. | 70 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 72 /// | 71 /// |
| 73 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 72 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 74 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 73 Future<String> readInputAsString(AssetId id, {Encoding encoding}) => |
| 75 if (encoding == null) encoding = UTF8; | 74 _aggregate.readInputAsString(id, encoding: encoding); |
| 76 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | |
| 77 } | |
| 78 | 75 |
| 79 /// A convenience method to the contents of the input with [id]. | 76 /// A convenience method to the contents of the input with [id]. |
| 80 /// | 77 /// |
| 81 /// This is equivalent to calling [getInput] followed by [Asset.read]. | 78 /// This is equivalent to calling [getInput] followed by [Asset.read]. |
| 82 /// | 79 /// |
| 83 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 80 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| 84 /// | 81 /// |
| 85 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 82 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 86 Stream<List<int>> readInput(AssetId id) => | 83 Stream<List<int>> readInput(AssetId id) => _aggregate.readInput(id); |
| 87 futureStream(getInput(id).then((input) => input.read())); | |
| 88 | 84 |
| 89 /// A convenience method to return whether or not an asset exists. | 85 /// A convenience method to return whether or not an asset exists. |
| 90 /// | 86 /// |
| 91 /// This is equivalent to calling [getInput] and catching an | 87 /// This is equivalent to calling [getInput] and catching an |
| 92 /// [AssetNotFoundException]. | 88 /// [AssetNotFoundException]. |
| 93 Future<bool> hasInput(AssetId id) { | 89 Future<bool> hasInput(AssetId id) => _aggregate.hasInput(id); |
| 94 return getInput(id).then((_) => true).catchError((error) { | |
| 95 if (error is AssetNotFoundException && error.id == id) return false; | |
| 96 throw error; | |
| 97 }); | |
| 98 } | |
| 99 | 90 |
| 100 /// Stores [output] as the output created by this transformation. | 91 /// Stores [output] as the output created by this transformation. |
| 101 /// | 92 /// |
| 102 /// A transformation can output as many assets as it wants. | 93 /// A transformation can output as many assets as it wants. |
| 103 void addOutput(Asset output) { | 94 void addOutput(Asset output) => _aggregate.addOutput(output); |
| 104 // TODO(rnystrom): This should immediately throw if an output with that ID | 95 |
| 105 // has already been created by this transformer. | 96 /// Consume the primary input so that it doesn't get processed by future |
| 106 _outputs.add(output); | 97 /// phases or emitted once processing has finished. |
| 107 } | 98 /// |
| 99 /// Normally the primary input will automatically be forwarded unless the |
| 100 /// transformer overwrites it by emitting an input with the same id. This |
| 101 /// allows the transformer to tell barback not to forward the primary input |
| 102 /// even if it's not overwritten. |
| 103 void consumePrimary() => _aggregate.consumePrimary(primaryInput.id); |
| 108 } | 104 } |
| 109 | |
| 110 /// The controller for [Transform]. | |
| 111 class TransformController extends BaseTransformController { | |
| 112 Transform get transform => super.transform; | |
| 113 | |
| 114 /// The set of assets that the transformer has emitted. | |
| 115 AssetSet get outputs => transform._outputs; | |
| 116 | |
| 117 TransformController(TransformNode node) | |
| 118 : super(new Transform._(node)); | |
| 119 } | |
| OLD | NEW |