| 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_cascade; |
| 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_graph_manager.dart'; | |
| 13 import 'asset_provider.dart'; | |
| 14 import 'asset_set.dart'; | 12 import 'asset_set.dart'; |
| 15 import 'errors.dart'; | 13 import 'errors.dart'; |
| 16 import 'change_batch.dart'; | 14 import 'change_batch.dart'; |
| 15 import 'package_graph.dart'; |
| 17 import 'phase.dart'; | 16 import 'phase.dart'; |
| 18 import 'transformer.dart'; | 17 import 'transformer.dart'; |
| 19 import 'utils.dart'; | 18 import 'utils.dart'; |
| 20 | 19 |
| 21 /// The asset manager for an individual package. | 20 /// The asset cascade for an individual package. |
| 22 /// | 21 /// |
| 23 /// This keeps track of which transformers are applied to which assets, and | 22 /// This keeps track of which [Transformer]s are applied to which assets, and |
| 24 /// re-runs those transformers when their dependencies change. The transformed | 23 /// re-runs those transformers when their dependencies change. The transformed |
| 25 /// assets are accessible via [getAssetById]. | 24 /// assets are accessible via [getAssetById]. |
| 26 class AssetGraph { | 25 /// |
| 26 /// A cascade consists of one or more [Phases], each of which has one or more |
| 27 /// [Transformer]s that run in parallel, potentially on the same inputs. The |
| 28 /// inputs of the first phase are the source assets for this cascade's package. |
| 29 /// The inputs of each successive phase are the outputs of the previous phase, |
| 30 /// as well as any assets that haven't yet been transformed. |
| 31 class AssetCascade { |
| 27 /// The name of the package whose assets are managed. | 32 /// The name of the package whose assets are managed. |
| 28 final String package; | 33 final String package; |
| 29 | 34 |
| 30 /// The [AssetGraphManager] that manages the [AssetGraph]s for all | 35 /// The [PackageGraph] that tracks all [AssetCascade]s for all dependencies of |
| 31 /// dependencies of the current app. | 36 /// the current app. |
| 32 final AssetGraphManager _manager; | 37 final PackageGraph _graph; |
| 33 | 38 |
| 34 final _phases = <Phase>[]; | 39 final _phases = <Phase>[]; |
| 35 | 40 |
| 36 /// A stream that emits a [BuildResult] each time the build is completed, | 41 /// A stream that emits a [BuildResult] each time the build is completed, |
| 37 /// whether or not it succeeded. | 42 /// whether or not it succeeded. |
| 38 /// | 43 /// |
| 39 /// If an unexpected error in barback itself occurs, it will be emitted | 44 /// If an unexpected error in barback itself occurs, it will be emitted |
| 40 /// through this stream's error channel. | 45 /// through this stream's error channel. |
| 41 Stream<BuildResult> get results => _resultsController.stream; | 46 Stream<BuildResult> get results => _resultsController.stream; |
| 42 final _resultsController = new StreamController<BuildResult>.broadcast(); | 47 final _resultsController = new StreamController<BuildResult>.broadcast(); |
| 43 | 48 |
| 44 /// A stream that emits any errors from the asset graph or the transformers. | 49 /// A stream that emits any errors from the cascade or the transformers. |
| 45 /// | 50 /// |
| 46 /// This emits errors as they're detected. If an error occurs in one part of | 51 /// This emits errors as they're detected. If an error occurs in one part of |
| 47 /// the asset graph, unrelated parts will continue building. | 52 /// the cascade, unrelated parts will continue building. |
| 48 /// | 53 /// |
| 49 /// This will not emit programming errors from barback itself. Those will be | 54 /// This will not emit programming errors from barback itself. Those will be |
| 50 /// emitted through the [results] stream's error channel. | 55 /// emitted through the [results] stream's error channel. |
| 51 Stream get errors => _errorsController.stream; | 56 Stream get errors => _errorsController.stream; |
| 52 final _errorsController = new StreamController.broadcast(); | 57 final _errorsController = new StreamController.broadcast(); |
| 53 | 58 |
| 54 /// The errors that have occurred since the current build started. | 59 /// The errors that have occurred since the current build started. |
| 55 /// | 60 /// |
| 56 /// This will be empty if no build is occurring. | 61 /// This will be empty if no build is occurring. |
| 57 Queue _accumulatedErrors; | 62 Queue _accumulatedErrors; |
| 58 | 63 |
| 59 /// A future that completes when the currently running build process finishes. | 64 /// A future that completes when the currently running build process finishes. |
| 60 /// | 65 /// |
| 61 /// If no build it in progress, is `null`. | 66 /// If no build it in progress, is `null`. |
| 62 Future _processDone; | 67 Future _processDone; |
| 63 | 68 |
| 64 ChangeBatch _sourceChanges; | 69 ChangeBatch _sourceChanges; |
| 65 | 70 |
| 66 /// Creates a new [AssetGraph]. | 71 /// Creates a new [AssetCascade]. |
| 67 /// | 72 /// |
| 68 /// It loads source assets within [package] using [provider] and then uses | 73 /// It loads source assets within [package] using [provider] and then uses |
| 69 /// [transformerPhases] to generate output files from them. | 74 /// [transformerPhases] to generate output files from them. |
| 70 //TODO(rnystrom): Better way of specifying transformers and their ordering. | 75 //TODO(rnystrom): Better way of specifying transformers and their ordering. |
| 71 AssetGraph(this._manager, this.package, | 76 AssetCascade(this._graph, this.package, |
| 72 Iterable<Iterable<Transformer>> transformerPhases) { | 77 Iterable<Iterable<Transformer>> transformerPhases) { |
| 73 // Flatten the phases to a list so we can traverse backwards to wire up | 78 // Flatten the phases to a list so we can traverse backwards to wire up |
| 74 // each phase to its next. | 79 // each phase to its next. |
| 75 var phases = transformerPhases.toList(); | 80 var phases = transformerPhases.toList(); |
| 76 | 81 |
| 77 // Each phase writes its outputs as inputs to the next phase after it. | 82 // Each phase writes its outputs as inputs to the next phase after it. |
| 78 // Add a phase at the end for the final outputs of the last phase. | 83 // Add a phase at the end for the final outputs of the last phase. |
| 79 phases.add([]); | 84 phases.add([]); |
| 80 | 85 |
| 81 Phase nextPhase = null; | 86 Phase nextPhase = null; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 | 228 |
| 224 // Take the current batch to ensure it doesn't get added to while we're | 229 // Take the current batch to ensure it doesn't get added to while we're |
| 225 // processing it. | 230 // processing it. |
| 226 var changes = _sourceChanges; | 231 var changes = _sourceChanges; |
| 227 _sourceChanges = null; | 232 _sourceChanges = null; |
| 228 | 233 |
| 229 var updated = new AssetSet(); | 234 var updated = new AssetSet(); |
| 230 var futures = []; | 235 var futures = []; |
| 231 for (var id in changes.updated) { | 236 for (var id in changes.updated) { |
| 232 // TODO(rnystrom): Catch all errors from provider and route to results. | 237 // TODO(rnystrom): Catch all errors from provider and route to results. |
| 233 futures.add(_manager.provider.getAsset(id).then((asset) { | 238 futures.add(_graph.provider.getAsset(id).then((asset) { |
| 234 updated.add(asset); | 239 updated.add(asset); |
| 235 }).catchError((error) { | 240 }).catchError((error) { |
| 236 if (error is AssetNotFoundException) { | 241 if (error is AssetNotFoundException) { |
| 237 // Handle missing asset errors like regular missing assets. | 242 // Handle missing asset errors like regular missing assets. |
| 238 reportError(error); | 243 reportError(error); |
| 239 } else { | 244 } else { |
| 240 // It's an unexpected error, so rethrow it. | 245 // It's an unexpected error, so rethrow it. |
| 241 throw error; | 246 throw error; |
| 242 } | 247 } |
| 243 })); | 248 })); |
| 244 } | 249 } |
| 245 | 250 |
| 246 return Future.wait(futures).then((_) { | 251 return Future.wait(futures).then((_) { |
| 247 _phases.first.updateInputs(updated, changes.removed); | 252 _phases.first.updateInputs(updated, changes.removed); |
| 248 }); | 253 }); |
| 249 }); | 254 }); |
| 250 } | 255 } |
| 251 } | 256 } |
| 252 | 257 |
| 253 /// An event indicating that the asset graph has finished building. | 258 /// An event indicating that the cascade has finished building all assets. |
| 254 /// | 259 /// |
| 255 /// A build can end either in success or failure. If there were no errors during | 260 /// A build can end either in success or failure. If there were no errors during |
| 256 /// the build, it's considered to be a success; any errors render it a failure, | 261 /// the build, it's considered to be a success; any errors render it a failure, |
| 257 /// although individual assets may still have built successfully. | 262 /// although individual assets may still have built successfully. |
| 258 class BuildResult { | 263 class BuildResult { |
| 259 /// All errors that occurred during the build. | 264 /// All errors that occurred during the build. |
| 260 final List errors; | 265 final List errors; |
| 261 | 266 |
| 262 /// `true` if the build succeeded. | 267 /// `true` if the build succeeded. |
| 263 bool get succeeded => errors.isEmpty; | 268 bool get succeeded => errors.isEmpty; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 282 msg.write(prefixLines(error.toString())); | 287 msg.write(prefixLines(error.toString())); |
| 283 if (stackTrace != null) { | 288 if (stackTrace != null) { |
| 284 msg.write("\n\n"); | 289 msg.write("\n\n"); |
| 285 msg.write("Stack trace:\n"); | 290 msg.write("Stack trace:\n"); |
| 286 msg.write(prefixLines(stackTrace.toString())); | 291 msg.write(prefixLines(stackTrace.toString())); |
| 287 } | 292 } |
| 288 return msg.toString(); | 293 return msg.toString(); |
| 289 }).join("\n\n"); | 294 }).join("\n\n"); |
| 290 } | 295 } |
| 291 } | 296 } |
| OLD | NEW |