| 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.package_graph; | 5 library barback.package_graph; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'asset_cascade.dart'; | 9 import 'asset_cascade.dart'; |
| 10 import 'asset_id.dart'; | 10 import 'asset_id.dart'; |
| 11 import 'asset_node.dart'; | 11 import 'asset_node.dart'; |
| 12 import 'asset_set.dart'; |
| 12 import 'build_result.dart'; | 13 import 'build_result.dart'; |
| 13 import 'errors.dart'; | 14 import 'errors.dart'; |
| 14 import 'package_provider.dart'; | 15 import 'package_provider.dart'; |
| 16 import 'transformer.dart'; |
| 15 import 'utils.dart'; | 17 import 'utils.dart'; |
| 16 | 18 |
| 17 /// The collection of [AssetCascade]s for an entire application. | 19 /// The collection of [AssetCascade]s for an entire application. |
| 18 /// | 20 /// |
| 19 /// This tracks each package's [AssetCascade] and routes asset requests between | 21 /// This tracks each package's [AssetCascade] and routes asset requests between |
| 20 /// them. | 22 /// them. |
| 21 class PackageGraph { | 23 class PackageGraph { |
| 22 /// The provider that exposes asset and package information. | 24 /// The provider that exposes asset and package information. |
| 23 final PackageProvider provider; | 25 final PackageProvider provider; |
| 24 | 26 |
| (...skipping 20 matching lines...) Expand all Loading... |
| 45 /// A stream that emits any errors from the graph or the transformers. | 47 /// A stream that emits any errors from the graph or the transformers. |
| 46 /// | 48 /// |
| 47 /// This emits errors as they're detected. If an error occurs in one part of | 49 /// This emits errors as they're detected. If an error occurs in one part of |
| 48 /// the graph, unrelated parts will continue building. | 50 /// the graph, unrelated parts will continue building. |
| 49 /// | 51 /// |
| 50 /// This will not emit programming errors from barback itself. Those will be | 52 /// This will not emit programming errors from barback itself. Those will be |
| 51 /// emitted through the [results] stream's error channel. | 53 /// emitted through the [results] stream's error channel. |
| 52 Stream<BarbackException> get errors => _errors; | 54 Stream<BarbackException> get errors => _errors; |
| 53 Stream<BarbackException> _errors; | 55 Stream<BarbackException> _errors; |
| 54 | 56 |
| 57 /// The most recent error emitted from a cascade's result stream. |
| 58 /// |
| 59 /// This is used to pipe an unexpected error from a build to the resulting |
| 60 /// [Future] returned by [getAllAssets]. |
| 61 var _lastUnexpectedError; |
| 62 |
| 55 /// Creates a new [PackageGraph] that will transform assets in all packages | 63 /// Creates a new [PackageGraph] that will transform assets in all packages |
| 56 /// made available by [provider]. | 64 /// made available by [provider]. |
| 57 PackageGraph(this.provider) { | 65 PackageGraph(this.provider) { |
| 58 for (var package in provider.packages) { | 66 for (var package in provider.packages) { |
| 59 var cascade = new AssetCascade(this, package); | 67 var cascade = new AssetCascade(this, package); |
| 60 // The initial result for each cascade is "success" since the cascade | 68 // The initial result for each cascade is "success" since the cascade |
| 61 // doesn't start building until some source in that graph is updated. | 69 // doesn't start building until some source in that graph is updated. |
| 62 _cascadeResults[package] = new BuildResult.success(); | 70 _cascadeResults[package] = new BuildResult.success(); |
| 63 _cascades[package] = cascade; | 71 _cascades[package] = cascade; |
| 64 | 72 |
| 65 cascade.results.listen((result) { | 73 cascade.results.listen((result) { |
| 66 _cascadeResults[cascade.package] = result; | 74 _cascadeResults[cascade.package] = result; |
| 67 // If any cascade hasn't yet finished, the overall build isn't finished | 75 // If any cascade hasn't yet finished, the overall build isn't finished |
| 68 // either. | 76 // either. |
| 69 if (_cascadeResults.values.any((result) => result == null)) return; | 77 if (_cascadeResults.values.any((result) => result == null)) return; |
| 70 | 78 |
| 71 // Include all build errors for all cascades. If no cascades have | 79 // Include all build errors for all cascades. If no cascades have |
| 72 // errors, the result will automatically be considered a success. | 80 // errors, the result will automatically be considered a success. |
| 73 _resultsController.add(new BuildResult(unionAll( | 81 _resultsController.add(new BuildResult(unionAll( |
| 74 _cascadeResults.values.map((result) => result.errors)))); | 82 _cascadeResults.values.map((result) => result.errors)))); |
| 75 }, onError: _resultsController.addError); | 83 }, onError: (error) { |
| 84 _lastUnexpectedError = error; |
| 85 _resultsController.addError(error); |
| 86 }); |
| 76 } | 87 } |
| 77 | 88 |
| 78 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors)); | 89 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors)); |
| 79 } | 90 } |
| 80 | 91 |
| 81 /// Gets the asset node identified by [id]. | 92 /// Gets the asset node identified by [id]. |
| 82 /// | 93 /// |
| 83 /// If [id] is for a generated or transformed asset, this will wait until it | 94 /// If [id] is for a generated or transformed asset, this will wait until it |
| 84 /// has been created and return it. This means that the returned asset will | 95 /// has been created and return it. This means that the returned asset will |
| 85 /// always be [AssetState.AVAILABLE]. | 96 /// always be [AssetState.AVAILABLE]. |
| 86 /// | 97 /// |
| 87 /// If the asset cannot be found, returns null. | 98 /// If the asset cannot be found, returns null. |
| 88 Future<AssetNode> getAssetNode(AssetId id) { | 99 Future<AssetNode> getAssetNode(AssetId id) { |
| 89 var cascade = _cascades[id.package]; | 100 var cascade = _cascades[id.package]; |
| 90 if (cascade != null) return cascade.getAssetNode(id); | 101 if (cascade != null) return cascade.getAssetNode(id); |
| 91 return new Future.value(null); | 102 return new Future.value(null); |
| 92 } | 103 } |
| 93 | 104 |
| 105 /// Gets all output assets. |
| 106 /// |
| 107 /// If a build is currently in progress, waits until it completes. The |
| 108 /// returned future will complete with an error if the build is not |
| 109 /// successful. |
| 110 Future<AssetSet> getAllAssets() { |
| 111 if (_cascadeResults.values.contains(null)) { |
| 112 // A build is still ongoing, so wait for it to complete and try again. |
| 113 return results.first.then((_) => getAllAssets()); |
| 114 } |
| 115 |
| 116 // If an unexpected error occurred, complete with that. |
| 117 if (_lastUnexpectedError != null) { |
| 118 var error = _lastUnexpectedError; |
| 119 _lastUnexpectedError = null; |
| 120 return new Future.error(error); |
| 121 } |
| 122 |
| 123 // If the build completed with an error, complete the future with it. |
| 124 var errors = unionAll( |
| 125 _cascadeResults.values.map((result) => result.errors)); |
| 126 if (errors.isNotEmpty) { |
| 127 return new Future.error(BarbackException.aggregate(errors)); |
| 128 } |
| 129 |
| 130 // Otherwise, return all of the final output assets. |
| 131 var assets = unionAll(_cascades.values.map( |
| 132 (cascade) => cascade.availableOutputs.toSet())); |
| 133 |
| 134 return new Future.value(new AssetSet.from(assets)); |
| 135 } |
| 136 |
| 94 /// Adds [sources] to the graph's known set of source assets. | 137 /// Adds [sources] to the graph's known set of source assets. |
| 95 /// | 138 /// |
| 96 /// Begins applying any transforms that can consume any of the sources. If a | 139 /// Begins applying any transforms that can consume any of the sources. If a |
| 97 /// given source is already known, it is considered modified and all | 140 /// given source is already known, it is considered modified and all |
| 98 /// transforms that use it will be re-applied. | 141 /// transforms that use it will be re-applied. |
| 99 void updateSources(Iterable<AssetId> sources) { | 142 void updateSources(Iterable<AssetId> sources) { |
| 100 groupBy(sources, (id) => id.package).forEach((package, ids) { | 143 groupBy(sources, (id) => id.package).forEach((package, ids) { |
| 101 var cascade = _cascades[package]; | 144 var cascade = _cascades[package]; |
| 102 if (cascade == null) throw new ArgumentError("Unknown package $package."); | 145 if (cascade == null) throw new ArgumentError("Unknown package $package."); |
| 103 _cascadeResults[package] = null; | 146 _cascadeResults[package] = null; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 114 cascade.removeSources(ids); | 157 cascade.removeSources(ids); |
| 115 }); | 158 }); |
| 116 } | 159 } |
| 117 | 160 |
| 118 void updateTransformers(String package, | 161 void updateTransformers(String package, |
| 119 Iterable<Iterable<Transformer>> transformers) { | 162 Iterable<Iterable<Transformer>> transformers) { |
| 120 _cascadeResults[package] = null; | 163 _cascadeResults[package] = null; |
| 121 _cascades[package].updateTransformers(transformers); | 164 _cascades[package].updateTransformers(transformers); |
| 122 } | 165 } |
| 123 } | 166 } |
| OLD | NEW |