3
3
// for details. All rights reserved. Use of this source code is governed by a
4
4
// BSD-style license that can be found in the LICENSE file.
5
5
6
- import 'dart:async' ;
7
6
import 'dart:io' show exitCode;
8
7
9
- import 'package:build/build.dart' ;
10
8
import 'package:build_config/build_config.dart' ;
11
9
import 'package:build_runner/build_runner.dart' ;
10
+ import 'package:build_test/builder.dart' ;
12
11
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' ;
15
14
16
15
final List <BuilderApplication > builders = [
17
- applyToRoot (
18
- new LibraryBuilder (new _NonNullableGenerator (),
19
- generatedExtension: '.non_nullable.dart' , header: _copyrightHeader),
16
+ applyToRoot (nonNull (),
20
17
generateFor: const InputSet (include: const [
21
18
'test/test_files/kitchen_sink.dart' ,
22
19
'test/test_files/json_test_example.dart'
23
20
])),
24
- applyToRoot (
25
- new LibraryBuilder (new _WrappedGenerator (),
26
- generatedExtension: '.wrapped.dart' , header: _copyrightHeader),
21
+ applyToRoot (wrapped (),
27
22
generateFor: const InputSet (include: const [
28
23
'test/test_files/kitchen_sink.dart' ,
29
24
'test/test_files/kitchen_sink.non_nullable.dart' ,
30
25
'test/test_files/json_test_example.dart' ,
31
26
'test/test_files/json_test_example.non_nullable.dart' ,
32
27
])),
33
- applyToRoot (jsonPartBuilder (header: _copyrightHeader ),
28
+ applyToRoot (jsonPartBuilder (header: copyrightHeader ),
34
29
generateFor: const InputSet (
35
30
include: const [
36
31
'example/example.dart' ,
@@ -41,96 +36,18 @@ final List<BuilderApplication> builders = [
41
36
'test/test_files/kitchen_sink.non_nullable.dart'
42
37
],
43
38
)),
44
- applyToRoot (jsonPartBuilder (useWrappers: true , header: _copyrightHeader ),
39
+ applyToRoot (jsonPartBuilder (useWrappers: true , header: copyrightHeader ),
45
40
generateFor: const InputSet (
46
41
include: const [
47
42
'test/test_files/kitchen_sink*wrapped.dart' ,
48
43
'test/test_files/json_test_example*wrapped.dart' ,
49
44
],
50
- ))
45
+ )),
46
+ applyToRoot (testBootstrapBuilder (null ),
47
+ generateFor: const InputSet (include: const ['test/**' ]),
48
+ hideOutput: true ),
51
49
];
52
50
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
-
134
51
main (List <String > args) async {
135
52
exitCode = await run (args, builders);
136
53
}
0 commit comments