| 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 'transform_logger.dart'; | 15 import 'transform_logger.dart'; |
| 15 import 'transform_node.dart'; | 16 import 'transform_node.dart'; |
| 16 import 'utils.dart'; | 17 import 'utils.dart'; |
| 17 | 18 |
| 18 /// 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 |
| 19 /// transformers. | 20 /// transformers. |
| 20 /// | 21 /// |
| 21 /// 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 |
| 22 /// subclasses to provide a means of emitting outputs. | 23 /// subclasses to provide a means of emitting outputs. |
| 23 abstract class BaseTransform { | 24 abstract class BaseTransform { |
| 24 final TransformNode _node; | 25 final TransformNode _node; |
| 25 final TransformLogger _logger; | 26 |
| 27 /// The controller for the stream of log entries emitted by the transformer. |
| 28 /// |
| 29 /// This is exposed via [BaseTransformController]. |
| 30 /// |
| 31 /// 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 |
| 33 /// transform or build has finished. |
| 34 final _onLogController = new StreamController<LogEntry>.broadcast(sync: true); |
| 26 | 35 |
| 27 /// A logger so that the [Transformer] can report build details. | 36 /// A logger so that the [Transformer] can report build details. |
| 28 TransformLogger get logger => _logger; | 37 TransformLogger get logger => _logger; |
| 38 TransformLogger _logger; |
| 29 | 39 |
| 30 /// Gets the primary input asset. | 40 /// Gets the primary input asset. |
| 31 /// | 41 /// |
| 32 /// While a transformation can use multiple input assets, one must be a | 42 /// While a transformation can use multiple input assets, one must be a |
| 33 /// special "primary" asset. This will be the "entrypoint" or "main" input | 43 /// special "primary" asset. This will be the "entrypoint" or "main" input |
| 34 /// file for a transformation. | 44 /// file for a transformation. |
| 35 /// | 45 /// |
| 36 /// For example, with a dart2js transform, the primary input would be the | 46 /// For example, with a dart2js transform, the primary input would be the |
| 37 /// entrypoint Dart file. All of the other Dart files that that imports | 47 /// entrypoint Dart file. All of the other Dart files that that imports |
| 38 /// would be secondary inputs. | 48 /// would be secondary inputs. |
| 39 /// | 49 /// |
| 40 /// This method may fail at runtime if called asynchronously after the | 50 /// This method may fail at runtime if called asynchronously after the |
| 41 /// transform begins running. The primary input may become unavailable while | 51 /// transform begins running. The primary input may become unavailable while |
| 42 /// this transformer is running due to asset changes earlier in the graph. | 52 /// this transformer is running due to asset changes earlier in the graph. |
| 43 /// You can ignore the error if this happens: the transformer will be re-run | 53 /// You can ignore the error if this happens: the transformer will be re-run |
| 44 /// automatically for you. | 54 /// automatically for you. |
| 45 Asset get primaryInput { | 55 Asset get primaryInput { |
| 46 if (_node.primary.state != AssetState.AVAILABLE) { | 56 if (_node.primary.state != AssetState.AVAILABLE) { |
| 47 throw new AssetNotFoundException(_node.primary.id); | 57 throw new AssetNotFoundException(_node.primary.id); |
| 48 } | 58 } |
| 49 | 59 |
| 50 return _node.primary.asset; | 60 return _node.primary.asset; |
| 51 } | 61 } |
| 52 | 62 |
| 53 BaseTransform(this._node, LogFunction logFunction) | 63 BaseTransform(this._node) { |
| 54 : _logger = new TransformLogger(logFunction); | 64 _logger = new TransformLogger((asset, level, message, span) { |
| 65 // If the log isn't already associated with an asset, use the primary. |
| 66 if (asset == null) asset = _node.primary.id; |
| 67 var entry = new LogEntry(_node.info, asset, level, message, span); |
| 68 _onLogController.add(entry); |
| 69 }); |
| 70 } |
| 55 | 71 |
| 56 /// Gets the asset for an input [id]. | 72 /// Gets the asset for an input [id]. |
| 57 /// | 73 /// |
| 58 /// If an input with that ID cannot be found, throws an | 74 /// If an input with that ID cannot be found, throws an |
| 59 /// [AssetNotFoundException]. | 75 /// [AssetNotFoundException]. |
| 60 Future<Asset> getInput(AssetId id) => _node.getInput(id); | 76 Future<Asset> getInput(AssetId id) => _node.getInput(id); |
| 61 | 77 |
| 62 /// A convenience method to the contents of the input with [id] as a string. | 78 /// A convenience method to the contents of the input with [id] as a string. |
| 63 /// | 79 /// |
| 64 /// This is equivalent to calling `getInput()` followed by `readAsString()`. | 80 /// This is equivalent to calling `getInput()` followed by `readAsString()`. |
| 65 /// | 81 /// |
| 66 /// If the asset was created from a [String] the original string is always | 82 /// If the asset was created from a [String] the original string is always |
| 67 /// returned and [encoding] is ignored. Otherwise, the binary data of the | 83 /// returned and [encoding] is ignored. Otherwise, the binary data of the |
| 68 /// asset is decoded using [encoding], which defaults to [UTF8]. | 84 /// asset is decoded using [encoding], which defaults to [UTF8]. |
| 69 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { | 85 Future<String> readInputAsString(AssetId id, {Encoding encoding}) { |
| 70 if (encoding == null) encoding = UTF8; | 86 if (encoding == null) encoding = UTF8; |
| 71 return getInput(id).then((input) => input.readAsString(encoding: encoding)); | 87 return getInput(id).then((input) => input.readAsString(encoding: encoding)); |
| 72 } | 88 } |
| 73 | 89 |
| 74 /// A convenience method to the contents of the input with [id]. | 90 /// A convenience method to the contents of the input with [id]. |
| 75 /// | 91 /// |
| 76 /// This is equivalent to calling `getInput()` followed by `read()`. | 92 /// This is equivalent to calling `getInput()` followed by `read()`. |
| 77 /// | 93 /// |
| 78 /// If the asset was created from a [String], this returns its UTF-8 encoding. | 94 /// If the asset was created from a [String], this returns its UTF-8 encoding. |
| 79 Stream<List<int>> readInput(AssetId id) => | 95 Stream<List<int>> readInput(AssetId id) => |
| 80 futureStream(getInput(id).then((input) => input.read())); | 96 futureStream(getInput(id).then((input) => input.read())); |
| 81 } | 97 } |
| 98 |
| 99 /// The base class for controllers of subclasses of [BaseTransform]. |
| 100 /// |
| 101 /// Controllers are used so that [TransformNode]s can get values from a |
| 102 /// [BaseTransform] without exposing getters in the public API. |
| 103 abstract class BaseTransformController { |
| 104 /// The [BaseTransform] controlled by this controller. |
| 105 final BaseTransform transform; |
| 106 |
| 107 /// The stream of log entries emitted by the transformer during a run. |
| 108 Stream<LogEntry> get onLog => transform._onLogController.stream; |
| 109 |
| 110 BaseTransformController(this.transform); |
| 111 |
| 112 /// Notifies the [BaseTransform] that the transformation has finished being |
| 113 /// applied. |
| 114 /// |
| 115 /// This will close any streams and release any resources that were allocated |
| 116 /// for the duration of the transformation. |
| 117 void close() { |
| 118 transform._onLogController.close(); |
| 119 } |
| 120 } |
| OLD | NEW |