Chromium Code Reviews| 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]. | |
|
Bob Nystrom
2014/05/05 23:41:56
It would be more helpful to document this in terms
nweiz
2014/05/06 22:46:40
Done.
| |
| 17 Future<Transform> newTransform(AggregateTransform aggregate) { | |
| 18 return aggregate.primaryInputs.first.then((primaryInput) => | |
| 19 new Transform._(aggregate, primaryInput)); | |
| 20 } | |
| 17 | 21 |
| 18 /// While a [Transformer] represents a *kind* of transformation, this defines | 22 /// While a [Transformer] represents a *kind* of transformation, this defines |
| 19 /// one specific usage of it on a set of files. | 23 /// one specific usage of it on a set of files. |
| 20 /// | 24 /// |
| 21 /// This ephemeral object exists only during an actual transform application to | 25 /// This ephemeral object exists only during an actual transform application to |
| 22 /// facilitate communication between the [Transformer] and the code hosting | 26 /// facilitate communication between the [Transformer] and the code hosting |
| 23 /// the transformation. It lets the [Transformer] access inputs and generate | 27 /// the transformation. It lets the [Transformer] access inputs and generate |
| 24 /// outputs. | 28 /// outputs. |
| 25 class Transform extends BaseTransform { | 29 class Transform { |
| 26 final TransformNode _node; | 30 final AggregateTransform _aggregate; |
|
Bob Nystrom
2014/05/05 23:41:56
Doc comment.
nweiz
2014/05/06 22:46:40
Done.
| |
| 27 | |
| 28 final _outputs = new AssetSet(); | |
| 29 | 31 |
| 30 /// Gets the primary input asset. | 32 /// Gets the primary input asset. |
| 31 /// | 33 /// |
| 32 /// While a transformation can use multiple input assets, one must be a | 34 /// While a transformation can use multiple input assets, one must be a |
| 33 /// special "primary" asset. This will be the "entrypoint" or "main" input | 35 /// special "primary" asset. This will be the "entrypoint" or "main" input |
| 34 /// file for a transformation. | 36 /// file for a transformation. |
| 35 /// | 37 /// |
| 36 /// For example, with a dart2js transform, the primary input would be the | 38 /// 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 | 39 /// entrypoint Dart file. All of the other Dart files that that imports |
| 38 /// would be secondary inputs. | 40 /// would be secondary inputs. |
| 39 /// | 41 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 | 42 |
| 50 return _node.primary.asset; | 43 /// A logger so that the [Transformer] can report build details. |
| 51 } | 44 TransformLogger get logger => _aggregate.logger; |
| 52 | 45 |
| 53 Transform._(TransformNode node) | 46 Transform._(this._aggregate, this.primaryInput); |
| 54 : _node = node, | |
| 55 super(node); | |
| 56 | 47 |
| 57 /// Gets the asset for an input [id]. | 48 /// Gets the asset for an input [id]. |
| 58 /// | 49 /// |
| 59 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 50 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 60 Future<Asset> getInput(AssetId id) { | 51 Future<Asset> getInput(AssetId id) => _aggregate.getInput(id); |
| 61 if (id == _node.primary.id) return syncFuture(() => primaryInput); | |
| 62 return _node.getInput(id); | |
| 63 } | |
| 64 | 52 |
| 65 /// A convenience method to the contents of the input with [id] as a string. | 53 /// A convenience method to the contents of the input with [id] as a string. |
| 66 /// | 54 /// |
| 67 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. | 55 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
| 68 /// | 56 /// |
| 69 /// If the asset was created from a [String] the original string is always | 57 /// 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 | 58 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 71 /// asset is decoded using [encoding], which defaults to [UTF8]. | 59 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 72 /// | 60 /// |
| 73 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 61 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 74 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 62 Future<String> readInputAsString(AssetId id, {Encoding encoding}) => |
| 75 if (encoding == null) encoding = UTF8; | 63 _aggregate.readInputAsString(id, encoding: encoding); |
| 76 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | |
| 77 } | |
| 78 | 64 |
| 79 /// A convenience method to the contents of the input with [id]. | 65 /// A convenience method to the contents of the input with [id]. |
| 80 /// | 66 /// |
| 81 /// This is equivalent to calling [getInput] followed by [Asset.read]. | 67 /// This is equivalent to calling [getInput] followed by [Asset.read]. |
| 82 /// | 68 /// |
| 83 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 69 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| 84 /// | 70 /// |
| 85 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | 71 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 86 Stream<List<int>> readInput(AssetId id) => | 72 Stream<List<int>> readInput(AssetId id) => _aggregate.readInput(id); |
| 87 futureStream(getInput(id).then((input) => input.read())); | |
| 88 | 73 |
| 89 /// A convenience method to return whether or not an asset exists. | 74 /// A convenience method to return whether or not an asset exists. |
| 90 /// | 75 /// |
| 91 /// This is equivalent to calling [getInput] and catching an | 76 /// This is equivalent to calling [getInput] and catching an |
| 92 /// [AssetNotFoundException]. | 77 /// [AssetNotFoundException]. |
| 93 Future<bool> hasInput(AssetId id) { | 78 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 | 79 |
| 100 /// Stores [output] as the output created by this transformation. | 80 /// Stores [output] as the output created by this transformation. |
| 101 /// | 81 /// |
| 102 /// A transformation can output as many assets as it wants. | 82 /// A transformation can output as many assets as it wants. |
| 103 void addOutput(Asset output) { | 83 void addOutput(Asset output) => _aggregate.addOutput(output); |
| 104 // TODO(rnystrom): This should immediately throw if an output with that ID | 84 |
| 105 // has already been created by this transformer. | 85 /// Consume the primary input so that it doesn't get processed by future |
| 106 _outputs.add(output); | 86 /// phases or emitted once processing has finished. |
| 107 } | 87 /// |
| 88 /// Normally the primary input will automatically be forwarded unless the | |
| 89 /// transformer overwrites it by emitting an input with the same id. This | |
| 90 /// allows the transformer to tell barback not to forward the primary input | |
| 91 /// even if it's not overwritten. | |
| 92 void consumePrimary() => _aggregate.consumePrimary(primaryInput.id); | |
| 108 } | 93 } |
| 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 |