OLD | NEW |
| (Empty) |
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 | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library barback.transformer.transform; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:convert'; | |
9 | |
10 import '../asset/asset.dart'; | |
11 import '../asset/asset_id.dart'; | |
12 import '../errors.dart'; | |
13 import 'aggregate_transform.dart'; | |
14 import 'transform_logger.dart'; | |
15 | |
16 /// Creates a new [Transform] wrapping an [AggregateTransform]. | |
17 /// | |
18 /// Although barback internally works in terms of [AggregateTransformer]s, most | |
19 /// transformers only work on individual primary inputs in isolation. We want to | |
20 /// allow those transformers to implement the more user-friendly [Transformer] | |
21 /// interface which takes the more user-friendly [Transform] object. This method | |
22 /// wraps the more general [AggregateTransform] to return a [Transform] instead. | |
23 Future<Transform> newTransform(AggregateTransform aggregate) { | |
24 // A wrapped [Transformer] will assign each primary input a unique transform | |
25 // key, so we can safely get the first asset emitted. We don't want to wait | |
26 // for the stream to close, since that requires barback to prove that no more | |
27 // new assets will be generated. | |
28 return aggregate.primaryInputs.first.then((primaryInput) => | |
29 new Transform._(aggregate, primaryInput)); | |
30 } | |
31 | |
32 /// While a [Transformer] represents a *kind* of transformation, this defines | |
33 /// one specific usage of it on a set of files. | |
34 /// | |
35 /// This ephemeral object exists only during an actual transform application to | |
36 /// facilitate communication between the [Transformer] and the code hosting | |
37 /// the transformation. It lets the [Transformer] access inputs and generate | |
38 /// outputs. | |
39 class Transform { | |
40 /// The underlying aggregate transform. | |
41 final AggregateTransform _aggregate; | |
42 | |
43 /// Gets the primary input asset. | |
44 /// | |
45 /// While a transformation can use multiple input assets, one must be a | |
46 /// special "primary" asset. This will be the "entrypoint" or "main" input | |
47 /// file for a transformation. | |
48 /// | |
49 /// For example, with a dart2js transform, the primary input would be the | |
50 /// entrypoint Dart file. All of the other Dart files that that imports | |
51 /// would be secondary inputs. | |
52 final Asset primaryInput; | |
53 | |
54 /// A logger so that the [Transformer] can report build details. | |
55 TransformLogger get logger => _aggregate.logger; | |
56 | |
57 Transform._(this._aggregate, this.primaryInput); | |
58 | |
59 /// Gets the asset for an input [id]. | |
60 /// | |
61 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | |
62 Future<Asset> getInput(AssetId id) => _aggregate.getInput(id); | |
63 | |
64 /// A convenience method to the contents of the input with [id] as a string. | |
65 /// | |
66 /// This is equivalent to calling [getInput] followed by [Asset.readAsString]. | |
67 /// | |
68 /// If the asset was created from a [String] the original string is always | |
69 /// returned and [encoding] is ignored. Otherwise, the binary data of the | |
70 /// asset is decoded using [encoding], which defaults to [UTF8]. | |
71 /// | |
72 /// If an input with [id] cannot be found, throws an [AssetNotFoundException]. | |
73 Future<String> readInputAsString(AssetId id, {Encoding encoding}) => | |
74 _aggregate.readInputAsString(id, encoding: encoding); | |
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) => _aggregate.readInput(id); | |
84 | |
85 /// A convenience method to return whether or not an asset exists. | |
86 /// | |
87 /// This is equivalent to calling [getInput] and catching an | |
88 /// [AssetNotFoundException]. | |
89 Future<bool> hasInput(AssetId id) => _aggregate.hasInput(id); | |
90 | |
91 /// Stores [output] as the output created by this transformation. | |
92 /// | |
93 /// A transformation can output as many assets as it wants. | |
94 void addOutput(Asset output) => _aggregate.addOutput(output); | |
95 | |
96 /// Consume the primary input so that it doesn't get processed by future | |
97 /// phases or emitted once processing has finished. | |
98 /// | |
99 /// Normally the primary input will automatically be forwarded unless the | |
100 /// transformer overwrites it by emitting an input with the same id. This | |
101 /// allows the transformer to tell barback not to forward the primary input | |
102 /// even if it's not overwritten. | |
103 void consumePrimary() => _aggregate.consumePrimary(primaryInput.id); | |
104 } | |
OLD | NEW |