| 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   /// Gets all output assets. |  | 
| 138   /// |  | 
| 139   /// If a build is currently in progress, waits until it completes. The |  | 
| 140   /// returned future will complete with an error if the build is not |  | 
| 141   /// successful. |  | 
| 142   /// |  | 
| 143   /// Any transforms using [LazyTransformer]s will be forced to generate |  | 
| 144   /// concrete outputs, and those outputs will be returned. |  | 
| 145   Future<AssetSet> getAllAssets() { |  | 
| 146     for (var cascade in _cascades.values) { |  | 
| 147       _inErrorZone(() => cascade.forceAllTransforms()); |  | 
| 148     } |  | 
| 149 |  | 
| 150     if (_status != NodeStatus.IDLE) { |  | 
| 151       // A build is still ongoing, so wait for it to complete and try again. |  | 
| 152       return results.first.then((_) => getAllAssets()); |  | 
| 153     } |  | 
| 154 |  | 
| 155     // If an unexpected error occurred, complete with that. |  | 
| 156     if (_lastUnexpectedError != null) { |  | 
| 157       var error = _lastUnexpectedError; |  | 
| 158       _lastUnexpectedError = null; |  | 
| 159       return new Future.error(error, _lastUnexpectedErrorTrace); |  | 
| 160     } |  | 
| 161 |  | 
| 162     // If the last build completed with an error, complete the future with it. |  | 
| 163     if (!_lastResult.succeeded) { |  | 
| 164       return new Future.error(BarbackException.aggregate(_lastResult.errors)); |  | 
| 165     } |  | 
| 166 |  | 
| 167     // Otherwise, return all of the final output assets. |  | 
| 168     return Future.wait(_cascades.values.map( |  | 
| 169             (cascade) => cascade.availableOutputs)) |  | 
| 170           .then((assetSets) { |  | 
| 171       var assets = unionAll(assetSets.map((assetSet) => assetSet.toSet())); |  | 
| 172       return new Future.value(new AssetSet.from(assets)); |  | 
| 173     }); |  | 
| 174   } |  | 
| 175 |  | 
| 176   /// Adds [sources] to the graph's known set of source assets. |  | 
| 177   /// |  | 
| 178   /// Begins applying any transforms that can consume any of the sources. If a |  | 
| 179   /// given source is already known, it is considered modified and all |  | 
| 180   /// transforms that use it will be re-applied. |  | 
| 181   void updateSources(Iterable<AssetId> sources) { |  | 
| 182     groupBy(sources, (id) => id.package).forEach((package, ids) { |  | 
| 183       var cascade = _cascades[package]; |  | 
| 184       if (cascade == null) throw new ArgumentError("Unknown package $package."); |  | 
| 185       _inErrorZone(() => cascade.updateSources(ids)); |  | 
| 186     }); |  | 
| 187 |  | 
| 188     // It's possible for adding sources not to cause any processing. The user |  | 
| 189     // still expects there to be a build, though, so we emit one immediately. |  | 
| 190     _tryScheduleResult(); |  | 
| 191   } |  | 
| 192 |  | 
| 193   /// Removes [removed] from the graph's known set of source assets. |  | 
| 194   void removeSources(Iterable<AssetId> sources) { |  | 
| 195     groupBy(sources, (id) => id.package).forEach((package, ids) { |  | 
| 196       var cascade = _cascades[package]; |  | 
| 197       if (cascade == null) throw new ArgumentError("Unknown package $package."); |  | 
| 198       _inErrorZone(() => cascade.removeSources(ids)); |  | 
| 199     }); |  | 
| 200 |  | 
| 201     // It's possible for removing sources not to cause any processing. The user |  | 
| 202     // still expects there to be a build, though, so we emit one immediately. |  | 
| 203     _tryScheduleResult(); |  | 
| 204   } |  | 
| 205 |  | 
| 206   void updateTransformers(String package, Iterable<Iterable> transformers) { |  | 
| 207     _inErrorZone(() => _cascades[package].updateTransformers(transformers)); |  | 
| 208 |  | 
| 209     // It's possible for updating transformers not to cause any processing. The |  | 
| 210     // user still expects there to be a build, though, so we emit one |  | 
| 211     // immediately. |  | 
| 212     _tryScheduleResult(); |  | 
| 213   } |  | 
| 214 |  | 
| 215   /// A handler for a log entry from an [AssetCascade]. |  | 
| 216   void _onLog(LogEntry entry) { |  | 
| 217     if (entry.level == LogLevel.ERROR) { |  | 
| 218       // TODO(nweiz): keep track of stack chain. |  | 
| 219       _accumulatedErrors.add( |  | 
| 220           new TransformerException(entry.transform, entry.message, null)); |  | 
| 221     } |  | 
| 222 |  | 
| 223     if (_logController.hasListener) { |  | 
| 224       _logController.add(entry); |  | 
| 225     } else if (entry.level != LogLevel.FINE) { |  | 
| 226       // No listeners, so just print entry. |  | 
| 227       var buffer = new StringBuffer(); |  | 
| 228       buffer.write("[${entry.level} ${entry.transform}] "); |  | 
| 229 |  | 
| 230       if (entry.span != null) { |  | 
| 231         buffer.write(entry.span.message(entry.message)); |  | 
| 232       } else { |  | 
| 233         buffer.write(entry.message); |  | 
| 234       } |  | 
| 235 |  | 
| 236       print(buffer); |  | 
| 237     } |  | 
| 238   } |  | 
| 239 |  | 
| 240   /// If [this] is done processing, schedule a [BuildResult] to be emitted on |  | 
| 241   /// [results]. |  | 
| 242   /// |  | 
| 243   /// This schedules the result (as opposed to just emitting one directly on |  | 
| 244   /// [BuildResult]) to ensure that calling multiple functions synchronously |  | 
| 245   /// produces only a single [BuildResult]. |  | 
| 246   void _tryScheduleResult() { |  | 
| 247     if (_status != NodeStatus.IDLE) return; |  | 
| 248     if (_resultScheduled) return; |  | 
| 249 |  | 
| 250     _resultScheduled = true; |  | 
| 251     newFuture(() { |  | 
| 252       _resultScheduled = false; |  | 
| 253       if (_status != NodeStatus.IDLE) return; |  | 
| 254 |  | 
| 255       _lastResult = new BuildResult(_accumulatedErrors); |  | 
| 256       _accumulatedErrors.clear(); |  | 
| 257       _resultsController.add(_lastResult); |  | 
| 258     }); |  | 
| 259   } |  | 
| 260 |  | 
| 261   /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to |  | 
| 262   /// the error channel of [results]. |  | 
| 263   /// |  | 
| 264   /// [body] can return a value or a [Future] that will be piped to the returned |  | 
| 265   /// [Future]. If it throws a [BarbackException], that exception will be piped |  | 
| 266   /// to the returned [Future] as well. Any other exceptions will be piped to |  | 
| 267   /// [results]. |  | 
| 268   Future _inErrorZone(body()) { |  | 
| 269     var completer = new Completer.sync(); |  | 
| 270     runZoned(() { |  | 
| 271       syncFuture(body).then(completer.complete).catchError((error, stackTrace) { |  | 
| 272         if (error is! BarbackException) throw error; |  | 
| 273         completer.completeError(error, stackTrace); |  | 
| 274       }); |  | 
| 275     }, onError: (error, stackTrace) { |  | 
| 276       _lastUnexpectedError = error; |  | 
| 277       _lastUnexpectedErrorTrace = stackTrace; |  | 
| 278       _resultsController.addError(error, stackTrace); |  | 
| 279     }); |  | 
| 280     return completer.future; |  | 
| 281   } |  | 
| 282 } |  | 
| OLD | NEW | 
|---|