Skip to content

Commit 3f16290

Browse files
authored
Use built_value for AssetNode and related types. (#3914)
* Use `built_value` for `AssetNode` and related types. * Clearer `_buildGlobNode`.
1 parent 113e278 commit 3f16290

31 files changed

+2296
-762
lines changed

_test_common/lib/matchers.dart

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,9 @@ class _AssetGraphMatcher extends Matcher {
8787
}
8888
if (node.type == NodeType.generated) {
8989
if (expectedNode.type == NodeType.generated) {
90-
final configuration = node.generatedNodeConfiguration;
91-
final expectedConfiguration = expectedNode.generatedNodeConfiguration;
90+
final configuration = node.generatedNodeConfiguration!;
91+
final expectedConfiguration =
92+
expectedNode.generatedNodeConfiguration!;
9293

9394
if (configuration.primaryInput !=
9495
expectedConfiguration.primaryInput) {
@@ -99,8 +100,8 @@ class _AssetGraphMatcher extends Matcher {
99100
matches = false;
100101
}
101102

102-
final state = node.generatedNodeState;
103-
final expectedState = expectedNode.generatedNodeState;
103+
final state = node.generatedNodeState!;
104+
final expectedState = expectedNode.generatedNodeState!;
104105

105106
if (state.pendingBuildAction != expectedState.pendingBuildAction) {
106107
matchState['pendingBuildAction of ${node.id}'] = [
@@ -145,8 +146,8 @@ class _AssetGraphMatcher extends Matcher {
145146
}
146147
} else if (node.type == NodeType.glob) {
147148
if (expectedNode.type == NodeType.glob) {
148-
final state = node.globNodeState;
149-
final expectedState = expectedNode.globNodeState;
149+
final state = node.globNodeState!;
150+
final expectedState = expectedNode.globNodeState!;
150151

151152
if (state.pendingBuildAction != expectedState.pendingBuildAction) {
152153
matchState['pendingBuildAction of ${node.id}'] = [
@@ -166,7 +167,7 @@ class _AssetGraphMatcher extends Matcher {
166167
}
167168

168169
if (!unorderedEquals(
169-
state.results!,
170+
state.results,
170171
).matches(expectedState.results, {})) {
171172
matchState['results of ${node.id}'] = [
172173
state.results,
@@ -175,8 +176,8 @@ class _AssetGraphMatcher extends Matcher {
175176
matches = false;
176177
}
177178

178-
final configuration = node.globNodeConfiguration;
179-
final expectedConfiguration = expectedNode.globNodeConfiguration;
179+
final configuration = node.globNodeConfiguration!;
180+
final expectedConfiguration = expectedNode.globNodeConfiguration!;
180181
if (configuration.glob.pattern !=
181182
expectedConfiguration.glob.pattern) {
182183
matchState['glob of ${node.id}'] = [
@@ -188,9 +189,9 @@ class _AssetGraphMatcher extends Matcher {
188189
}
189190
} else if (node.type == NodeType.postProcessAnchor) {
190191
if (expectedNode.type == NodeType.postProcessAnchor) {
191-
final nodeConfiguration = node.postProcessAnchorNodeConfiguration;
192+
final nodeConfiguration = node.postProcessAnchorNodeConfiguration!;
192193
final expectedNodeConfiguration =
193-
expectedNode.postProcessAnchorNodeConfiguration;
194+
expectedNode.postProcessAnchorNodeConfiguration!;
194195
if (nodeConfiguration.actionNumber !=
195196
expectedNodeConfiguration.actionNumber) {
196197
matchState['actionNumber of ${node.id}'] = [
@@ -207,8 +208,8 @@ class _AssetGraphMatcher extends Matcher {
207208
];
208209
matches = false;
209210
}
210-
final nodeState = node.postProcessAnchorNodeState;
211-
final expectedNodeState = expectedNode.postProcessAnchorNodeState;
211+
final nodeState = node.postProcessAnchorNodeState!;
212+
final expectedNodeState = expectedNode.postProcessAnchorNodeState!;
212213
if (checkPreviousInputsDigest &&
213214
nodeState.previousInputsDigest !=
214215
expectedNodeState.previousInputsDigest) {
@@ -250,8 +251,9 @@ class _AssetGraphMatcher extends Matcher {
250251
bool verbose,
251252
) {
252253
matchState.forEach((k, v) {
253-
v = v as List;
254-
mismatchDescription.add('$k: got ${v[0]} but expected ${v[1]}');
254+
if (v is List) {
255+
mismatchDescription.add('$k: got ${v[0]} but expected ${v[1]}');
256+
}
255257
});
256258

257259
return mismatchDescription;

build_runner/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- Refactor `FileBasedAssetReader` and `FileBasedAssetWriter` to `ReaderWriter`.
1010
- Remove `OnDeleteWriter`, add functionality to `ReaderWriter`.
1111
- Add `NodeType` to `AssetNode`, remove subtypes. Make mutations explicit.
12+
- Use `built_value` for `AssetNode` and related types.
1213

1314
## 2.4.15
1415

build_runner/bin/graph_inspector.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ class InspectNodeCommand extends Command<bool> {
140140
..writeln(' type: ${node.runtimeType}');
141141

142142
if (node.type == NodeType.generated) {
143-
final nodeState = node.generatedNodeState;
144-
final nodeConfiguration = node.generatedNodeConfiguration;
143+
final nodeState = node.generatedNodeState!;
144+
final nodeConfiguration = node.generatedNodeConfiguration!;
145145
description
146146
..writeln(' state: ${nodeState.pendingBuildAction}')
147147
..writeln(' wasOutput: ${nodeState.wasOutput}')
@@ -157,9 +157,7 @@ class InspectNodeCommand extends Command<bool> {
157157
node.primaryOutputs.forEach(printAsset);
158158

159159
description.writeln(' secondary outputs:');
160-
node.inspect.outputs
161-
.difference(node.inspect.primaryOutputs)
162-
.forEach(printAsset);
160+
node.outputs.difference(node.primaryOutputs).forEach(printAsset);
163161

164162
if (node.type == NodeType.generated || node.type == NodeType.glob) {
165163
description.writeln(' inputs:');

build_runner/lib/src/entrypoint/clean.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ Future<void> _cleanUpSourceOutputs(
8686
for (var id in assetGraph.outputs) {
8787
if (id.package != packageGraph.root.name) continue;
8888
var node = assetGraph.get(id)!;
89-
if (node.generatedNodeState.wasOutput) {
89+
if (node.generatedNodeState!.wasOutput) {
9090
// Note that this does a file.exists check in the root package and
9191
// only tries to delete the file if it exists. This way we only
9292
// actually delete to_source outputs, without reading in the build

build_runner/lib/src/server/asset_graph_handler.dart

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ class AssetGraphHandler {
129129
if (node.type == NodeType.generated || node.type == NodeType.glob) {
130130
final inputs =
131131
node.type == NodeType.generated
132-
? node.generatedNodeState.inputs
133-
: node.globNodeState.inputs;
132+
? node.generatedNodeState!.inputs
133+
: node.globNodeState!.inputs;
134134
for (final input in inputs) {
135135
if (filterGlob != null && !filterGlob.matches(input.toString())) {
136136
continue;
@@ -148,32 +148,32 @@ class AssetGraphHandler {
148148
'id': '${node.id}',
149149
'hidden':
150150
node.type == NodeType.generated
151-
? node.generatedNodeConfiguration.isHidden
151+
? node.generatedNodeConfiguration!.isHidden
152152
: null,
153153
'state':
154154
node.type == NodeType.generated
155-
? '${node.generatedNodeState.pendingBuildAction}'
155+
? '${node.generatedNodeState!.pendingBuildAction}'
156156
: node.type == NodeType.glob
157-
? '${node.globNodeState.pendingBuildAction}'
157+
? '${node.globNodeState!.pendingBuildAction}'
158158
: null,
159159
'wasOutput':
160160
node.type == NodeType.generated
161-
? node.generatedNodeState.wasOutput
161+
? node.generatedNodeState!.wasOutput
162162
: null,
163163
'isFailure':
164164
node.type == NodeType.generated
165-
? node.generatedNodeState.isFailure
165+
? node.generatedNodeState!.isFailure
166166
: null,
167167
'phaseNumber':
168168
node.type == NodeType.generated
169-
? node.generatedNodeConfiguration.phaseNumber
169+
? node.generatedNodeConfiguration!.phaseNumber
170170
: node.type == NodeType.glob
171-
? node.globNodeConfiguration.phaseNumber
171+
? node.globNodeConfiguration!.phaseNumber
172172
: null,
173173
'type': node.runtimeType.toString(),
174174
'glob':
175175
node.type == NodeType.glob
176-
? node.globNodeConfiguration.glob.pattern
176+
? node.globNodeConfiguration!.glob.pattern
177177
: null,
178178
'lastKnownDigest': node.lastKnownDigest.toString(),
179179
},

build_runner/test/generate/watch_test.dart

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,23 +366,24 @@ void main() {
366366
builderOptionsId,
367367
lastKnownDigest: computeBuilderOptionsDigest(defaultBuilderOptions),
368368
);
369-
expectedGraph.add(builderOptionsNode);
370369

371370
var bCopyId = makeAssetId('a|web/b.txt.copy');
372371
var bTxtId = makeAssetId('a|web/b.txt');
373372
var bCopyNode = AssetNode.generated(
374373
bCopyId,
375374
phaseNumber: 0,
376375
primaryInput: makeAssetId('a|web/b.txt'),
377-
state: PendingBuildAction.none,
376+
pendingBuildAction: PendingBuildAction.none,
378377
wasOutput: true,
379378
isFailure: false,
380379
builderOptionsId: builderOptionsId,
381380
lastKnownDigest: computeDigest(bCopyId, 'b2'),
382381
inputs: [makeAssetId('a|web/b.txt')],
383382
isHidden: false,
384383
);
385-
builderOptionsNode.mutate.outputs.add(bCopyNode.id);
384+
builderOptionsNode = builderOptionsNode.rebuild(
385+
(b) => b..outputs.add(bCopyNode.id),
386+
);
386387
expectedGraph
387388
..add(bCopyNode)
388389
..add(
@@ -397,15 +398,17 @@ void main() {
397398
cCopyId,
398399
phaseNumber: 0,
399400
primaryInput: cTxtId,
400-
state: PendingBuildAction.none,
401+
pendingBuildAction: PendingBuildAction.none,
401402
wasOutput: true,
402403
isFailure: false,
403404
builderOptionsId: builderOptionsId,
404405
lastKnownDigest: computeDigest(cCopyId, 'c'),
405406
inputs: [makeAssetId('a|web/c.txt')],
406407
isHidden: false,
407408
);
408-
builderOptionsNode.mutate.outputs.add(cCopyNode.id);
409+
builderOptionsNode = builderOptionsNode.rebuild(
410+
(b) => b..outputs.add(cCopyNode.id),
411+
);
409412
expectedGraph
410413
..add(cCopyNode)
411414
..add(
@@ -414,6 +417,8 @@ void main() {
414417
], computeDigest(cTxtId, 'c')),
415418
);
416419

420+
expectedGraph.add(builderOptionsNode);
421+
417422
// TODO: We dont have a shared way of computing the combined input
418423
// hashes today, but eventually we should test those here too.
419424
expect(

build_runner/test/server/asset_handler_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ void main() {
4747
void addAsset(String id, String content, {bool deleted = false}) {
4848
var node = makeAssetNode(id, [], computeDigest(AssetId.parse(id), 'a'));
4949
if (deleted) {
50-
node.mutate.deletedBy.add(node.id.addExtension('.post_anchor.1'));
50+
node = node.rebuild(
51+
(b) => b..deletedBy.add(node.id.addExtension('.post_anchor.1')),
52+
);
5153
}
5254
graph.add(node);
5355
delegate.testing.writeString(node.id, content);
@@ -132,7 +134,7 @@ void main() {
132134
AssetId('a', 'web/main.ddc.js'),
133135
builderOptionsId: AssetId('_\$fake', 'options_id'),
134136
phaseNumber: 0,
135-
state: PendingBuildAction.none,
137+
pendingBuildAction: PendingBuildAction.none,
136138
isHidden: false,
137139
wasOutput: true,
138140
isFailure: true,

build_runner/test/server/serve_handler_test.dart

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,9 @@ void main() {
138138
void addSource(String id, String content, {bool deleted = false}) {
139139
var node = makeAssetNode(id, [], computeDigest(AssetId.parse(id), content));
140140
if (deleted) {
141-
node.mutate.deletedBy.add(node.id.addExtension('.post_anchor.1'));
141+
node = node.rebuild(
142+
(b) => b..deletedBy.add(node.id.addExtension('.post_anchor.1')),
143+
);
142144
}
143145
assetGraph.add(node);
144146
readerWriter.testing.writeString(node.id, content);
@@ -205,7 +207,7 @@ void main() {
205207
AssetId('a', 'web/main.ddc.js'),
206208
builderOptionsId: AssetId('_\$fake', 'options_id'),
207209
phaseNumber: 0,
208-
state: PendingBuildAction.none,
210+
pendingBuildAction: PendingBuildAction.none,
209211
isHidden: false,
210212
wasOutput: true,
211213
isFailure: true,

build_runner_core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
- Refactor `SingleStepReader` to `SingleStepReaderWriter`, incorporating
1818
`AssetWriterSpy` functionality.
1919
- Add `NodeType` to `AssetNode`, remove subtypes. Make mutations explicit.
20+
- Use `built_value` for `AssetNode` and related types.
2021

2122
## 8.0.0
2223

build_runner_core/lib/src/asset/build_cache.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class BuildCacheAssetPathProvider implements AssetPathProvider {
3838
}
3939
final assetNode = _assetGraph.get(id)!;
4040
if (assetNode.type == NodeType.generated &&
41-
assetNode.generatedNodeConfiguration.isHidden) {
41+
assetNode.generatedNodeConfiguration!.isHidden) {
4242
return AssetId(
4343
_rootPackage,
4444
'$generatedOutputDirectory/${id.package}/${id.path}',

0 commit comments

Comments
 (0)