Skip to content

Commit 501bfa8

Browse files
authored
Use built_value for serialization. (#3915)
1 parent 3f16290 commit 501bfa8

File tree

10 files changed

+989
-474
lines changed

10 files changed

+989
-474
lines changed

_test_common/lib/matchers.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,10 @@ class _AssetGraphMatcher extends Matcher {
178178

179179
final configuration = node.globNodeConfiguration!;
180180
final expectedConfiguration = expectedNode.globNodeConfiguration!;
181-
if (configuration.glob.pattern !=
182-
expectedConfiguration.glob.pattern) {
181+
if (configuration.glob != expectedConfiguration.glob) {
183182
matchState['glob of ${node.id}'] = [
184-
configuration.glob.pattern,
185-
expectedConfiguration.glob.pattern,
183+
configuration.glob,
184+
expectedConfiguration.glob,
186185
];
187186
matches = false;
188187
}

build_runner/lib/src/server/asset_graph_handler.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ class AssetGraphHandler {
173173
'type': node.runtimeType.toString(),
174174
'glob':
175175
node.type == NodeType.glob
176-
? node.globNodeConfiguration!.glob.pattern
176+
? node.globNodeConfiguration!.glob
177177
: null,
178178
'lastKnownDigest': node.lastKnownDigest.toString(),
179179
},

build_runner_core/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +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.
20+
- Use `built_value` for `AssetNode` and related types, and for serialization.
2121

2222
## 8.0.0
2323

build_runner_core/lib/src/asset_graph/graph.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,8 @@ class AssetGraph {
469469
);
470470
for (final node in samePackageGlobNodes) {
471471
final nodeConfiguration = node.globNodeConfiguration!;
472-
if (nodeConfiguration.glob.matches(id.path)) {
472+
final glob = Glob(nodeConfiguration.glob);
473+
if (glob.matches(id.path)) {
473474
invalidateNodeAndDeps(node.id);
474475
updateNode(node.id, (nodeBuilder) {
475476
nodeBuilder.globNodeState.pendingBuildAction =

build_runner_core/lib/src/asset_graph/node.dart

Lines changed: 87 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import 'dart:convert';
77
import 'package:build/build.dart' hide Builder;
88
import 'package:built_collection/built_collection.dart';
99
import 'package:built_value/built_value.dart';
10+
import 'package:built_value/serializer.dart';
1011
import 'package:crypto/crypto.dart';
11-
import 'package:glob/glob.dart';
1212

1313
import '../generate/phase.dart';
1414

1515
part 'node.g.dart';
1616

1717
/// Types of [AssetNode].
1818
class NodeType extends EnumClass {
19+
static Serializer<NodeType> get serializer => _$nodeTypeSerializer;
20+
1921
static const NodeType builderOptions = _$builderOptions;
2022
static const NodeType generated = _$generated;
2123
static const NodeType glob = _$glob;
@@ -33,6 +35,8 @@ class NodeType extends EnumClass {
3335

3436
/// A node in the asset graph which may be an input to other assets.
3537
abstract class AssetNode implements Built<AssetNode, AssetNodeBuilder> {
38+
static Serializer<AssetNode> get serializer => _$assetNodeSerializer;
39+
3640
AssetId get id;
3741
NodeType get type;
3842

@@ -219,7 +223,7 @@ abstract class AssetNode implements Built<AssetNode, AssetNodeBuilder> {
219223
factory AssetNode.glob(
220224
AssetId id, {
221225
Digest? lastKnownDigest,
222-
required Glob glob,
226+
required String glob,
223227
required int phaseNumber,
224228
Iterable<AssetId>? inputs,
225229
required PendingBuildAction pendingBuildAction,
@@ -236,11 +240,8 @@ abstract class AssetNode implements Built<AssetNode, AssetNodeBuilder> {
236240
..lastKnownDigest = lastKnownDigest,
237241
);
238242

239-
static AssetId createGlobNodeId(String package, Glob glob, int phaseNum) =>
240-
AssetId(
241-
package,
242-
'glob.$phaseNum.${base64.encode(utf8.encode(glob.pattern))}',
243-
);
243+
static AssetId createGlobNodeId(String package, String glob, int phaseNum) =>
244+
AssetId(package, 'glob.$phaseNum.${base64.encode(utf8.encode(glob))}');
244245

245246
/// A [primaryInput] to a [PostBuildAction].
246247
///
@@ -313,6 +314,9 @@ abstract class AssetNode implements Built<AssetNode, AssetNodeBuilder> {
313314
abstract class GeneratedNodeConfiguration
314315
implements
315316
Built<GeneratedNodeConfiguration, GeneratedNodeConfigurationBuilder> {
317+
static Serializer<GeneratedNodeConfiguration> get serializer =>
318+
_$generatedNodeConfigurationSerializer;
319+
316320
/// The primary input which generated this node.
317321
AssetId get primaryInput;
318322

@@ -342,6 +346,9 @@ abstract class GeneratedNodeConfiguration
342346
/// State for an [AssetNode.generated] that changes during the build.
343347
abstract class GeneratedNodeState
344348
implements Built<GeneratedNodeState, GeneratedNodeStateBuilder> {
349+
static Serializer<GeneratedNodeState> get serializer =>
350+
_$generatedNodeStateSerializer;
351+
345352
/// All the inputs that were read when generating this asset, or deciding not
346353
/// to generate it.
347354
BuiltSet<AssetId> get inputs;
@@ -373,7 +380,10 @@ abstract class GeneratedNodeState
373380
/// Additional configuration for an [AssetNode.glob].
374381
abstract class GlobNodeConfiguration
375382
implements Built<GlobNodeConfiguration, GlobNodeConfigurationBuilder> {
376-
Glob get glob;
383+
static Serializer<GlobNodeConfiguration> get serializer =>
384+
_$globNodeConfigurationSerializer;
385+
386+
String get glob;
377387
int get phaseNumber;
378388

379389
factory GlobNodeConfiguration(
@@ -386,6 +396,10 @@ abstract class GlobNodeConfiguration
386396
/// State for an [AssetNode.glob] that changes during the build.
387397
abstract class GlobNodeState
388398
implements Built<GlobNodeState, GlobNodeStateBuilder> {
399+
static Serializer<GlobNodeState> get serializer => _$globNodeStateSerializer;
400+
401+
/// The next work that needs doing on this node.
402+
389403
/// All the potential inputs matching this glob.
390404
///
391405
/// This field differs from [results] in that [AssetNode.generated] which may
@@ -412,6 +426,9 @@ abstract class PostProcessAnchorNodeConfiguration
412426
PostProcessAnchorNodeConfiguration,
413427
PostProcessAnchorNodeConfigurationBuilder
414428
> {
429+
static Serializer<PostProcessAnchorNodeConfiguration> get serializer =>
430+
_$postProcessAnchorNodeConfigurationSerializer;
431+
415432
int get actionNumber;
416433
AssetId get builderOptionsId;
417434
AssetId get primaryInput;
@@ -427,6 +444,9 @@ abstract class PostProcessAnchorNodeConfiguration
427444
abstract class PostProcessAnchorNodeState
428445
implements
429446
Built<PostProcessAnchorNodeState, PostProcessAnchorNodeStateBuilder> {
447+
static Serializer<PostProcessAnchorNodeState> get serializer =>
448+
_$postProcessAnchorNodeStateSerializer;
449+
430450
Digest? get previousInputsDigest;
431451

432452
factory PostProcessAnchorNodeState(
@@ -438,6 +458,9 @@ abstract class PostProcessAnchorNodeState
438458

439459
/// Work that needs doing for a node that tracks its inputs.
440460
class PendingBuildAction extends EnumClass {
461+
static Serializer<PendingBuildAction> get serializer =>
462+
_$pendingBuildActionSerializer;
463+
441464
static const PendingBuildAction none = _$none;
442465
static const PendingBuildAction buildIfInputsChanged = _$buildIfInputsChanged;
443466
static const PendingBuildAction build = _$build;
@@ -448,3 +471,59 @@ class PendingBuildAction extends EnumClass {
448471
static PendingBuildAction valueOf(String name) =>
449472
_$pendingBuildActionValueOf(name);
450473
}
474+
475+
@SerializersFor([AssetNode])
476+
final Serializers serializers =
477+
(_$serializers.toBuilder()
478+
..add(AssetIdSerializer())
479+
..add(DigestSerializer()))
480+
.build();
481+
482+
/// Serializer for [AssetId].
483+
///
484+
/// It would also work to make `AssetId` a `built_value` class, but there's
485+
/// little benefit and it's nicer to keep codegen local to this package.
486+
class AssetIdSerializer implements PrimitiveSerializer<AssetId> {
487+
@override
488+
Iterable<Type> get types => [AssetId];
489+
490+
@override
491+
String get wireName => 'AssetId';
492+
493+
@override
494+
AssetId deserialize(
495+
Serializers serializers,
496+
Object serialized, {
497+
FullType specifiedType = FullType.unspecified,
498+
}) => AssetId.parse(serialized as String);
499+
500+
@override
501+
Object serialize(
502+
Serializers serializers,
503+
AssetId object, {
504+
FullType specifiedType = FullType.unspecified,
505+
}) => object.toString();
506+
}
507+
508+
/// Serializer for [Digest].
509+
class DigestSerializer implements PrimitiveSerializer<Digest> {
510+
@override
511+
Iterable<Type> get types => [Digest];
512+
513+
@override
514+
String get wireName => 'Digest';
515+
516+
@override
517+
Digest deserialize(
518+
Serializers serializers,
519+
Object serialized, {
520+
FullType specifiedType = FullType.unspecified,
521+
}) => Digest(base64.decode(serialized as String));
522+
523+
@override
524+
Object serialize(
525+
Serializers serializers,
526+
Digest object, {
527+
FullType specifiedType = FullType.unspecified,
528+
}) => base64.encode(object.bytes);
529+
}

0 commit comments

Comments
 (0)