OLD | NEW |
---|---|
1 // Copyright (c) 2013, 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 | |
10 import 'package:source_maps/span.dart'; | |
11 | |
12 import 'asset.dart'; | 7 import 'asset.dart'; |
13 import 'asset_id.dart'; | |
14 import 'asset_node.dart'; | |
15 import 'asset_set.dart'; | 8 import 'asset_set.dart'; |
16 import 'errors.dart'; | 9 import 'base_transform.dart'; |
17 import 'log.dart'; | |
18 import 'transform_logger.dart'; | 10 import 'transform_logger.dart'; |
19 import 'transform_node.dart'; | 11 import 'transform_node.dart'; |
20 import 'utils.dart'; | |
21 | |
22 typedef void LogFunction(AssetId asset, LogLevel level, String message, | |
23 Span span); | |
24 | |
25 /// Creates a [Transform] by forwarding to the private constructor. | |
26 /// | |
27 /// Lets [TransformNode] create [Transforms] without giving a [Transform] | |
28 /// itself a public constructor, which would be visible to external users. | |
29 /// Unlike the [Transform] class, this function is not exported by barback.dart. | |
30 Transform createTransform(TransformNode node, AssetSet outputs, | |
31 LogFunction logFunction) => | |
32 new Transform._(node, outputs, logFunction); | |
33 | 12 |
34 /// While a [Transformer] represents a *kind* of transformation, this defines | 13 /// While a [Transformer] represents a *kind* of transformation, this defines |
35 /// one specific usage of it on a set of files. | 14 /// one specific usage of it on a set of files. |
36 /// | 15 /// |
37 /// This ephemeral object exists only during an actual transform application to | 16 /// This ephemeral object exists only during an actual transform application to |
38 /// facilitate communication between the [Transformer] and the code hosting | 17 /// facilitate communication between the [Transformer] and the code hosting |
39 /// the transformation. It lets the [Transformer] access inputs and generate | 18 /// the transformation. It lets the [Transformer] access inputs and generate |
40 /// outputs. | 19 /// outputs. |
41 class Transform { | 20 class Transform extends BaseTransform { |
42 final TransformNode _node; | |
43 final TransformLogger _logger; | |
44 final AssetSet _outputs; | 21 final AssetSet _outputs; |
45 | 22 |
46 /// A logger so that the [Transformer] can report build details. | 23 Transform(TransformNode node, this._outputs, LogFunction logFunction) |
Bob Nystrom
2014/01/30 19:33:44
Transform is in barback's public API, but Transfor
nweiz
2014/01/31 03:43:27
In general I prefer exposing constructors that are
| |
47 TransformLogger get logger => _logger; | 24 : super(node, logFunction); |
48 | |
49 /// Gets the primary input asset. | |
50 /// | |
51 /// While a transformation can use multiple input assets, one must be a | |
52 /// special "primary" asset. This will be the "entrypoint" or "main" input | |
53 /// file for a transformation. | |
54 /// | |
55 /// For example, with a dart2js transform, the primary input would be the | |
56 /// entrypoint Dart file. All of the other Dart files that that imports | |
57 /// would be secondary inputs. | |
58 /// | |
59 /// This method may fail at runtime if called asynchronously after the | |
60 /// transform begins running. The primary input may become unavailable while | |
61 /// this transformer is running due to asset changes earlier in the graph. | |
62 /// You can ignore the error if this happens: the transformer will be re-run | |
63 /// automatically for you. | |
64 Asset get primaryInput { | |
65 if (_node.primary.state != AssetState.AVAILABLE) { | |
66 throw new AssetNotFoundException(_node.primary.id); | |
67 } | |
68 | |
69 return _node.primary.asset; | |
70 } | |
71 | |
72 Transform._(this._node, this._outputs, LogFunction logFunction) | |
73 : _logger = new TransformLogger(logFunction); | |
74 | |
75 /// Gets the asset for an input [id]. | |
76 /// | |
77 /// If an input with that ID cannot be found, throws an | |
78 /// [AssetNotFoundException]. | |
79 Future<Asset> getInput(AssetId id) => _node.getInput(id); | |
80 | |
81 /// A convenience method to the contents of the input with [id] as a string. | |
82 /// | |
83 /// This is equivalent to calling `getInput()` followed by `readAsString()`. | |
84 /// | |
85 /// If the asset was created from a [String] the original string is always | |
86 /// returned and [encoding] is ignored. Otherwise, the binary data of the | |
87 /// asset is decoded using [encoding], which defaults to [UTF8]. | |
88 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | |
89 if (encoding == null) encoding = UTF8; | |
90 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | |
91 } | |
92 | |
93 /// A convenience method to the contents of the input with [id]. | |
94 /// | |
95 /// This is equivalent to calling `getInput()` followed by `read()`. | |
96 /// | |
97 /// If the asset was created from a [String], this returns its UTF-8 encoding. | |
98 Stream<List<int>> readInput(AssetId id) => | |
99 futureStream(getInput(id).then((input) => input.read())); | |
100 | 25 |
101 /// Stores [output] as the output created by this transformation. | 26 /// Stores [output] as the output created by this transformation. |
102 /// | 27 /// |
103 /// A transformation can output as many assets as it wants. | 28 /// A transformation can output as many assets as it wants. |
104 void addOutput(Asset output) { | 29 void addOutput(Asset output) { |
105 // TODO(rnystrom): This should immediately throw if an output with that ID | 30 // TODO(rnystrom): This should immediately throw if an output with that ID |
106 // has already been created by this transformer. | 31 // has already been created by this transformer. |
107 _outputs.add(output); | 32 _outputs.add(output); |
108 } | 33 } |
109 } | 34 } |
OLD | NEW |