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.transform; | 5 library barback.transform; |
6 | 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
7 import 'asset.dart'; | 10 import 'asset.dart'; |
| 11 import 'asset_id.dart'; |
8 import 'asset_set.dart'; | 12 import 'asset_set.dart'; |
9 import 'base_transform.dart'; | 13 import 'base_transform.dart'; |
| 14 import 'errors.dart'; |
10 import 'transform_node.dart'; | 15 import 'transform_node.dart'; |
| 16 import 'utils.dart'; |
11 | 17 |
12 /// While a [Transformer] represents a *kind* of transformation, this defines | 18 /// While a [Transformer] represents a *kind* of transformation, this defines |
13 /// one specific usage of it on a set of files. | 19 /// one specific usage of it on a set of files. |
14 /// | 20 /// |
15 /// This ephemeral object exists only during an actual transform application to | 21 /// This ephemeral object exists only during an actual transform application to |
16 /// facilitate communication between the [Transformer] and the code hosting | 22 /// facilitate communication between the [Transformer] and the code hosting |
17 /// the transformation. It lets the [Transformer] access inputs and generate | 23 /// the transformation. It lets the [Transformer] access inputs and generate |
18 /// outputs. | 24 /// outputs. |
19 class Transform extends BaseTransform { | 25 class Transform extends BaseTransform { |
| 26 final TransformNode _node; |
| 27 |
20 final _outputs = new AssetSet(); | 28 final _outputs = new AssetSet(); |
21 | 29 |
| 30 /// Gets the primary input asset. |
| 31 /// |
| 32 /// While a transformation can use multiple input assets, one must be a |
| 33 /// special "primary" asset. This will be the "entrypoint" or "main" input |
| 34 /// file for a transformation. |
| 35 /// |
| 36 /// 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 |
| 38 /// would be secondary inputs. |
| 39 /// |
| 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 |
| 50 return _node.primary.asset; |
| 51 } |
| 52 |
22 Transform._(TransformNode node) | 53 Transform._(TransformNode node) |
23 : super(node); | 54 : _node = node, |
| 55 super(node); |
| 56 |
| 57 /// Gets the asset for an input [id]. |
| 58 /// |
| 59 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 60 Future<Asset> getInput(AssetId id) => _node.getInput(id); |
| 61 |
| 62 /// A convenience method to the contents of the input with [id] as a string. |
| 63 /// |
| 64 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. |
| 65 /// |
| 66 /// If the asset was created from a [String] the original string is always |
| 67 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 68 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 69 /// |
| 70 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 71 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
| 72 if (encoding == null) encoding = UTF8; |
| 73 return getInput(id).then((input) => input.readAsString(encoding: encoding)); |
| 74 } |
| 75 |
| 76 /// A convenience method to the contents of the input with [id]. |
| 77 /// |
| 78 /// This is equivalent to calling [getInput] followed by [Asset.read]. |
| 79 /// |
| 80 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| 81 /// |
| 82 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. |
| 83 Stream<List<int>> readInput(AssetId id) => |
| 84 futureStream(getInput(id).then((input) => input.read())); |
| 85 |
| 86 /// A convenience method to return whether or not an asset exists. |
| 87 /// |
| 88 /// This is equivalent to calling [getInput] and catching an |
| 89 /// [AssetNotFoundException]. |
| 90 Future<bool> hasInput(AssetId id) { |
| 91 return getInput(id).then((_) => true).catchError((error) { |
| 92 if (error is AssetNotFoundException && error.id == id) return false; |
| 93 throw error; |
| 94 }); |
| 95 } |
24 | 96 |
25 /// Stores [output] as the output created by this transformation. | 97 /// Stores [output] as the output created by this transformation. |
26 /// | 98 /// |
27 /// A transformation can output as many assets as it wants. | 99 /// A transformation can output as many assets as it wants. |
28 void addOutput(Asset output) { | 100 void addOutput(Asset output) { |
29 // TODO(rnystrom): This should immediately throw if an output with that ID | 101 // TODO(rnystrom): This should immediately throw if an output with that ID |
30 // has already been created by this transformer. | 102 // has already been created by this transformer. |
31 _outputs.add(output); | 103 _outputs.add(output); |
32 } | 104 } |
33 } | 105 } |
34 | 106 |
35 /// The controller for [Transform]. | 107 /// The controller for [Transform]. |
36 class TransformController extends BaseTransformController { | 108 class TransformController extends BaseTransformController { |
37 Transform get transform => super.transform; | 109 Transform get transform => super.transform; |
38 | 110 |
39 /// The set of assets that the transformer has emitted. | 111 /// The set of assets that the transformer has emitted. |
40 AssetSet get outputs => transform._outputs; | 112 AssetSet get outputs => transform._outputs; |
41 | 113 |
42 TransformController(TransformNode node) | 114 TransformController(TransformNode node) |
43 : super(new Transform._(node)); | 115 : super(new Transform._(node)); |
44 } | 116 } |
OLD | NEW |