OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library barback.graph.package_graph; |
| 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:collection'; |
| 9 |
| 10 import '../asset/asset_id.dart'; |
| 11 import '../asset/asset_node.dart'; |
| 12 import '../asset/asset_set.dart'; |
| 13 import '../build_result.dart'; |
| 14 import '../errors.dart'; |
| 15 import '../log.dart'; |
| 16 import '../package_provider.dart'; |
| 17 import '../utils.dart'; |
| 18 import 'asset_cascade.dart'; |
| 19 import 'node_status.dart'; |
| 20 import 'static_asset_cascade.dart'; |
| 21 |
| 22 /// The collection of [AssetCascade]s for an entire application. |
| 23 /// |
| 24 /// This tracks each package's [AssetCascade] and routes asset requests between |
| 25 /// them. |
| 26 class PackageGraph { |
| 27 /// The provider that exposes asset and package information. |
| 28 final PackageProvider provider; |
| 29 |
| 30 /// The [AssetCascade] for each package. |
| 31 final _cascades = <String, AssetCascade>{}; |
| 32 |
| 33 /// A stream that emits a [BuildResult] each time the build is completed, |
| 34 /// whether or not it succeeded. |
| 35 /// |
| 36 /// This will emit a result only once every package's [AssetCascade] has |
| 37 /// finished building. |
| 38 /// |
| 39 /// If an unexpected error in barback itself occurs, it will be emitted |
| 40 /// through this stream's error channel. |
| 41 Stream<BuildResult> get results => _resultsController.stream; |
| 42 final _resultsController = |
| 43 new StreamController<BuildResult>.broadcast(sync: true); |
| 44 |
| 45 /// A stream that emits any errors from the graph or the transformers. |
| 46 /// |
| 47 /// This emits errors as they're detected. If an error occurs in one part of |
| 48 /// the graph, unrelated parts will continue building. |
| 49 /// |
| 50 /// This will not emit programming errors from barback itself. Those will be |
| 51 /// emitted through the [results] stream's error channel. |
| 52 Stream<BarbackException> get errors => _errors; |
| 53 Stream<BarbackException> _errors; |
| 54 |
| 55 /// The stream of [LogEntry] objects used to report transformer log entries. |
| 56 Stream<LogEntry> get log => _logController.stream; |
| 57 final _logController = new StreamController<LogEntry>.broadcast(sync: true); |
| 58 |
| 59 /// How far along [this] is in processing its assets. |
| 60 NodeStatus get _status => NodeStatus.dirtiest( |
| 61 _cascades.values.map((cascade) => cascade.status)); |
| 62 |
| 63 /// Whether a [BuildResult] is scheduled to be emitted on [results] (see |
| 64 /// [_tryScheduleResult]). |
| 65 bool _resultScheduled = false; |
| 66 |
| 67 /// The most recent [BuildResult] emitted on [results]. |
| 68 BuildResult _lastResult; |
| 69 |
| 70 // TODO(nweiz): This can have bogus errors if an error is created and resolved |
| 71 // in the space of one build. |
| 72 /// The errors that have occurred since the current build started. |
| 73 /// |
| 74 /// This will be empty if no build is occurring. |
| 75 final _accumulatedErrors = new Queue<BarbackException>(); |
| 76 |
| 77 /// The most recent error emitted from a cascade's result stream. |
| 78 /// |
| 79 /// This is used to pipe an unexpected error from a build to the resulting |
| 80 /// [Future] returned by [getAllAssets]. |
| 81 var _lastUnexpectedError; |
| 82 |
| 83 /// The stack trace for [_lastUnexpectedError]. |
| 84 StackTrace _lastUnexpectedErrorTrace; |
| 85 |
| 86 /// Creates a new [PackageGraph] that will transform assets in all packages |
| 87 /// made available by [provider]. |
| 88 PackageGraph(this.provider) { |
| 89 _inErrorZone(() { |
| 90 for (var package in provider.packages) { |
| 91 var cascade = new AssetCascade(this, package); |
| 92 _cascades[package] = cascade; |
| 93 cascade.onLog.listen(_onLog); |
| 94 cascade.onStatusChange.listen((status) { |
| 95 if (status == NodeStatus.IDLE) _tryScheduleResult(); |
| 96 }); |
| 97 } |
| 98 |
| 99 if (provider is StaticPackageProvider) { |
| 100 StaticPackageProvider staticProvider = provider; |
| 101 for (var package in staticProvider.staticPackages) { |
| 102 if (_cascades.containsKey(package)) { |
| 103 throw new StateError('Package "$package" is in both ' |
| 104 'PackageProvider.packages and PackageProvider.staticPackages.'); |
| 105 } |
| 106 |
| 107 var cascade = new StaticAssetCascade(this, package); |
| 108 _cascades[package] = cascade; |
| 109 } |
| 110 } |
| 111 |
| 112 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors), |
| 113 broadcast: true); |
| 114 _errors.listen(_accumulatedErrors.add); |
| 115 |
| 116 // Make sure a result gets scheduled even if there are no cascades or all |
| 117 // of them are static. |
| 118 if (provider.packages.isEmpty) _tryScheduleResult(); |
| 119 }); |
| 120 } |
| 121 |
| 122 /// Gets the asset node identified by [id]. |
| 123 /// |
| 124 /// If [id] is for a generated or transformed asset, this will wait until it |
| 125 /// has been created and return it. This means that the returned asset will |
| 126 /// always be [AssetState.AVAILABLE]. |
| 127 /// |
| 128 /// If the asset cannot be found, returns null. |
| 129 Future<AssetNode> getAssetNode(AssetId id) { |
| 130 return _inErrorZone(() { |
| 131 var cascade = _cascades[id.package]; |
| 132 if (cascade != null) return cascade.getAssetNode(id); |
| 133 return new Future.value(null); |
| 134 }); |
| 135 } |
| 136 |
| 137 /// Returns the stream of newly-emitted assets for the given package's |
| 138 /// cascade. |
| 139 /// |
| 140 /// If there's no cascade for [package], returns `null`. |
| 141 Stream<AssetNode> onAssetFor(String package) { |
| 142 var cascade = _cascades[package]; |
| 143 return cascade == null ? null : cascade.onAsset; |
| 144 } |
| 145 |
| 146 /// Gets all output assets. |
| 147 /// |
| 148 /// If a build is currently in progress, waits until it completes. The |
| 149 /// returned future will complete with an error if the build is not |
| 150 /// successful. |
| 151 /// |
| 152 /// Any transforms using [LazyTransformer]s will be forced to generate |
| 153 /// concrete outputs, and those outputs will be returned. |
| 154 Future<AssetSet> getAllAssets() { |
| 155 for (var cascade in _cascades.values) { |
| 156 _inErrorZone(() => cascade.forceAllTransforms()); |
| 157 } |
| 158 |
| 159 if (_status != NodeStatus.IDLE) { |
| 160 // A build is still ongoing, so wait for it to complete and try again. |
| 161 return results.first.then((_) => getAllAssets()); |
| 162 } |
| 163 |
| 164 // If an unexpected error occurred, complete with that. |
| 165 if (_lastUnexpectedError != null) { |
| 166 var error = _lastUnexpectedError; |
| 167 _lastUnexpectedError = null; |
| 168 return new Future.error(error, _lastUnexpectedErrorTrace); |
| 169 } |
| 170 |
| 171 // If the last build completed with an error, complete the future with it. |
| 172 if (!_lastResult.succeeded) { |
| 173 return new Future.error(BarbackException.aggregate(_lastResult.errors)); |
| 174 } |
| 175 |
| 176 // Otherwise, return all of the final output assets. |
| 177 return Future.wait(_cascades.values.map( |
| 178 (cascade) => cascade.availableOutputs)) |
| 179 .then((assetSets) { |
| 180 var assets = unionAll(assetSets.map((assetSet) => assetSet.toSet())); |
| 181 return new Future.value(new AssetSet.from(assets)); |
| 182 }); |
| 183 } |
| 184 |
| 185 /// Adds [sources] to the graph's known set of source assets. |
| 186 /// |
| 187 /// Begins applying any transforms that can consume any of the sources. If a |
| 188 /// given source is already known, it is considered modified and all |
| 189 /// transforms that use it will be re-applied. |
| 190 void updateSources(Iterable<AssetId> sources) { |
| 191 groupBy(sources, (id) => id.package).forEach((package, ids) { |
| 192 var cascade = _cascades[package]; |
| 193 if (cascade == null) throw new ArgumentError("Unknown package $package."); |
| 194 _inErrorZone(() => cascade.updateSources(ids)); |
| 195 }); |
| 196 |
| 197 // It's possible for adding sources not to cause any processing. The user |
| 198 // still expects there to be a build, though, so we emit one immediately. |
| 199 _tryScheduleResult(); |
| 200 } |
| 201 |
| 202 /// Removes [removed] from the graph's known set of source assets. |
| 203 void removeSources(Iterable<AssetId> sources) { |
| 204 groupBy(sources, (id) => id.package).forEach((package, ids) { |
| 205 var cascade = _cascades[package]; |
| 206 if (cascade == null) throw new ArgumentError("Unknown package $package."); |
| 207 _inErrorZone(() => cascade.removeSources(ids)); |
| 208 }); |
| 209 |
| 210 // It's possible for removing sources not to cause any processing. The user |
| 211 // still expects there to be a build, though, so we emit one immediately. |
| 212 _tryScheduleResult(); |
| 213 } |
| 214 |
| 215 void updateTransformers(String package, Iterable<Iterable> transformers) { |
| 216 _inErrorZone(() => _cascades[package].updateTransformers(transformers)); |
| 217 |
| 218 // It's possible for updating transformers not to cause any processing. The |
| 219 // user still expects there to be a build, though, so we emit one |
| 220 // immediately. |
| 221 _tryScheduleResult(); |
| 222 } |
| 223 |
| 224 /// A handler for a log entry from an [AssetCascade]. |
| 225 void _onLog(LogEntry entry) { |
| 226 if (entry.level == LogLevel.ERROR) { |
| 227 // TODO(nweiz): keep track of stack chain. |
| 228 _accumulatedErrors.add( |
| 229 new TransformerException(entry.transform, entry.message, null)); |
| 230 } |
| 231 |
| 232 if (_logController.hasListener) { |
| 233 _logController.add(entry); |
| 234 } else if (entry.level != LogLevel.FINE) { |
| 235 // No listeners, so just print entry. |
| 236 var buffer = new StringBuffer(); |
| 237 buffer.write("[${entry.level} ${entry.transform}] "); |
| 238 |
| 239 if (entry.span != null) { |
| 240 buffer.write(entry.span.message(entry.message)); |
| 241 } else { |
| 242 buffer.write(entry.message); |
| 243 } |
| 244 |
| 245 print(buffer); |
| 246 } |
| 247 } |
| 248 |
| 249 /// If [this] is done processing, schedule a [BuildResult] to be emitted on |
| 250 /// [results]. |
| 251 /// |
| 252 /// This schedules the result (as opposed to just emitting one directly on |
| 253 /// [BuildResult]) to ensure that calling multiple functions synchronously |
| 254 /// produces only a single [BuildResult]. |
| 255 void _tryScheduleResult() { |
| 256 if (_status != NodeStatus.IDLE) return; |
| 257 if (_resultScheduled) return; |
| 258 |
| 259 _resultScheduled = true; |
| 260 newFuture(() { |
| 261 _resultScheduled = false; |
| 262 if (_status != NodeStatus.IDLE) return; |
| 263 |
| 264 _lastResult = new BuildResult(_accumulatedErrors); |
| 265 _accumulatedErrors.clear(); |
| 266 _resultsController.add(_lastResult); |
| 267 }); |
| 268 } |
| 269 |
| 270 /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to |
| 271 /// the error channel of [results]. |
| 272 /// |
| 273 /// [body] can return a value or a [Future] that will be piped to the returned |
| 274 /// [Future]. If it throws a [BarbackException], that exception will be piped |
| 275 /// to the returned [Future] as well. Any other exceptions will be piped to |
| 276 /// [results]. |
| 277 Future _inErrorZone(body()) { |
| 278 var completer = new Completer.sync(); |
| 279 runZoned(() { |
| 280 syncFuture(body).then(completer.complete).catchError((error, stackTrace) { |
| 281 if (error is! BarbackException) throw error; |
| 282 completer.completeError(error, stackTrace); |
| 283 }); |
| 284 }, onError: (error, stackTrace) { |
| 285 _lastUnexpectedError = error; |
| 286 _lastUnexpectedErrorTrace = stackTrace; |
| 287 _resultsController.addError(error, stackTrace); |
| 288 }); |
| 289 return completer.future; |
| 290 } |
| 291 } |
OLD | NEW |