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