| 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.base_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 'asset.dart'; | 10 import 'asset.dart'; |
| 11 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
| 12 import 'asset_node.dart'; | 12 import 'asset_node.dart'; |
| 13 import 'errors.dart'; | 13 import 'errors.dart'; |
| 14 import 'log.dart'; | 14 import 'log.dart'; |
| 15 import 'transform_logger.dart'; | 15 import 'transform_logger.dart'; |
| 16 import 'transform_node.dart'; | 16 import 'transform_node.dart'; |
| 17 import 'utils.dart'; | 17 import 'utils.dart'; |
| 18 | 18 |
| 19 /// The base class for the ephemeral transform objects that are passed to | 19 /// The base class for the ephemeral transform objects that are passed to |
| 20 /// transformers. | 20 /// transformers. |
| 21 /// | 21 /// |
| 22 /// This class provides the transformers with inputs, but its up to the | 22 /// This class provides the transformers with inputs, but its up to the |
| 23 /// subclasses to provide a means of emitting outputs. | 23 /// subclasses to provide a means of emitting outputs. |
| 24 abstract class BaseTransform { | 24 abstract class BaseTransform { |
| 25 final TransformNode _node; | 25 final TransformNode _node; |
| 26 | 26 |
| 27 /// Whether the primary input should be consumed. |
| 28 /// |
| 29 /// This is exposed via [BaseTransformController]. |
| 30 bool _consumePrimary = false; |
| 31 |
| 27 /// The controller for the stream of log entries emitted by the transformer. | 32 /// The controller for the stream of log entries emitted by the transformer. |
| 28 /// | 33 /// |
| 29 /// This is exposed via [BaseTransformController]. | 34 /// This is exposed via [BaseTransformController]. |
| 30 /// | 35 /// |
| 31 /// This is synchronous because error logs can cause the transform to fail, so | 36 /// This is synchronous because error logs can cause the transform to fail, so |
| 32 /// we need to ensure that their processing isn't delayed until after the | 37 /// we need to ensure that their processing isn't delayed until after the |
| 33 /// transform or build has finished. | 38 /// transform or build has finished. |
| 34 final _onLogController = new StreamController<LogEntry>.broadcast(sync: true); | 39 final _onLogController = new StreamController<LogEntry>.broadcast(sync: true); |
| 35 | 40 |
| 36 /// A logger so that the [Transformer] can report build details. | 41 /// A logger so that the [Transformer] can report build details. |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | 92 return getInput(id).then((input) => input.readAsString(encoding: encoding)); |
| 88 } | 93 } |
| 89 | 94 |
| 90 /// A convenience method to the contents of the input with [id]. | 95 /// A convenience method to the contents of the input with [id]. |
| 91 /// | 96 /// |
| 92 /// This is equivalent to calling `getInput()` followed by `read()`. | 97 /// This is equivalent to calling `getInput()` followed by `read()`. |
| 93 /// | 98 /// |
| 94 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 99 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| 95 Stream<List<int>> readInput(AssetId id) => | 100 Stream<List<int>> readInput(AssetId id) => |
| 96 futureStream(getInput(id).then((input) => input.read())); | 101 futureStream(getInput(id).then((input) => input.read())); |
| 102 |
| 103 /// Consume the primary input so that it doesn't get processed by future |
| 104 /// phases or emitted once processing has finished. |
| 105 /// |
| 106 /// Normally the primary input will automatically be forwarded unless the |
| 107 /// transformer overwrites it by emitting an input with the same id. This |
| 108 /// allows the transformer to tell barback not to forward the primary input |
| 109 /// even if it's not overwritten. |
| 110 void consumePrimary() { |
| 111 _consumePrimary = true; |
| 112 } |
| 97 } | 113 } |
| 98 | 114 |
| 99 /// The base class for controllers of subclasses of [BaseTransform]. | 115 /// The base class for controllers of subclasses of [BaseTransform]. |
| 100 /// | 116 /// |
| 101 /// Controllers are used so that [TransformNode]s can get values from a | 117 /// Controllers are used so that [TransformNode]s can get values from a |
| 102 /// [BaseTransform] without exposing getters in the public API. | 118 /// [BaseTransform] without exposing getters in the public API. |
| 103 abstract class BaseTransformController { | 119 abstract class BaseTransformController { |
| 104 /// The [BaseTransform] controlled by this controller. | 120 /// The [BaseTransform] controlled by this controller. |
| 105 final BaseTransform transform; | 121 final BaseTransform transform; |
| 106 | 122 |
| 123 /// Whether the primary input should be consumed. |
| 124 bool get consumePrimary => transform._consumePrimary; |
| 125 |
| 107 /// The stream of log entries emitted by the transformer during a run. | 126 /// The stream of log entries emitted by the transformer during a run. |
| 108 Stream<LogEntry> get onLog => transform._onLogController.stream; | 127 Stream<LogEntry> get onLog => transform._onLogController.stream; |
| 109 | 128 |
| 110 BaseTransformController(this.transform); | 129 BaseTransformController(this.transform); |
| 111 | 130 |
| 112 /// Notifies the [BaseTransform] that the transformation has finished being | 131 /// Notifies the [BaseTransform] that the transformation has finished being |
| 113 /// applied. | 132 /// applied. |
| 114 /// | 133 /// |
| 115 /// This will close any streams and release any resources that were allocated | 134 /// This will close any streams and release any resources that were allocated |
| 116 /// for the duration of the transformation. | 135 /// for the duration of the transformation. |
| 117 void close() { | 136 void close() { |
| 118 transform._onLogController.close(); | 137 transform._onLogController.close(); |
| 119 } | 138 } |
| 120 } | 139 } |
| OLD | NEW |