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

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

Issue 16854005: First pass at build dependency graph for barback. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Revise. Created 7 years, 6 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
(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.transform_node;
6
7 import 'dart:async';
8
9 import '../barback.dart';
10 import '../transformer.dart';
11 import 'asset_graph.dart';
12 import 'asset_node.dart';
13 import 'phase.dart';
14
15 /// Describes a transform on a set of assets and its relationship to the build
16 /// dependency graph. Keeps track of whether it's dirty and needs to be run and
17 /// which primary asset it depends on.
nweiz 2013/06/18 23:14:46 "primary asset" -> "assets"
Bob Nystrom 2013/06/20 00:23:59 Done.
18 class TransformNode {
19 final Phase phase;
20 final Transformer transformer;
21 final AssetNode primary;
22 var isDirty = true;
nweiz 2013/06/18 23:14:46 These could use at least brief documentation. Als
Bob Nystrom 2013/06/20 00:23:59 Done.
23
24 /// The inputs read by this transform the last time it was run. Used to
25 /// tell if an input was removed in a later run.
26 var inputs = new Set<AssetNode>();
27
28 /// The outputs created by this transform the last time it was run. Used to
29 /// tell if an output was removed in a later run.
30 var outputs = new Set<AssetId>();
31
32 TransformNode(this.phase, this.transformer, this.primary);
33
34 /// Applies this transform.
35 ///
36 /// Returns a [TransformOutputs] describing the resulting outputs compared to
37 /// previous runs.
38 Future<TransformOutputs> apply() {
39 var transform = new _Transform(this);
40 return transformer.apply(transform).catchError((error) {
41 // Catch all transformer errors and pipe them to the results stream.
42 // This was a broken transformer doesn't take down the whole graph.
nweiz 2013/06/18 23:14:46 "was" -> "is so"
Bob Nystrom 2013/06/20 00:23:59 Done.
43 phase.graph.reportError(error);
44
45 // Don't allow partial results from a failed transform.
46 transform._outputs.clear();
47 }).then((_) {
48 isDirty = false;
49
50 // Stop watching any inputs that were removed.
51 for (var oldInput in inputs) {
52 oldInput.consumers.remove(this);
53 }
54
55 // Watch any new inputs so this transform will be re-processed when an
56 // input is modified.
57 for (var newInput in transform._inputs) {
58 newInput.consumers.add(this);
59 }
60
61 inputs = transform._inputs;
62
63 // See which outputs are missing from the last run.
64 var outputIds = transform._outputs.keys.toSet();
65 var removed = outputs.difference(outputIds);
66 outputs = outputIds;
67
68 return new TransformOutputs(transform._outputs, removed);
69 });
70 }
71 }
72
73 /// The result of running a [Transform], compared to the previous time it was
74 /// applied.
75 class TransformOutputs {
76 /// The outputs that are new or were modified since the last run.
77 final Map<AssetId, Asset> updated;
78
79 /// The outputs that were created by the previous run but were not generated
80 /// by the most recent run.
81 final Set<AssetId> removed;
82
83 TransformOutputs(this.updated, this.removed);
84 }
85
86 /// A concrete implementation of [Transform].
87 class _Transform implements Transform {
88 final TransformNode _node;
89
90 final _inputs = new Set<AssetNode>();
91 final _outputs = new Map<AssetId, Asset>();
92
93 AssetId get primaryId => _node.primary.id;
94 Future<Asset> get primaryInput => getInput(primaryId);
95
96 _Transform(this._node);
97
98 Future<Asset> getInput(AssetId id) {
99 return new Future(() {
100 var node = _node.phase.inputs[id];
101 // TODO(rnystrom): Need to handle passthrough where an asset from a
102 // previous phase can be found.
103
104 // Throw if the input isn't found. This ensures the transformer's apply
105 // is exited. We'll then catch this and report it through the proper
106 // results stream.
107 if (node == null) throw new MissingInputException(id);
108
109 // Keep track of which assets this transform depends on.
110 _inputs.add(node);
111 return node.asset;
112 });
113 }
114
115 void addOutput(AssetId id, Asset output) {
116 _outputs[id] = output;
117 }
118 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698