Skip to content

Commit 19fe85b

Browse files
authored
Put custom builders in their own file, enable build_test (#154)
* tool: move custom builders into their own file Hopefully towards using build.yaml for this package * Doc comment nit in builder.dart * set things up for build_test
1 parent 9b05248 commit 19fe85b

File tree

4 files changed

+110
-96
lines changed

4 files changed

+110
-96
lines changed

json_serializable/lib/builder.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import 'package:build/build.dart';
1616

1717
import 'src/json_part_builder.dart';
1818

19-
/// Supports `package:build_runner` creation and configuration of `build_cli`.
19+
/// Supports `package:build_runner` creation and configuration of
20+
/// `json_serializable`.
2021
///
2122
/// Not meant to be invoked by hand-authored code.
2223
Builder jsonSerializable(BuilderOptions options) {

json_serializable/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: json_serializable
2-
version: 0.5.2
2+
version: 0.5.3-dev
33
author: Dart Team <misc@dartlang.org>
44
description: Generates utilities to aid in serializing to/from JSON.
55
homepage: https://github.com/dart-lang/json_serializable

json_serializable/tool/build.dart

Lines changed: 11 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,29 @@
33
// for details. All rights reserved. Use of this source code is governed by a
44
// BSD-style license that can be found in the LICENSE file.
55

6-
import 'dart:async';
76
import 'dart:io' show exitCode;
87

9-
import 'package:build/build.dart';
108
import 'package:build_config/build_config.dart';
119
import 'package:build_runner/build_runner.dart';
10+
import 'package:build_test/builder.dart';
1211
import 'package:json_serializable/src/json_part_builder.dart';
13-
import 'package:path/path.dart' as p;
14-
import 'package:source_gen/source_gen.dart';
12+
13+
import 'builder.dart';
1514

1615
final List<BuilderApplication> builders = [
17-
applyToRoot(
18-
new LibraryBuilder(new _NonNullableGenerator(),
19-
generatedExtension: '.non_nullable.dart', header: _copyrightHeader),
16+
applyToRoot(nonNull(),
2017
generateFor: const InputSet(include: const [
2118
'test/test_files/kitchen_sink.dart',
2219
'test/test_files/json_test_example.dart'
2320
])),
24-
applyToRoot(
25-
new LibraryBuilder(new _WrappedGenerator(),
26-
generatedExtension: '.wrapped.dart', header: _copyrightHeader),
21+
applyToRoot(wrapped(),
2722
generateFor: const InputSet(include: const [
2823
'test/test_files/kitchen_sink.dart',
2924
'test/test_files/kitchen_sink.non_nullable.dart',
3025
'test/test_files/json_test_example.dart',
3126
'test/test_files/json_test_example.non_nullable.dart',
3227
])),
33-
applyToRoot(jsonPartBuilder(header: _copyrightHeader),
28+
applyToRoot(jsonPartBuilder(header: copyrightHeader),
3429
generateFor: const InputSet(
3530
include: const [
3631
'example/example.dart',
@@ -41,96 +36,18 @@ final List<BuilderApplication> builders = [
4136
'test/test_files/kitchen_sink.non_nullable.dart'
4237
],
4338
)),
44-
applyToRoot(jsonPartBuilder(useWrappers: true, header: _copyrightHeader),
39+
applyToRoot(jsonPartBuilder(useWrappers: true, header: copyrightHeader),
4540
generateFor: const InputSet(
4641
include: const [
4742
'test/test_files/kitchen_sink*wrapped.dart',
4843
'test/test_files/json_test_example*wrapped.dart',
4944
],
50-
))
45+
)),
46+
applyToRoot(testBootstrapBuilder(null),
47+
generateFor: const InputSet(include: const ['test/**']),
48+
hideOutput: true),
5149
];
5250

53-
final _copyrightContent =
54-
'''// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
55-
// for details. All rights reserved. Use of this source code is governed by a
56-
// BSD-style license that can be found in the LICENSE file.
57-
''';
58-
59-
final _copyrightHeader = '$_copyrightContent\n$defaultFileHeader';
60-
61-
class _NonNullableGenerator extends Generator {
62-
@override
63-
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
64-
final path = buildStep.inputId.path;
65-
final baseName = p.basenameWithoutExtension(path);
66-
67-
final content = await buildStep.readAsString(buildStep.inputId);
68-
var replacements = <_Replacement>[
69-
new _Replacement(_copyrightContent, ''),
70-
new _Replacement(
71-
"part '$baseName.g.dart",
72-
"part '$baseName.non_nullable.g.dart",
73-
),
74-
new _Replacement(
75-
'@JsonSerializable()', '@JsonSerializable(nullable: false)'),
76-
];
77-
78-
if (baseName == 'kitchen_sink') {
79-
replacements.addAll([
80-
new _Replacement('List<T> _defaultList<T>() => null;',
81-
'List<T> _defaultList<T>() => <T>[];'),
82-
new _Replacement(
83-
'Map _defaultMap() => null;', 'Map _defaultMap() => {};'),
84-
new _Replacement('DateTime dateTime;',
85-
'DateTime dateTime = new DateTime(1981, 6, 5);')
86-
]);
87-
}
88-
89-
return _Replacement.generate(content, replacements);
90-
}
91-
}
92-
93-
class _WrappedGenerator extends Generator {
94-
@override
95-
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
96-
final path = buildStep.inputId.path;
97-
final baseName = p.basenameWithoutExtension(path);
98-
99-
final content = await buildStep.readAsString(buildStep.inputId);
100-
var replacements = <_Replacement>[
101-
new _Replacement(_copyrightContent, ''),
102-
new _Replacement(
103-
"part '$baseName.g.dart",
104-
"part '$baseName.wrapped.g.dart",
105-
),
106-
];
107-
108-
return _Replacement.generate(content, replacements);
109-
}
110-
}
111-
112-
class _Replacement {
113-
final Pattern existing;
114-
final String replacement;
115-
116-
_Replacement(this.existing, this.replacement);
117-
118-
static String generate(
119-
String inputContent, Iterable<_Replacement> replacements) {
120-
var outputContent = inputContent;
121-
122-
for (var r in replacements) {
123-
if (!outputContent.contains(r.existing)) {
124-
throw new StateError(
125-
'Input string did not contain `${r.existing}` as expected.');
126-
}
127-
outputContent = outputContent.replaceAll(r.existing, r.replacement);
128-
}
129-
130-
return outputContent;
131-
}
132-
}
133-
13451
main(List<String> args) async {
13552
exitCode = await run(args, builders);
13653
}

json_serializable/tool/builder.dart

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
// Copyright (c) 2018, 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+
import 'dart:async';
6+
import 'package:build/build.dart';
7+
8+
import 'package:path/path.dart' as p;
9+
import 'package:source_gen/source_gen.dart';
10+
11+
final _copyrightContent =
12+
'''// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
13+
// for details. All rights reserved. Use of this source code is governed by a
14+
// BSD-style license that can be found in the LICENSE file.
15+
''';
16+
17+
final copyrightHeader = '$_copyrightContent\n$defaultFileHeader';
18+
19+
Builder nonNull([_]) => new LibraryBuilder(new _NonNullableGenerator(),
20+
generatedExtension: '.non_nullable.dart', header: copyrightHeader);
21+
22+
Builder wrapped([_]) => new LibraryBuilder(new _WrappedGenerator(),
23+
generatedExtension: '.wrapped.dart', header: copyrightHeader);
24+
25+
class _NonNullableGenerator extends Generator {
26+
@override
27+
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
28+
final path = buildStep.inputId.path;
29+
final baseName = p.basenameWithoutExtension(path);
30+
31+
final content = await buildStep.readAsString(buildStep.inputId);
32+
var replacements = [
33+
new _Replacement(_copyrightContent, ''),
34+
new _Replacement(
35+
"part '$baseName.g.dart",
36+
"part '$baseName.non_nullable.g.dart",
37+
),
38+
new _Replacement(
39+
'@JsonSerializable()', '@JsonSerializable(nullable: false)'),
40+
];
41+
42+
if (baseName == 'kitchen_sink') {
43+
replacements.addAll([
44+
new _Replacement('List<T> _defaultList<T>() => null;',
45+
'List<T> _defaultList<T>() => <T>[];'),
46+
new _Replacement(
47+
'Map _defaultMap() => null;', 'Map _defaultMap() => {};'),
48+
new _Replacement('DateTime dateTime;',
49+
'DateTime dateTime = new DateTime(1981, 6, 5);')
50+
]);
51+
}
52+
53+
return _Replacement.generate(content, replacements);
54+
}
55+
}
56+
57+
class _WrappedGenerator extends Generator {
58+
@override
59+
FutureOr<String> generate(LibraryReader library, BuildStep buildStep) async {
60+
final path = buildStep.inputId.path;
61+
final baseName = p.basenameWithoutExtension(path);
62+
63+
final content = await buildStep.readAsString(buildStep.inputId);
64+
var replacements = [
65+
new _Replacement(_copyrightContent, ''),
66+
new _Replacement(
67+
"part '$baseName.g.dart",
68+
"part '$baseName.wrapped.g.dart",
69+
),
70+
];
71+
72+
return _Replacement.generate(content, replacements);
73+
}
74+
}
75+
76+
class _Replacement {
77+
final Pattern existing;
78+
final String replacement;
79+
80+
_Replacement(this.existing, this.replacement);
81+
82+
static String generate(
83+
String inputContent, Iterable<_Replacement> replacements) {
84+
var outputContent = inputContent;
85+
86+
for (var r in replacements) {
87+
if (!outputContent.contains(r.existing)) {
88+
throw new StateError(
89+
'Input string did not contain `${r.existing}` as expected.');
90+
}
91+
outputContent = outputContent.replaceAll(r.existing, r.replacement);
92+
}
93+
94+
return outputContent;
95+
}
96+
}

0 commit comments

Comments
 (0)