| 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 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 for (var i = _phases.length - 1; i >= 0; i--) { | 87 for (var i = _phases.length - 1; i >= 0; i--) { |
| 88 var node = _phases[i].inputs[id]; | 88 var node = _phases[i].inputs[id]; |
| 89 if (node != null) { | 89 if (node != null) { |
| 90 // By the time we get here, the asset should have been built. | 90 // By the time we get here, the asset should have been built. |
| 91 assert(node.asset != null); | 91 assert(node.asset != null); |
| 92 return node.asset; | 92 return node.asset; |
| 93 } | 93 } |
| 94 } | 94 } |
| 95 | 95 |
| 96 // Couldn't find it. | 96 // Couldn't find it. |
| 97 var error = new AssetNotFoundException(id); | 97 throw new AssetNotFoundException(id); |
| 98 reportError(error); | |
| 99 throw error; | |
| 100 }); | 98 }); |
| 101 } | 99 } |
| 102 | 100 |
| 103 /// Adds [sources] to the graph's known set of source assets. | 101 /// Adds [sources] to the graph's known set of source assets. |
| 104 /// | 102 /// |
| 105 /// Begins applying any transforms that can consume any of the sources. If a | 103 /// Begins applying any transforms that can consume any of the sources. If a |
| 106 /// given source is already known, it is considered modified and all | 104 /// given source is already known, it is considered modified and all |
| 107 /// transforms that use it will be re-applied. | 105 /// transforms that use it will be re-applied. |
| 108 void updateSources(Iterable<AssetId> sources) { | 106 void updateSources(Iterable<AssetId> sources) { |
| 109 if (_sourceChanges == null) _sourceChanges = new ChangeBatch(); | 107 if (_sourceChanges == null) _sourceChanges = new ChangeBatch(); |
| 110 _sourceChanges.update(sources); | 108 _sourceChanges.update(sources); |
| 111 | 109 |
| 112 _waitForProcess(); | 110 _waitForProcess(); |
| 113 } | 111 } |
| 114 | 112 |
| 115 /// Removes [removed] from the graph's known set of source assets. | 113 /// Removes [removed] from the graph's known set of source assets. |
| 116 void removeSources(Iterable<AssetId> removed) { | 114 void removeSources(Iterable<AssetId> removed) { |
| 117 if (_sourceChanges == null) _sourceChanges = new ChangeBatch(); | 115 if (_sourceChanges == null) _sourceChanges = new ChangeBatch(); |
| 118 _sourceChanges.remove(removed); | 116 _sourceChanges.remove(removed); |
| 119 | 117 |
| 120 _waitForProcess(); | 118 _waitForProcess(); |
| 121 } | 119 } |
| 122 | 120 |
| 123 /// Reports a process result with the given error then throws it. | 121 /// Reports a process result with the given error then throws it. |
| 124 void reportError(error) { | 122 void reportError(error) { |
| 125 _resultsController.add(new ProcessResult(error)); | 123 _resultsController.add(new BuildResult(error)); |
| 126 } | 124 } |
| 127 | 125 |
| 128 /// Starts the build process asynchronously if there is work to be done. | 126 /// Starts the build process asynchronously if there is work to be done. |
| 129 /// | 127 /// |
| 130 /// Returns a future that completes with the background processing is done. | 128 /// 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. | 129 /// 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 | 130 /// 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 | 131 /// [results] stream) before they get to the returned future, so it is safe |
| 134 /// to discard it. | 132 /// to discard it. |
| 135 Future _waitForProcess() { | 133 Future _waitForProcess() { |
| 136 if (_processDone != null) return _processDone; | 134 if (_processDone != null) return _processDone; |
| 137 return _processDone = _process().catchError((error) { | 135 return _processDone = _process().then((_) { |
| 136 // Report the build completion. |
| 137 // TODO(rnystrom): Put some useful data in here. |
| 138 _resultsController.add(new BuildResult()); |
| 139 }).catchError((error) { |
| 138 // If we get here, it's an unexpected error. Runtime errors like missing | 140 // If we get here, it's an unexpected error. Runtime errors like missing |
| 139 // assets should be handled earlier. Errors from transformers or other | 141 // assets should be handled earlier. Errors from transformers or other |
| 140 // external code that barback calls into should be caught at that API | 142 // external code that barback calls into should be caught at that API |
| 141 // boundary. | 143 // boundary. |
| 142 // | 144 // |
| 143 // On the off chance we get here, pipe the error to the results stream | 145 // 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 | 146 // as an error. That will let applications handle it without it appearing |
| 145 // in the same path as "normal" errors that get reported. | 147 // in the same path as "normal" errors that get reported. |
| 146 _resultsController.addError(error); | 148 _resultsController.addError(error); |
| 147 }).whenComplete(() { | 149 }).whenComplete(() { |
| 148 _processDone = null; | 150 _processDone = null; |
| 149 // Report the build completion. | |
| 150 // TODO(rnystrom): Put some useful data in here. | |
| 151 _resultsController.add(new ProcessResult()); | |
| 152 }); | 151 }); |
| 153 } | 152 } |
| 154 | 153 |
| 155 /// Starts the background processing. | 154 /// Starts the background processing. |
| 156 /// | 155 /// |
| 157 /// Returns a future that completes when all assets have been processed. | 156 /// Returns a future that completes when all assets have been processed. |
| 158 Future _process() { | 157 Future _process() { |
| 159 return _processSourceChanges().then((_) { | 158 return _processSourceChanges().then((_) { |
| 160 // Find the first phase that has work to do and do it. | 159 // Find the first phase that has work to do and do it. |
| 161 var future; | 160 var future; |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 209 |
| 211 return Future.wait(futures).then((_) { | 210 return Future.wait(futures).then((_) { |
| 212 _phases.first.updateInputs(updated, changes.removed); | 211 _phases.first.updateInputs(updated, changes.removed); |
| 213 }); | 212 }); |
| 214 }); | 213 }); |
| 215 } | 214 } |
| 216 } | 215 } |
| 217 | 216 |
| 218 /// Used to report build results back from the asynchronous build process | 217 /// Used to report build results back from the asynchronous build process |
| 219 /// running in the background. | 218 /// running in the background. |
| 220 class ProcessResult { | 219 class BuildResult { |
| 221 /// The error that occurred, or `null` if the result is not an error. | 220 /// The error that occurred, or `null` if the result is not an error. |
| 222 final error; | 221 final error; |
| 223 | 222 |
| 224 ProcessResult([this.error]); | 223 /// `true` if this result is for a successful build. |
| 224 bool get succeeded => error == null; |
| 225 |
| 226 BuildResult([this.error]); |
| 225 } | 227 } |
| OLD | NEW |