Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(336)

Side by Side Diff: pkg/barback/lib/src/package_graph.dart

Issue 22824023: Start sketching out a buildAll() method. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Don't wrap a single error in an aggregate. Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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';
15 import 'utils.dart'; 16 import 'utils.dart';
16 17
17 /// The collection of [AssetCascade]s for an entire application. 18 /// The collection of [AssetCascade]s for an entire application.
18 /// 19 ///
19 /// This tracks each package's [AssetCascade] and routes asset requests between 20 /// This tracks each package's [AssetCascade] and routes asset requests between
20 /// them. 21 /// them.
21 class PackageGraph { 22 class PackageGraph {
(...skipping 23 matching lines...) Expand all
45 /// A stream that emits any errors from the graph or the transformers. 46 /// A stream that emits any errors from the graph or the transformers.
46 /// 47 ///
47 /// 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
48 /// the graph, unrelated parts will continue building. 49 /// the graph, unrelated parts will continue building.
49 /// 50 ///
50 /// 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
51 /// emitted through the [results] stream's error channel. 52 /// emitted through the [results] stream's error channel.
52 Stream<BarbackException> get errors => _errors; 53 Stream<BarbackException> get errors => _errors;
53 Stream<BarbackException> _errors; 54 Stream<BarbackException> _errors;
54 55
56 /// The error associated with the previous build, or `null` if the last build
57 /// was successful.
58 BarbackException _lastError;
nweiz 2013/08/20 19:59:26 I don't understand this. Why does the user only ca
Bob Nystrom 2013/08/20 21:29:20 They don't. This was aggregating all cascades.
59
55 /// Creates a new [PackageGraph] that will transform assets in all packages 60 /// Creates a new [PackageGraph] that will transform assets in all packages
56 /// made available by [provider]. 61 /// made available by [provider].
57 PackageGraph(this.provider) { 62 PackageGraph(this.provider) {
58 for (var package in provider.packages) { 63 for (var package in provider.packages) {
59 var cascade = new AssetCascade(this, package, 64 var cascade = new AssetCascade(this, package,
60 provider.getTransformers(package)); 65 provider.getTransformers(package));
61 // The initial result for each cascade is "success" since the cascade 66 // The initial result for each cascade is "success" since the cascade
62 // doesn't start building until some source in that graph is updated. 67 // doesn't start building until some source in that graph is updated.
63 _cascadeResults[package] = new BuildResult.success(); 68 _cascadeResults[package] = new BuildResult.success();
64 _cascades[package] = cascade; 69 _cascades[package] = cascade;
65 70
66 cascade.results.listen((result) { 71 cascade.results.listen((result) {
67 _cascadeResults[cascade.package] = result; 72 _cascadeResults[cascade.package] = result;
68 // If any cascade hasn't yet finished, the overall build isn't finished 73 // If any cascade hasn't yet finished, the overall build isn't finished
69 // either. 74 // either.
70 if (_cascadeResults.values.any((result) => result == null)) return; 75 if (_cascadeResults.values.any((result) => result == null)) return;
71 76
72 // Include all build errors for all cascades. If no cascades have 77 // Include all build errors for all cascades. If no cascades have
73 // errors, the result will automatically be considered a success. 78 // errors, the result will automatically be considered a success.
74 _resultsController.add(new BuildResult(unionAll( 79 var errors = unionAll(
75 _cascadeResults.values.map((result) => result.errors)))); 80 _cascadeResults.values.map((result) => result.errors));
76 }, onError: _resultsController.addError); 81
82 // Remember any errors for when [getAllAssets()] is called.
83 if (errors.length > 1) {
84 _lastError = new AggregateException(errors);
85 } else if (errors.length == 1) {
86 _lastError = errors.first;
87 } else {
88 // The build completed successfully.
89 _lastError = null;
90 }
91
92 _resultsController.add(new BuildResult(errors));
93 }, onError: (error) {
94 _lastError = error;
nweiz 2013/08/20 19:59:26 This is definitely not right. The error that's cau
Bob Nystrom 2013/08/20 21:29:20 Removed. I'm not sure what the best thing to do i
nweiz 2013/08/20 22:29:03 I think it's reasonable for [getAllAssets] to thro
Bob Nystrom 2013/08/21 18:10:23 Done.
95 _resultsController.addError(error);
96 });
77 } 97 }
78 98
79 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors)); 99 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors));
80 } 100 }
81 101
82 /// Gets the asset node identified by [id]. 102 /// Gets the asset node identified by [id].
83 /// 103 ///
84 /// If [id] is for a generated or transformed asset, this will wait until it 104 /// If [id] is for a generated or transformed asset, this will wait until it
85 /// has been created and return it. This means that the returned asset will 105 /// has been created and return it. This means that the returned asset will
86 /// always be [AssetState.AVAILABLE]. 106 /// always be [AssetState.AVAILABLE].
87 /// 107 ///
88 /// If the asset cannot be found, returns null. 108 /// If the asset cannot be found, returns null.
89 Future<AssetNode> getAssetNode(AssetId id) { 109 Future<AssetNode> getAssetNode(AssetId id) {
90 var cascade = _cascades[id.package]; 110 var cascade = _cascades[id.package];
91 if (cascade != null) return cascade.getAssetNode(id); 111 if (cascade != null) return cascade.getAssetNode(id);
92 return new Future.value(null); 112 return new Future.value(null);
93 } 113 }
94 114
115 /// Gets all output assets.
116 ///
117 /// If a build is currently in progress, waits until it completes. The
118 /// returned future will complete with an error if the build is not
119 /// successful.
120 Future<AssetSet> getAllAssets() {
121 if (_cascadeResults.values.any((result) => result == null)) {
nweiz 2013/08/20 19:59:26 _cascadeResults.values.contains(null)
Bob Nystrom 2013/08/20 21:29:20 Done.
122 // A build is still ongoing, so wait for it to complete and try again.
123 return results.first.then((_) => getAllAssets());
124 }
125
126 // If the build completed with an error, complete the future with it.
127 if (_lastError != null) return new Future.error(_lastError);
128
129 // Otherwise, return all of the final output assets.
130 var assets = unionAll(_cascades.values.map(
131 (cascade) => cascade.availableOutputs.toSet()));
132
133 return new Future.value(new AssetSet.from(assets));
134 }
135
95 /// Adds [sources] to the graph's known set of source assets. 136 /// Adds [sources] to the graph's known set of source assets.
96 /// 137 ///
97 /// Begins applying any transforms that can consume any of the sources. If a 138 /// Begins applying any transforms that can consume any of the sources. If a
98 /// given source is already known, it is considered modified and all 139 /// given source is already known, it is considered modified and all
99 /// transforms that use it will be re-applied. 140 /// transforms that use it will be re-applied.
100 void updateSources(Iterable<AssetId> sources) { 141 void updateSources(Iterable<AssetId> sources) {
101 groupBy(sources, (id) => id.package).forEach((package, ids) { 142 groupBy(sources, (id) => id.package).forEach((package, ids) {
102 var cascade = _cascades[package]; 143 var cascade = _cascades[package];
103 if (cascade == null) throw new ArgumentError("Unknown package $package."); 144 if (cascade == null) throw new ArgumentError("Unknown package $package.");
104 _cascadeResults[package] = null; 145 _cascadeResults[package] = null;
105 cascade.updateSources(ids); 146 cascade.updateSources(ids);
106 }); 147 });
107 } 148 }
108 149
109 /// Removes [removed] from the graph's known set of source assets. 150 /// Removes [removed] from the graph's known set of source assets.
110 void removeSources(Iterable<AssetId> sources) { 151 void removeSources(Iterable<AssetId> sources) {
111 groupBy(sources, (id) => id.package).forEach((package, ids) { 152 groupBy(sources, (id) => id.package).forEach((package, ids) {
112 var cascade = _cascades[package]; 153 var cascade = _cascades[package];
113 if (cascade == null) throw new ArgumentError("Unknown package $package."); 154 if (cascade == null) throw new ArgumentError("Unknown package $package.");
114 _cascadeResults[package] = null; 155 _cascadeResults[package] = null;
115 cascade.removeSources(ids); 156 cascade.removeSources(ids);
116 }); 157 });
117 } 158 }
118 } 159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698