| OLD | NEW |
| 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.asset_graph; | 5 library barback.asset_graph; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'asset.dart'; | 10 import 'asset.dart'; |
| 11 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
| 12 import 'asset_provider.dart'; | 12 import 'asset_provider.dart'; |
| 13 import 'errors.dart'; | 13 import 'errors.dart'; |
| 14 import 'change_batch.dart'; | 14 import 'change_batch.dart'; |
| 15 import 'phase.dart'; | 15 import 'phase.dart'; |
| 16 import 'transformer.dart'; | 16 import 'transformer.dart'; |
| 17 | 17 |
| 18 /// The main build dependency manager. | 18 /// The main build dependency manager. |
| 19 /// | 19 /// |
| 20 /// For any given input file, it can tell which output files are affected by | 20 /// For any given input file, it can tell which output files are affected by |
| 21 /// it, and vice versa. | 21 /// it, and vice versa. |
| 22 class AssetGraph { | 22 class AssetGraph { |
| 23 final AssetProvider _provider; | 23 final AssetProvider _provider; |
| 24 | 24 |
| 25 final _phases = <Phase>[]; | 25 final _phases = <Phase>[]; |
| 26 | 26 |
| 27 Stream<ProcessResult> get results => _resultsController.stream; | 27 Stream<BuildResult> get results => _resultsController.stream; |
| 28 final _resultsController = new StreamController<ProcessResult>.broadcast(); | 28 final _resultsController = new StreamController<BuildResult>.broadcast(); |
| 29 | 29 |
| 30 /// A future that completes when the currently running build process finishes. | 30 /// A future that completes when the currently running build process finishes. |
| 31 /// | 31 /// |
| 32 /// If no build it in progress, is `null`. | 32 /// If no build it in progress, is `null`. |
| 33 Future _processDone; | 33 Future _processDone; |
| 34 | 34 |
| 35 ChangeBatch _sourceChanges; | 35 ChangeBatch _sourceChanges; |
| 36 | 36 |
| 37 /// Creates a new [AssetGraph]. | 37 /// Creates a new [AssetGraph]. |
| 38 /// | 38 /// |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 /// Removes [removed] from the graph's known set of source assets. | 115 /// Removes [removed] from the graph's known set of source assets. |
| 116 void removeSources(Iterable<AssetId> removed) { | 116 void removeSources(Iterable<AssetId> removed) { |
| 117 if (_sourceChanges == null) _sourceChanges = new ChangeBatch(); | 117 if (_sourceChanges == null) _sourceChanges = new ChangeBatch(); |
| 118 _sourceChanges.remove(removed); | 118 _sourceChanges.remove(removed); |
| 119 | 119 |
| 120 _waitForProcess(); | 120 _waitForProcess(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 /// Reports a process result with the given error then throws it. | 123 /// Reports a process result with the given error then throws it. |
| 124 void reportError(error) { | 124 void reportError(error) { |
| 125 _resultsController.add(new ProcessResult(error)); | 125 _resultsController.add(new BuildResult(error)); |
| 126 } | 126 } |
| 127 | 127 |
| 128 /// Starts the build process asynchronously if there is work to be done. | 128 /// Starts the build process asynchronously if there is work to be done. |
| 129 /// | 129 /// |
| 130 /// Returns a future that completes with the background processing is done. | 130 /// Returns a future that completes with the background processing is done. |
| 131 /// If there is no work to do, returns a future that completes immediately. | 131 /// If there is no work to do, returns a future that completes immediately. |
| 132 /// All errors that occur during processing will be caught (and routed to the | 132 /// All errors that occur during processing will be caught (and routed to the |
| 133 /// [results] stream) before they get to the returned future, so it is safe | 133 /// [results] stream) before they get to the returned future, so it is safe |
| 134 /// to discard it. | 134 /// to discard it. |
| 135 Future _waitForProcess() { | 135 Future _waitForProcess() { |
| 136 if (_processDone != null) return _processDone; | 136 if (_processDone != null) return _processDone; |
| 137 return _processDone = _process().catchError((error) { | 137 return _processDone = _process().then((_) { |
| 138 // Report the build completion. |
| 139 // TODO(rnystrom): Put some useful data in here. |
| 140 _resultsController.add(new BuildResult()); |
| 141 }).catchError((error) { |
| 138 // If we get here, it's an unexpected error. Runtime errors like missing | 142 // If we get here, it's an unexpected error. Runtime errors like missing |
| 139 // assets should be handled earlier. Errors from transformers or other | 143 // assets should be handled earlier. Errors from transformers or other |
| 140 // external code that barback calls into should be caught at that API | 144 // external code that barback calls into should be caught at that API |
| 141 // boundary. | 145 // boundary. |
| 142 // | 146 // |
| 143 // On the off chance we get here, pipe the error to the results stream | 147 // On the off chance we get here, pipe the error to the results stream |
| 144 // as an error. That will let applications handle it without it appearing | 148 // as an error. That will let applications handle it without it appearing |
| 145 // in the same path as "normal" errors that get reported. | 149 // in the same path as "normal" errors that get reported. |
| 146 _resultsController.addError(error); | 150 _resultsController.addError(error); |
| 147 }).whenComplete(() { | 151 }).whenComplete(() { |
| 148 _processDone = null; | 152 _processDone = null; |
| 149 // Report the build completion. | |
| 150 // TODO(rnystrom): Put some useful data in here. | |
| 151 _resultsController.add(new ProcessResult()); | |
| 152 }); | 153 }); |
| 153 } | 154 } |
| 154 | 155 |
| 155 /// Starts the background processing. | 156 /// Starts the background processing. |
| 156 /// | 157 /// |
| 157 /// Returns a future that completes when all assets have been processed. | 158 /// Returns a future that completes when all assets have been processed. |
| 158 Future _process() { | 159 Future _process() { |
| 159 return _processSourceChanges().then((_) { | 160 return _processSourceChanges().then((_) { |
| 160 // Find the first phase that has work to do and do it. | 161 // Find the first phase that has work to do and do it. |
| 161 var future; | 162 var future; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 211 |
| 211 return Future.wait(futures).then((_) { | 212 return Future.wait(futures).then((_) { |
| 212 _phases.first.updateInputs(updated, changes.removed); | 213 _phases.first.updateInputs(updated, changes.removed); |
| 213 }); | 214 }); |
| 214 }); | 215 }); |
| 215 } | 216 } |
| 216 } | 217 } |
| 217 | 218 |
| 218 /// Used to report build results back from the asynchronous build process | 219 /// Used to report build results back from the asynchronous build process |
| 219 /// running in the background. | 220 /// running in the background. |
| 220 class ProcessResult { | 221 class BuildResult { |
| 221 /// The error that occurred, or `null` if the result is not an error. | 222 /// The error that occurred, or `null` if the result is not an error. |
| 222 final error; | 223 final error; |
| 223 | 224 |
| 224 ProcessResult([this.error]); | 225 /// `true` if this result is for a successful build. |
| 226 bool get succeeded => error == null; |
| 227 |
| 228 BuildResult([this.error]); |
| 225 } | 229 } |
| OLD | NEW |