| 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.package_graph; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import 'dart:collection'; | |
| 9 | |
| 10 import 'asset_cascade.dart'; | |
| 11 import 'asset_id.dart'; | |
| 12 import 'asset_node.dart'; | |
| 13 import 'asset_set.dart'; | |
| 14 import 'build_result.dart'; | |
| 15 import 'errors.dart'; | |
| 16 import 'log.dart'; | |
| 17 import 'node_status.dart'; | |
| 18 import 'package_provider.dart'; | |
| 19 import 'transformer.dart'; | |
| 20 import 'utils.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 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors), | |
| 100 broadcast: true); | |
| 101 _errors.listen(_accumulatedErrors.add); | |
| 102 }); | |
| 103 } | |
| 104 | |
| 105 /// Gets the asset node identified by [id]. | |
| 106 /// | |
| 107 /// If [id] is for a generated or transformed asset, this will wait until it | |
| 108 /// has been created and return it. This means that the returned asset will | |
| 109 /// always be [AssetState.AVAILABLE]. | |
| 110 /// | |
| 111 /// If the asset cannot be found, returns null. | |
| 112 Future<AssetNode> getAssetNode(AssetId id) { | |
| 113 return _inErrorZone(() { | |
| 114 var cascade = _cascades[id.package]; | |
| 115 if (cascade != null) return cascade.getAssetNode(id); | |
| 116 return new Future.value(null); | |
| 117 }); | |
| 118 } | |
| 119 | |
| 120 /// Gets all output assets. | |
| 121 /// | |
| 122 /// If a build is currently in progress, waits until it completes. The | |
| 123 /// returned future will complete with an error if the build is not | |
| 124 /// successful. | |
| 125 /// | |
| 126 /// Any transforms using [LazyTransformer]s will be forced to generate | |
| 127 /// concrete outputs, and those outputs will be returned. | |
| 128 Future<AssetSet> getAllAssets() { | |
| 129 for (var cascade in _cascades.values) { | |
| 130 _inErrorZone(() => cascade.forceAllTransforms()); | |
| 131 } | |
| 132 | |
| 133 if (_status != NodeStatus.IDLE) { | |
| 134 // A build is still ongoing, so wait for it to complete and try again. | |
| 135 return results.first.then((_) => getAllAssets()); | |
| 136 } | |
| 137 | |
| 138 // If an unexpected error occurred, complete with that. | |
| 139 if (_lastUnexpectedError != null) { | |
| 140 var error = _lastUnexpectedError; | |
| 141 _lastUnexpectedError = null; | |
| 142 return new Future.error(error, _lastUnexpectedErrorTrace); | |
| 143 } | |
| 144 | |
| 145 // If the last build completed with an error, complete the future with it. | |
| 146 if (!_lastResult.succeeded) { | |
| 147 return new Future.error(BarbackException.aggregate(_lastResult.errors)); | |
| 148 } | |
| 149 | |
| 150 // Otherwise, return all of the final output assets. | |
| 151 var assets = unionAll(_cascades.values.map( | |
| 152 (cascade) => cascade.availableOutputs.toSet())); | |
| 153 | |
| 154 return new Future.value(new AssetSet.from(assets)); | |
| 155 } | |
| 156 | |
| 157 /// Adds [sources] to the graph's known set of source assets. | |
| 158 /// | |
| 159 /// Begins applying any transforms that can consume any of the sources. If a | |
| 160 /// given source is already known, it is considered modified and all | |
| 161 /// transforms that use it will be re-applied. | |
| 162 void updateSources(Iterable<AssetId> sources) { | |
| 163 groupBy(sources, (id) => id.package).forEach((package, ids) { | |
| 164 var cascade = _cascades[package]; | |
| 165 if (cascade == null) throw new ArgumentError("Unknown package $package."); | |
| 166 _inErrorZone(() => cascade.updateSources(ids)); | |
| 167 }); | |
| 168 | |
| 169 // It's possible for adding sources not to cause any processing. The user | |
| 170 // still expects there to be a build, though, so we emit one immediately. | |
| 171 _tryScheduleResult(); | |
| 172 } | |
| 173 | |
| 174 /// Removes [removed] from the graph's known set of source assets. | |
| 175 void removeSources(Iterable<AssetId> sources) { | |
| 176 groupBy(sources, (id) => id.package).forEach((package, ids) { | |
| 177 var cascade = _cascades[package]; | |
| 178 if (cascade == null) throw new ArgumentError("Unknown package $package."); | |
| 179 _inErrorZone(() => cascade.removeSources(ids)); | |
| 180 }); | |
| 181 | |
| 182 // It's possible for removing sources not to cause any processing. The user | |
| 183 // still expects there to be a build, though, so we emit one immediately. | |
| 184 _tryScheduleResult(); | |
| 185 } | |
| 186 | |
| 187 void updateTransformers(String package, | |
| 188 Iterable<Iterable<Transformer>> transformers) { | |
| 189 _inErrorZone(() => _cascades[package].updateTransformers(transformers)); | |
| 190 | |
| 191 // It's possible for updating transformers not to cause any processing. The | |
| 192 // user still expects there to be a build, though, so we emit one | |
| 193 // immediately. | |
| 194 _tryScheduleResult(); | |
| 195 } | |
| 196 | |
| 197 /// A handler for a log entry from an [AssetCascade]. | |
| 198 void _onLog(LogEntry entry) { | |
| 199 if (entry.level == LogLevel.ERROR) { | |
| 200 // TODO(nweiz): keep track of stack chain. | |
| 201 _accumulatedErrors.add( | |
| 202 new TransformerException(entry.transform, entry.message, null)); | |
| 203 } | |
| 204 | |
| 205 if (_logController.hasListener) { | |
| 206 _logController.add(entry); | |
| 207 } else if (entry.level != LogLevel.FINE) { | |
| 208 // No listeners, so just print entry. | |
| 209 var buffer = new StringBuffer(); | |
| 210 buffer.write("[${entry.level} ${entry.transform}] "); | |
| 211 | |
| 212 if (entry.span != null) { | |
| 213 buffer.write(entry.span.getLocationMessage(entry.message)); | |
| 214 } else { | |
| 215 buffer.write(entry.message); | |
| 216 } | |
| 217 | |
| 218 print(buffer); | |
| 219 } | |
| 220 } | |
| 221 | |
| 222 /// If [this] is done processing, schedule a [BuildResult] to be emitted on | |
| 223 /// [results]. | |
| 224 /// | |
| 225 /// This schedules the result (as opposed to just emitting one directly on | |
| 226 /// [BuildResult]) to ensure that calling multiple functions synchronously | |
| 227 /// produces only a single [BuildResult]. | |
| 228 void _tryScheduleResult() { | |
| 229 if (_status != NodeStatus.IDLE) return; | |
| 230 if (_resultScheduled) return; | |
| 231 | |
| 232 _resultScheduled = true; | |
| 233 newFuture(() { | |
| 234 _resultScheduled = false; | |
| 235 if (_status != NodeStatus.IDLE) return; | |
| 236 | |
| 237 _lastResult = new BuildResult(_accumulatedErrors); | |
| 238 _accumulatedErrors.clear(); | |
| 239 _resultsController.add(_lastResult); | |
| 240 }); | |
| 241 } | |
| 242 | |
| 243 /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to | |
| 244 /// the error channel of [results]. | |
| 245 /// | |
| 246 /// [body] can return a value or a [Future] that will be piped to the returned | |
| 247 /// [Future]. If it throws a [BarbackException], that exception will be piped | |
| 248 /// to the returned [Future] as well. Any other exceptions will be piped to | |
| 249 /// [results]. | |
| 250 Future _inErrorZone(body()) { | |
| 251 var completer = new Completer.sync(); | |
| 252 runZoned(() { | |
| 253 syncFuture(body).then(completer.complete).catchError((error, stackTrace) { | |
| 254 if (error is! BarbackException) throw error; | |
| 255 completer.completeError(error, stackTrace); | |
| 256 }); | |
| 257 }, onError: (error, stackTrace) { | |
| 258 _lastUnexpectedError = error; | |
| 259 _lastUnexpectedErrorTrace = stackTrace; | |
| 260 _resultsController.addError(error, stackTrace); | |
| 261 }); | |
| 262 return completer.future; | |
| 263 } | |
| 264 } | |
| OLD | NEW |