| 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.package_graph; | 5 library barback.package_graph; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:collection'; | 8 import 'dart:collection'; |
| 9 | 9 |
| 10 import 'asset_cascade.dart'; | 10 import 'asset_cascade.dart'; |
| 11 import 'asset_id.dart'; | 11 import 'asset_id.dart'; |
| 12 import 'asset_node.dart'; | 12 import 'asset_node.dart'; |
| 13 import 'asset_set.dart'; | 13 import 'asset_set.dart'; |
| 14 import 'build_result.dart'; | 14 import 'build_result.dart'; |
| 15 import 'errors.dart'; | 15 import 'errors.dart'; |
| 16 import 'log.dart'; | 16 import 'log.dart'; |
| 17 import 'node_status.dart'; |
| 17 import 'package_provider.dart'; | 18 import 'package_provider.dart'; |
| 18 import 'transformer.dart'; | 19 import 'transformer.dart'; |
| 19 import 'utils.dart'; | 20 import 'utils.dart'; |
| 20 | 21 |
| 21 /// The collection of [AssetCascade]s for an entire application. | 22 /// The collection of [AssetCascade]s for an entire application. |
| 22 /// | 23 /// |
| 23 /// This tracks each package's [AssetCascade] and routes asset requests between | 24 /// This tracks each package's [AssetCascade] and routes asset requests between |
| 24 /// them. | 25 /// them. |
| 25 class PackageGraph { | 26 class PackageGraph { |
| 26 /// The provider that exposes asset and package information. | 27 /// The provider that exposes asset and package information. |
| (...skipping 21 matching lines...) Expand all Loading... |
| 48 /// | 49 /// |
| 49 /// This will not emit programming errors from barback itself. Those will be | 50 /// This will not emit programming errors from barback itself. Those will be |
| 50 /// emitted through the [results] stream's error channel. | 51 /// emitted through the [results] stream's error channel. |
| 51 Stream<BarbackException> get errors => _errors; | 52 Stream<BarbackException> get errors => _errors; |
| 52 Stream<BarbackException> _errors; | 53 Stream<BarbackException> _errors; |
| 53 | 54 |
| 54 /// The stream of [LogEntry] objects used to report transformer log entries. | 55 /// The stream of [LogEntry] objects used to report transformer log entries. |
| 55 Stream<LogEntry> get log => _logController.stream; | 56 Stream<LogEntry> get log => _logController.stream; |
| 56 final _logController = new StreamController<LogEntry>.broadcast(sync: true); | 57 final _logController = new StreamController<LogEntry>.broadcast(sync: true); |
| 57 | 58 |
| 58 /// Whether [this] is dirty and still has more processing to do. | 59 /// How far along [this] is in processing its assets. |
| 59 bool get _isDirty => _cascades.values.any((cascade) => cascade.isDirty); | 60 NodeStatus get _status => NodeStatus.dirtiest( |
| 61 _cascades.values.map((cascade) => cascade.status)); |
| 60 | 62 |
| 61 /// Whether a [BuildResult] is scheduled to be emitted on [results] (see | 63 /// Whether a [BuildResult] is scheduled to be emitted on [results] (see |
| 62 /// [_tryScheduleResult]). | 64 /// [_tryScheduleResult]). |
| 63 bool _resultScheduled = false; | 65 bool _resultScheduled = false; |
| 64 | 66 |
| 65 /// The most recent [BuildResult] emitted on [results]. | 67 /// The most recent [BuildResult] emitted on [results]. |
| 66 BuildResult _lastResult; | 68 BuildResult _lastResult; |
| 67 | 69 |
| 68 // TODO(nweiz): This can have bogus errors if an error is created and resolved | 70 // TODO(nweiz): This can have bogus errors if an error is created and resolved |
| 69 // in the space of one build. | 71 // in the space of one build. |
| (...skipping 12 matching lines...) Expand all Loading... |
| 82 StackTrace _lastUnexpectedErrorTrace; | 84 StackTrace _lastUnexpectedErrorTrace; |
| 83 | 85 |
| 84 /// Creates a new [PackageGraph] that will transform assets in all packages | 86 /// Creates a new [PackageGraph] that will transform assets in all packages |
| 85 /// made available by [provider]. | 87 /// made available by [provider]. |
| 86 PackageGraph(this.provider) { | 88 PackageGraph(this.provider) { |
| 87 _inErrorZone(() { | 89 _inErrorZone(() { |
| 88 for (var package in provider.packages) { | 90 for (var package in provider.packages) { |
| 89 var cascade = new AssetCascade(this, package); | 91 var cascade = new AssetCascade(this, package); |
| 90 _cascades[package] = cascade; | 92 _cascades[package] = cascade; |
| 91 cascade.onLog.listen(_onLog); | 93 cascade.onLog.listen(_onLog); |
| 92 cascade.onDone.listen((_) => _tryScheduleResult()); | 94 cascade.onStatusChange.listen((status) { |
| 95 if (status == NodeStatus.IDLE) _tryScheduleResult(); |
| 96 }); |
| 93 } | 97 } |
| 94 | 98 |
| 95 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors), | 99 _errors = mergeStreams(_cascades.values.map((cascade) => cascade.errors), |
| 96 broadcast: true); | 100 broadcast: true); |
| 97 _errors.listen(_accumulatedErrors.add); | 101 _errors.listen(_accumulatedErrors.add); |
| 98 }); | 102 }); |
| 99 } | 103 } |
| 100 | 104 |
| 101 /// Gets the asset node identified by [id]. | 105 /// Gets the asset node identified by [id]. |
| 102 /// | 106 /// |
| (...skipping 16 matching lines...) Expand all Loading... |
| 119 /// returned future will complete with an error if the build is not | 123 /// returned future will complete with an error if the build is not |
| 120 /// successful. | 124 /// successful. |
| 121 /// | 125 /// |
| 122 /// Any transforms using [LazyTransformer]s will be forced to generate | 126 /// Any transforms using [LazyTransformer]s will be forced to generate |
| 123 /// concrete outputs, and those outputs will be returned. | 127 /// concrete outputs, and those outputs will be returned. |
| 124 Future<AssetSet> getAllAssets() { | 128 Future<AssetSet> getAllAssets() { |
| 125 for (var cascade in _cascades.values) { | 129 for (var cascade in _cascades.values) { |
| 126 _inErrorZone(() => cascade.forceAllTransforms()); | 130 _inErrorZone(() => cascade.forceAllTransforms()); |
| 127 } | 131 } |
| 128 | 132 |
| 129 if (_isDirty) { | 133 if (_status != NodeStatus.IDLE) { |
| 130 // A build is still ongoing, so wait for it to complete and try again. | 134 // A build is still ongoing, so wait for it to complete and try again. |
| 131 return results.first.then((_) => getAllAssets()); | 135 return results.first.then((_) => getAllAssets()); |
| 132 } | 136 } |
| 133 | 137 |
| 134 // If an unexpected error occurred, complete with that. | 138 // If an unexpected error occurred, complete with that. |
| 135 if (_lastUnexpectedError != null) { | 139 if (_lastUnexpectedError != null) { |
| 136 var error = _lastUnexpectedError; | 140 var error = _lastUnexpectedError; |
| 137 _lastUnexpectedError = null; | 141 _lastUnexpectedError = null; |
| 138 return new Future.error(error, _lastUnexpectedErrorTrace); | 142 return new Future.error(error, _lastUnexpectedErrorTrace); |
| 139 } | 143 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 215 } | 219 } |
| 216 } | 220 } |
| 217 | 221 |
| 218 /// If [this] is done processing, schedule a [BuildResult] to be emitted on | 222 /// If [this] is done processing, schedule a [BuildResult] to be emitted on |
| 219 /// [results]. | 223 /// [results]. |
| 220 /// | 224 /// |
| 221 /// This schedules the result (as opposed to just emitting one directly on | 225 /// This schedules the result (as opposed to just emitting one directly on |
| 222 /// [BuildResult]) to ensure that calling multiple functions synchronously | 226 /// [BuildResult]) to ensure that calling multiple functions synchronously |
| 223 /// produces only a single [BuildResult]. | 227 /// produces only a single [BuildResult]. |
| 224 void _tryScheduleResult() { | 228 void _tryScheduleResult() { |
| 225 if (_isDirty) return; | 229 if (_status != NodeStatus.IDLE) return; |
| 226 if (_resultScheduled) return; | 230 if (_resultScheduled) return; |
| 227 | 231 |
| 228 _resultScheduled = true; | 232 _resultScheduled = true; |
| 229 newFuture(() { | 233 newFuture(() { |
| 230 _resultScheduled = false; | 234 _resultScheduled = false; |
| 231 if (_isDirty) return; | 235 if (_status != NodeStatus.IDLE) return; |
| 232 | 236 |
| 233 _lastResult = new BuildResult(_accumulatedErrors); | 237 _lastResult = new BuildResult(_accumulatedErrors); |
| 234 _accumulatedErrors.clear(); | 238 _accumulatedErrors.clear(); |
| 235 _resultsController.add(_lastResult); | 239 _resultsController.add(_lastResult); |
| 236 }); | 240 }); |
| 237 } | 241 } |
| 238 | 242 |
| 239 /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to | 243 /// Run [body] in an error-handling [Zone] and pipe any unexpected errors to |
| 240 /// the error channel of [results]. | 244 /// the error channel of [results]. |
| 241 /// | 245 /// |
| 242 /// [body] can return a value or a [Future] that will be piped to the returned | 246 /// [body] can return a value or a [Future] that will be piped to the returned |
| 243 /// [Future]. If it throws a [BarbackException], that exception will be piped | 247 /// [Future]. If it throws a [BarbackException], that exception will be piped |
| 244 /// to the returned [Future] as well. Any other exceptions will be piped to | 248 /// to the returned [Future] as well. Any other exceptions will be piped to |
| 245 /// [results]. | 249 /// [results]. |
| 246 Future _inErrorZone(body()) { | 250 Future _inErrorZone(body()) { |
| 247 var completer = new Completer.sync(); | 251 var completer = new Completer.sync(); |
| 248 runZoned(() { | 252 runZoned(() { |
| 249 syncFuture(body).then(completer.complete).catchError((error, stackTrace) { | 253 syncFuture(body).then(completer.complete).catchError((error, stackTrace) { |
| 250 if (error is! BarbackException) throw error; | 254 if (error is! BarbackException) throw error; |
| 251 completer.completeError(error, stackTrace); | 255 completer.completeError(error, stackTrace); |
| 252 }); | 256 }); |
| 253 }, onError: (error, stackTrace) { | 257 }, onError: (error, stackTrace) { |
| 254 _lastUnexpectedError = error; | 258 _lastUnexpectedError = error; |
| 255 _lastUnexpectedErrorTrace = stackTrace; | 259 _lastUnexpectedErrorTrace = stackTrace; |
| 256 _resultsController.addError(error, stackTrace); | 260 _resultsController.addError(error, stackTrace); |
| 257 }); | 261 }); |
| 258 return completer.future; | 262 return completer.future; |
| 259 } | 263 } |
| 260 } | 264 } |
| OLD | NEW |