Skip to content

Commit 0bc9e3f

Browse files
committed
list all assets up front, and only add outputs after each entire group is done
1 parent f382599 commit 0bc9e3f

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

lib/src/generate/build.dart

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import '../builder/builder.dart';
1616
import '../builder/build_step_impl.dart';
1717
import '../package_graph/package_graph.dart';
1818
import 'build_result.dart';
19+
import 'input_set.dart';
1920
import 'phase.dart';
2021

2122
/// Runs all of the [Phases] in [phaseGroups].
@@ -75,23 +76,71 @@ PackageGraph get _packageGraph => Zone.current[_packageGraphKey];
7576
/// Runs the [phaseGroups] and returns a [Future<BuildResult>] which completes
7677
/// once all [Phase]s are done.
7778
Future<BuildResult> _runPhases(List<List<Phase>> phaseGroups) async {
78-
var outputs = [];
79+
final allInputs = await _allInputs(phaseGroups);
80+
final outputs = <Asset>[];
7981
for (var group in phaseGroups) {
82+
final groupOutputs = <Asset>[];
8083
for (var phase in group) {
81-
var inputs = (await _reader.listAssetIds(phase.inputSets).toList())
82-
.where(_isValidInput);
84+
var inputs = _matchingInputs(allInputs, phase.inputSets);
8385
for (var builder in phase.builders) {
8486
// TODO(jakemac): Optimize, we can run all the builders in a phase
8587
// at the same time instead of sequentially.
8688
await for (var output in _runBuilder(builder, inputs)) {
89+
groupOutputs.add(output);
8790
outputs.add(output);
8891
}
8992
}
9093
}
94+
/// Once the group is done, add all outputs so they can be used in the next
95+
/// phase.
96+
for (var output in groupOutputs) {
97+
allInputs.putIfAbsent(output.id.package, () => new Set<AssetId>());
98+
allInputs[output.id.package].add(output.id);
99+
}
91100
}
92101
return new BuildResult(BuildStatus.Success, BuildType.Full, outputs);
93102
}
94103

104+
/// Returns a map of all the available inputs by package.
105+
Future<Map<String, Set<AssetId>>> _allInputs(
106+
List<List<Phase>> phaseGroups) async {
107+
final packages = new Set<String>();
108+
for (var group in phaseGroups) {
109+
for (var phase in group) {
110+
for (var inputSet in phase.inputSets) {
111+
packages.add(inputSet.package);
112+
}
113+
}
114+
}
115+
116+
var inputSets = packages.map((package) => new InputSet(package));
117+
var allInputs = await _reader.listAssetIds(inputSets).toList();
118+
var inputsByPackage = {};
119+
for (var input in allInputs) {
120+
inputsByPackage.putIfAbsent(input.package, () => new Set<AssetId>());
121+
122+
if (_isValidInput(input)) {
123+
inputsByPackage[input.package].add(input);
124+
}
125+
}
126+
return inputsByPackage;
127+
}
128+
129+
/// Gets a list of all inputs matching [inputSets] given [allInputs].
130+
Set<AssetId> _matchingInputs(
131+
Map<String, Set<AssetId>> inputsByPackage, Iterable<InputSet> inputSets) {
132+
var inputs = new Set<AssetId>();
133+
for (var inputSet in inputSets) {
134+
assert(inputsByPackage.containsKey(inputSet.package));
135+
for (var input in inputsByPackage[inputSet.package]) {
136+
if (inputSet.globs.any((g) => g.matches(input.path))) {
137+
inputs.add(input);
138+
}
139+
}
140+
}
141+
return inputs;
142+
}
143+
95144
/// Checks if an [input] is valid.
96145
bool _isValidInput(AssetId input) {
97146
var parts = path.split(input.path);

test/generate/build_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ main() {
5858
'a|lib/b.txt.copy.0': 'b',
5959
'a|lib/b.txt.copy.1': 'b',
6060
});
61-
}, skip: 'Failing, outputs from phases in same group are available');
61+
});
6262

6363
test('multiple phases, multiple builders', () async {
6464
var phases = [
@@ -174,7 +174,7 @@ main() {
174174

175175
test('outputs from previous full builds shouldn\'t be inputs to later ones',
176176
() {},
177-
skip: 'Unimplemented');
177+
skip: 'Unimplemented: https://github.com/dart-lang/build/issues/34');
178178
}
179179

180180
testPhases(List<List<Phase>> phases, Map<String, String> inputs,

0 commit comments

Comments
 (0)