Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(243)

Side by Side Diff: pkg/barback/lib/src/transform.dart

Issue 23543005: Make Transform a little more pleasant to use: (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise test code for handling primaryInput. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/barback/lib/src/utils.dart » ('j') | pkg/barback/test/transformer/mock.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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';
17 import 'utils.dart';
16 18
17 /// Creates a [Transform] by forwarding to the private constructor. 19 /// Creates a [Transform] by forwarding to the private constructor.
18 /// 20 ///
19 /// Lets [TransformNode] create [Transforms] without giving a [Transform] 21 /// Lets [TransformNode] create [Transforms] without giving a [Transform]
20 /// itself a public constructor, which would be visible to external users. 22 /// itself a public constructor, which would be visible to external users.
21 /// Unlike the [Transform] class, this function is not exported by barback.dart. 23 /// Unlike the [Transform] class, this function is not exported by barback.dart.
22 Transform createTransform(TransformNode node, AssetSet outputs) => 24 Transform createTransform(TransformNode node, AssetSet outputs) =>
23 new Transform._(node, outputs); 25 new Transform._(node, outputs);
24 26
25 /// While a [Transformer] represents a *kind* of transformation, this defines 27 /// While a [Transformer] represents a *kind* of transformation, this defines
26 /// one specific usage of it on a set of files. 28 /// one specific usage of it on a set of files.
27 /// 29 ///
28 /// This ephemeral object exists only during an actual transform application to 30 /// This ephemeral object exists only during an actual transform application to
29 /// facilitate communication between the [Transformer] and the code hosting 31 /// facilitate communication between the [Transformer] and the code hosting
30 /// the transformation. It lets the [Transformer] access inputs and generate 32 /// the transformation. It lets the [Transformer] access inputs and generate
31 /// outputs. 33 /// outputs.
32 class Transform { 34 class Transform {
33 final TransformNode _node; 35 final TransformNode _node;
34 36
35 final AssetSet _outputs; 37 final AssetSet _outputs;
36 38
37 /// Gets the ID of the primary input for this transformation. 39 /// A logger so that the [Transformer] can report build details.
40 TransformLogger get logger => _logger;
41
42 /// Gets the primary input asset.
38 /// 43 ///
39 /// While a transformation can use multiple input assets, one must be a 44 /// While a transformation can use multiple input assets, one must be a
40 /// special "primary" asset. This will be the "entrypoint" or "main" input 45 /// special "primary" asset. This will be the "entrypoint" or "main" input
41 /// file for a transformation. 46 /// file for a transformation.
42 /// 47 ///
43 /// For example, with a dart2js transform, the primary input would be the 48 /// 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 49 /// entrypoint Dart file. All of the other Dart files that that imports
45 /// would be secondary inputs. 50 /// would be secondary inputs.
46 AssetId get primaryId => _node.primary.id; 51 ///
52 /// This method may fail at runtime if called asynchronously after the
53 /// transform begins running. If the previous transform that generated this
54 /// primary input starts re-running while this transformer is in the middle
55 /// of running, it may be unavailable when this transformer later resumes.
nweiz 2013/08/28 21:15:00 This sentence is confusing and slightly inaccurate
Bob Nystrom 2013/08/28 22:38:24 Re-worded a bit.
56 ///
57 /// The intent is that this exception will unwind the transformer, which will
58 /// then be automatically re-run when the primary input becomes available
59 /// again, so you don't need to worry about this failing.
60 Asset get primaryInput {
61 if (_node.primary.state != AssetState.AVAILABLE) {
62 throw new AssetNotFoundException(_node.primary.id);
63 }
47 64
48 /// A logger so that the [Transformer] can report build details. 65 return _node.primary.asset;
49 TransformLogger get logger => _logger; 66 }
50
51 /// Gets the asset for the primary input.
52 Future<Asset> get primaryInput => getInput(primaryId);
53 67
54 Transform._(this._node, this._outputs); 68 Transform._(this._node, this._outputs);
55 69
56 /// Gets the asset for an input [id]. 70 /// Gets the asset for an input [id].
57 /// 71 ///
58 /// If an input with that ID cannot be found, throws an 72 /// If an input with that ID cannot be found, throws an
59 /// [AssetNotFoundException]. 73 /// [AssetNotFoundException].
60 Future<Asset> getInput(AssetId id) => _node.getInput(id); 74 Future<Asset> getInput(AssetId id) => _node.getInput(id);
61 75
76 /// A convenience method to the contents of the input with [id] as a string.
77 ///
78 /// This is equivalent to calling `getInput()` followed by `readAsString()`.
79 ///
80 /// If the asset was created from a [String] the original string is always
81 /// returned and [encoding] is ignored. Otherwise, the binary data of the
82 /// asset is decoded using [encoding], which defaults to [UTF8].
83 Future<String> readInputAsString(AssetId id, {Encoding encoding}) {
84 if (encoding == null) encoding = UTF8;
85 return getInput(id).then((input) => input.readAsString(encoding: encoding));
86 }
87
88 /// A convenience method to the contents of the input with [id].
89 ///
90 /// This is equivalent to calling `getInput()` followed by `read()`.
91 ///
92 /// If the asset was created from a [String], this returns its UTF-8 encoding.
93 Stream<List<int>> readInput(AssetId id) =>
94 futureStream(getInput(id).then((input) => input.read()));
95
62 /// Stores [output] as the output created by this transformation. 96 /// Stores [output] as the output created by this transformation.
63 /// 97 ///
64 /// A transformation can output as many assets as it wants. 98 /// A transformation can output as many assets as it wants.
65 void addOutput(Asset output) { 99 void addOutput(Asset output) {
66 // TODO(rnystrom): This should immediately throw if an output with that ID 100 // TODO(rnystrom): This should immediately throw if an output with that ID
67 // has already been created by this transformer. 101 // has already been created by this transformer.
68 _outputs.add(output); 102 _outputs.add(output);
69 } 103 }
70 } 104 }
71 105
72 // TODO(sigmund,rnystrom): create a separate logger for each Transfom. 106 // TODO(sigmund,rnystrom): create a separate logger for each Transfom.
73 final TransformLogger _logger = new TransformLogger(true); 107 final TransformLogger _logger = new TransformLogger(true);
OLDNEW
« no previous file with comments | « no previous file | pkg/barback/lib/src/utils.dart » ('j') | pkg/barback/test/transformer/mock.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698