| 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_manager; | 5 library barback.package_graph; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; | 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 | 10 |
| 11 import 'asset.dart'; | 11 import 'asset.dart'; |
| 12 import 'asset_provider.dart'; | 12 import 'asset_cascade.dart'; |
| 13 import 'asset_graph.dart'; | |
| 14 import 'asset_id.dart'; | 13 import 'asset_id.dart'; |
| 15 import 'errors.dart'; | 14 import 'errors.dart'; |
| 15 import 'package_provider.dart'; |
| 16 import 'utils.dart'; | 16 import 'utils.dart'; |
| 17 | 17 |
| 18 // TODO(nweiz): come up with better names for this and AssetGraph. | 18 /// The collection of [AssetCascade]s for an entire application. |
| 19 /// |
| 20 /// This tracks each package's [AssetCascade] and routes asset requests between |
| 21 /// them. |
| 22 class PackageGraph { |
| 23 /// The provider that exposes asset and package information. |
| 24 final PackageProvider provider; |
| 19 | 25 |
| 20 /// The asset manager for an entire application. | 26 /// The [AssetCascade] for each package. |
| 21 /// | 27 final _cascades = <String, AssetCascade>{}; |
| 22 /// This tracks each package's [AssetGraph] and routes asset requests between | |
| 23 /// them. | |
| 24 class AssetGraphManager { | |
| 25 /// The provider that exposes asset and package information. | |
| 26 final AssetProvider provider; | |
| 27 | 28 |
| 28 /// The [AssetGraph] for each package. | 29 /// The current [BuildResult] for each package's [AssetCascade]. |
| 29 final _graphs = <String, AssetGraph>{}; | |
| 30 | |
| 31 /// The current [BuildResult] for each package's [AssetGraph]. | |
| 32 /// | 30 /// |
| 33 /// The result for a given package will be `null` if that [AssetGraph] is | 31 /// The result for a given package will be `null` if that [AssetCascade] is |
| 34 /// actively building. | 32 /// actively building. |
| 35 final _graphResults = <String, BuildResult>{}; | 33 final _cascadeResults = <String, BuildResult>{}; |
| 36 | 34 |
| 37 /// A stream that emits a [BuildResult] each time the build is completed, | 35 /// A stream that emits a [BuildResult] each time the build is completed, |
| 38 /// whether or not it succeeded. | 36 /// whether or not it succeeded. |
| 39 /// | 37 /// |
| 40 /// This will emit a result only once every package's [AssetGraph] has | 38 /// This will emit a result only once every package's [AssetCascade] has |
| 41 /// finished building. | 39 /// finished building. |
| 42 /// | 40 /// |
| 43 /// If an unexpected error in barback itself occurs, it will be emitted | 41 /// If an unexpected error in barback itself occurs, it will be emitted |
| 44 /// through this stream's error channel. | 42 /// through this stream's error channel. |
| 45 Stream<BuildResult> get results => _resultsController.stream; | 43 Stream<BuildResult> get results => _resultsController.stream; |
| 46 final _resultsController = new StreamController<BuildResult>.broadcast(); | 44 final _resultsController = new StreamController<BuildResult>.broadcast(); |
| 47 | 45 |
| 48 /// A stream that emits any errors from the asset graph or the transformers. | 46 /// A stream that emits any errors from the graph or the transformers. |
| 49 /// | 47 /// |
| 50 /// This emits errors as they're detected. If an error occurs in one part of | 48 /// This emits errors as they're detected. If an error occurs in one part of |
| 51 /// the asset graph, unrelated parts will continue building. | 49 /// the graph, unrelated parts will continue building. |
| 52 /// | 50 /// |
| 53 /// This will not emit programming errors from barback itself. Those will be | 51 /// This will not emit programming errors from barback itself. Those will be |
| 54 /// emitted through the [results] stream's error channel. | 52 /// emitted through the [results] stream's error channel. |
| 55 Stream get errors => _errors; | 53 Stream get errors => _errors; |
| 56 Stream _errors; | 54 Stream _errors; |
| 57 | 55 |
| 58 /// Creates a new [AssetGraphManager] that will transform assets in all | 56 /// Creates a new [PackageGraph] that will transform assets in all packages |
| 59 /// packages made available by [provider]. | 57 /// made available by [provider]. |
| 60 AssetGraphManager(this.provider) { | 58 PackageGraph(this.provider) { |
| 61 for (var package in provider.packages) { | 59 for (var package in provider.packages) { |
| 62 var graph = new AssetGraph(this, package, | 60 var cascade = new AssetCascade(this, package, |
| 63 provider.getTransformers(package)); | 61 provider.getTransformers(package)); |
| 64 // The initial result for each graph is "success" since the graph doesn't | 62 // The initial result for each cascade is "success" since the cascade |
| 65 // start building until some source in that graph is updated. | 63 // doesn't start building until some source in that graph is updated. |
| 66 _graphResults[package] = new BuildResult.success(); | 64 _cascadeResults[package] = new BuildResult.success(); |
| 67 _graphs[package] = graph; | 65 _cascades[package] = cascade; |
| 68 | 66 |
| 69 graph.results.listen((result) { | 67 cascade.results.listen((result) { |
| 70 _graphResults[graph.package] = result; | 68 _cascadeResults[cascade.package] = result; |
| 71 // If any graph hasn't yet finished, the overall build isn't finished | 69 // If any cascade hasn't yet finished, the overall build isn't finished |
| 72 // either. | 70 // either. |
| 73 if (_graphResults.values.any((result) => result == null)) return; | 71 if (_cascadeResults.values.any((result) => result == null)) return; |
| 74 | 72 |
| 75 // Include all build errors for all graphs. If no graphs have errors, | 73 // Include all build errors for all cascades. If no cascades have |
| 76 // the result will automatically be considered a success. | 74 // errors, the result will automatically be considered a success. |
| 77 _resultsController.add(new BuildResult(flatten( | 75 _resultsController.add(new BuildResult(flatten( |
| 78 _graphResults.values.map((result) => result.errors)))); | 76 _cascadeResults.values.map((result) => result.errors)))); |
| 79 }, onError: _resultsController.addError); | 77 }, onError: _resultsController.addError); |
| 80 } | 78 } |
| 81 | 79 |
| 82 _errors = mergeStreams(_graphs.values.map((graph) => graph.errors)); | 80 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors)); |
| 83 } | 81 } |
| 84 | 82 |
| 85 /// Gets the asset identified by [id]. | 83 /// Gets the asset identified by [id]. |
| 86 /// | 84 /// |
| 87 /// If [id] is for a generated or transformed asset, this will wait until | 85 /// If [id] is for a generated or transformed asset, this will wait until |
| 88 /// it has been created and return it. If the asset cannot be found, throws | 86 /// it has been created and return it. If the asset cannot be found, throws |
| 89 /// [AssetNotFoundException]. | 87 /// [AssetNotFoundException]. |
| 90 Future<Asset> getAssetById(AssetId id) { | 88 Future<Asset> getAssetById(AssetId id) { |
| 91 var graph = _graphs[id.package]; | 89 var cascade = _cascades[id.package]; |
| 92 if (graph != null) return graph.getAssetById(id); | 90 if (cascade != null) return cascade.getAssetById(id); |
| 93 return new Future.error( | 91 return new Future.error( |
| 94 new AssetNotFoundException(id), | 92 new AssetNotFoundException(id), |
| 95 new Trace.current().vmTrace); | 93 new Trace.current().vmTrace); |
| 96 } | 94 } |
| 97 | 95 |
| 98 /// Adds [sources] to the graph's known set of source assets. | 96 /// Adds [sources] to the graph's known set of source assets. |
| 99 /// | 97 /// |
| 100 /// Begins applying any transforms that can consume any of the sources. If a | 98 /// Begins applying any transforms that can consume any of the sources. If a |
| 101 /// given source is already known, it is considered modified and all | 99 /// given source is already known, it is considered modified and all |
| 102 /// transforms that use it will be re-applied. | 100 /// transforms that use it will be re-applied. |
| 103 void updateSources(Iterable<AssetId> sources) { | 101 void updateSources(Iterable<AssetId> sources) { |
| 104 groupBy(sources, (id) => id.package).forEach((package, ids) { | 102 groupBy(sources, (id) => id.package).forEach((package, ids) { |
| 105 var graph = _graphs[package]; | 103 var cascade = _cascades[package]; |
| 106 if (graph == null) throw new ArgumentError("Unknown package $package."); | 104 if (cascade == null) throw new ArgumentError("Unknown package $package."); |
| 107 _graphResults[package] = null; | 105 _cascadeResults[package] = null; |
| 108 graph.updateSources(ids); | 106 cascade.updateSources(ids); |
| 109 }); | 107 }); |
| 110 } | 108 } |
| 111 | 109 |
| 112 /// Removes [removed] from the graph's known set of source assets. | 110 /// Removes [removed] from the graph's known set of source assets. |
| 113 void removeSources(Iterable<AssetId> sources) { | 111 void removeSources(Iterable<AssetId> sources) { |
| 114 groupBy(sources, (id) => id.package).forEach((package, ids) { | 112 groupBy(sources, (id) => id.package).forEach((package, ids) { |
| 115 var graph = _graphs[package]; | 113 var cascade = _cascades[package]; |
| 116 if (graph == null) throw new ArgumentError("Unknown package $package."); | 114 if (cascade == null) throw new ArgumentError("Unknown package $package."); |
| 117 _graphResults[package] = null; | 115 _cascadeResults[package] = null; |
| 118 graph.removeSources(ids); | 116 cascade.removeSources(ids); |
| 119 }); | 117 }); |
| 120 } | 118 } |
| 121 } | 119 } |
| OLD | NEW |