Chromium Code Reviews| 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 import 'dart:convert'; | |
| 8 | 9 |
| 9 import 'asset.dart'; | 10 import 'asset.dart'; |
| 10 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
| 11 import 'asset_node.dart'; | 12 import 'asset_node.dart'; |
| 12 import 'asset_set.dart'; | 13 import 'asset_set.dart'; |
| 13 import 'errors.dart'; | 14 import 'errors.dart'; |
| 14 import 'transform_logger.dart'; | 15 import 'transform_logger.dart'; |
| 15 import 'transform_node.dart'; | 16 import 'transform_node.dart'; |
| 16 | 17 |
| 17 /// Creates a [Transform] by forwarding to the private constructor. | 18 /// Creates a [Transform] by forwarding to the private constructor. |
| 18 /// | 19 /// |
| 19 /// Lets [TransformNode] create [Transforms] without giving a [Transform] | 20 /// Lets [TransformNode] create [Transforms] without giving a [Transform] |
| 20 /// itself a public constructor, which would be visible to external users. | 21 /// itself a public constructor, which would be visible to external users. |
| 21 /// Unlike the [Transform] class, this function is not exported by barback.dart. | 22 /// Unlike the [Transform] class, this function is not exported by barback.dart. |
| 22 Transform createTransform(TransformNode node, AssetSet outputs) => | 23 Transform createTransform(TransformNode node, AssetSet outputs) => |
| 23 new Transform._(node, outputs); | 24 new Transform._(node, outputs); |
| 24 | 25 |
| 25 /// While a [Transformer] represents a *kind* of transformation, this defines | 26 /// While a [Transformer] represents a *kind* of transformation, this defines |
| 26 /// one specific usage of it on a set of files. | 27 /// one specific usage of it on a set of files. |
| 27 /// | 28 /// |
| 28 /// This ephemeral object exists only during an actual transform application to | 29 /// This ephemeral object exists only during an actual transform application to |
| 29 /// facilitate communication between the [Transformer] and the code hosting | 30 /// facilitate communication between the [Transformer] and the code hosting |
| 30 /// the transformation. It lets the [Transformer] access inputs and generate | 31 /// the transformation. It lets the [Transformer] access inputs and generate |
| 31 /// outputs. | 32 /// outputs. |
| 32 class Transform { | 33 class Transform { |
| 33 final TransformNode _node; | 34 final TransformNode _node; |
| 34 | 35 |
| 35 final AssetSet _outputs; | 36 final AssetSet _outputs; |
| 36 | 37 |
| 37 /// Gets the ID of the primary input for this transformation. | 38 /// A logger so that the [Transformer] can report build details. |
| 39 TransformLogger get logger => _logger; | |
| 40 | |
| 41 /// Gets the primary input asset. | |
| 38 /// | 42 /// |
| 39 /// While a transformation can use multiple input assets, one must be a | 43 /// While a transformation can use multiple input assets, one must be a |
| 40 /// special "primary" asset. This will be the "entrypoint" or "main" input | 44 /// special "primary" asset. This will be the "entrypoint" or "main" input |
| 41 /// file for a transformation. | 45 /// file for a transformation. |
| 42 /// | 46 /// |
| 43 /// For example, with a dart2js transform, the primary input would be the | 47 /// 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 | 48 /// entrypoint Dart file. All of the other Dart files that that imports |
| 45 /// would be secondary inputs. | 49 /// would be secondary inputs. |
| 46 AssetId get primaryId => _node.primary.id; | 50 Asset get primaryInput { |
| 47 | 51 assert(_node.primary.state == AssetState.AVAILABLE); |
|
nweiz
2013/08/28 19:29:43
This shouldn't be an assertion. It's expected to f
Bob Nystrom
2013/08/28 20:51:27
Done.
| |
| 48 /// A logger so that the [Transformer] can report build details. | 52 return _node.primary.asset; |
| 49 TransformLogger get logger => _logger; | 53 } |
| 50 | |
| 51 /// Gets the asset for the primary input. | |
| 52 Future<Asset> get primaryInput => getInput(primaryId); | |
| 53 | 54 |
| 54 Transform._(this._node, this._outputs); | 55 Transform._(this._node, this._outputs); |
| 55 | 56 |
| 56 /// Gets the asset for an input [id]. | 57 /// Gets the asset for an input [id]. |
| 57 /// | 58 /// |
| 58 /// If an input with that ID cannot be found, throws an | 59 /// If an input with that ID cannot be found, throws an |
| 59 /// [AssetNotFoundException]. | 60 /// [AssetNotFoundException]. |
| 60 Future<Asset> getInput(AssetId id) => _node.getInput(id); | 61 Future<Asset> getInput(AssetId id) => _node.getInput(id); |
| 61 | 62 |
| 63 /// A convenience method to the contents of the input with [id] as a string. | |
| 64 /// | |
| 65 /// This is equivalent to `getInput().readAsString()`. | |
|
nweiz
2013/08/28 19:29:43
`getInput().readAsString()` is a little confusing,
Bob Nystrom
2013/08/28 20:51:27
Reworded a bit. I didn't want to do the full async
| |
| 66 /// | |
| 67 /// If the asset was created from a [String] the original string is always | |
| 68 /// returned and [encoding] is ignored. Otherwise, the binary data of the | |
| 69 /// asset is decoded using [encoding], which defaults to [UTF8]. | |
| 70 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | |
| 71 if (encoding == null) encoding = UTF8; | |
|
Siggi Cherem (dart-lang)
2013/08/28 00:12:30
why not use "encoding: UTF8" as default value abov
Bob Nystrom
2013/08/28 19:11:34
If you do that, passing an explicit null will over
| |
| 72 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | |
| 73 } | |
| 74 | |
| 75 /// A convenience method to the contents of the input with [id]. | |
| 76 /// | |
| 77 /// This is equivalent to `getInput().read()`. | |
| 78 /// | |
| 79 /// If the asset was created from a [String], this returns its UTF-8 encoding. | |
| 80 Future<List<int>> readInput(AssetId id) => | |
| 81 getInput(id).then((input) => input.read()); | |
|
nweiz
2013/08/28 19:29:43
[Asset.read] returns a Stream<List<int>>, not a Fu
Bob Nystrom
2013/08/28 20:51:27
Done.
| |
| 82 | |
| 62 /// Stores [output] as the output created by this transformation. | 83 /// Stores [output] as the output created by this transformation. |
| 63 /// | 84 /// |
| 64 /// A transformation can output as many assets as it wants. | 85 /// A transformation can output as many assets as it wants. |
| 65 void addOutput(Asset output) { | 86 void addOutput(Asset output) { |
| 66 // TODO(rnystrom): This should immediately throw if an output with that ID | 87 // TODO(rnystrom): This should immediately throw if an output with that ID |
| 67 // has already been created by this transformer. | 88 // has already been created by this transformer. |
| 68 _outputs.add(output); | 89 _outputs.add(output); |
| 69 } | 90 } |
| 70 } | 91 } |
| 71 | 92 |
| 72 // TODO(sigmund,rnystrom): create a separate logger for each Transfom. | 93 // TODO(sigmund,rnystrom): create a separate logger for each Transfom. |
| 73 final TransformLogger _logger = new TransformLogger(true); | 94 final TransformLogger _logger = new TransformLogger(true); |
| OLD | NEW |