/
builder_definition.dart
228 lines (190 loc) · 7.26 KB
/
builder_definition.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2018, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:json_annotation/json_annotation.dart';
import 'build_target.dart';
import 'common.dart';
import 'expandos.dart';
import 'input_set.dart';
import 'key_normalization.dart';
part 'builder_definition.g.dart';
@JsonEnum(fieldRename: FieldRename.snake)
enum AutoApply { none, dependents, allPackages, rootPackage }
enum BuildTo {
/// Generated files are written to the source directory next to their primary
/// inputs.
source,
/// Generated files are written to the hidden 'generated' directory.
cache
}
/// Definition of a builder parsed from the `builders` section of `build.yaml`.
@JsonSerializable()
class BuilderDefinition {
/// The package which provides this Builder.
String get package => packageExpando[this]!;
/// A unique key for this Builder in `'$package:$builder'` format.
String get key => builderKeyExpando[this]!;
/// The names of the top-level methods in [import] from args -> Builder.
@JsonKey(required: true, disallowNullValue: true)
final List<String> builderFactories;
/// The import to be used to load `clazz`.
@JsonKey(required: true, disallowNullValue: true)
final String import;
/// A map from input extension to the output extensions created for matching
/// inputs.
@JsonKey(required: true, disallowNullValue: true)
final Map<String, List<String>> buildExtensions;
/// The name of the dart_library target that contains `import`.
///
/// May be null or unreliable and should not be used.
@Deprecated('Do not use')
final String? target;
/// Which packages should have this builder applied automatically.
final AutoApply autoApply;
/// A list of file extensions which are required to run this builder.
///
/// No builder which outputs any extension in this list is allowed to run
/// after this builder.
final List<String> requiredInputs;
/// Builder keys in `$package:$builder` format which should only be run after
/// this Builder.
final List<String> runsBefore;
/// Builder keys in `$package:$builder` format which should be run on any
/// target which also runs this Builder.
final List<String> appliesBuilders;
/// Whether this Builder should be deferred until it's output is requested.
///
/// Optional builders are lazy and will not run unless some later builder
/// requests one of it's possible outputs through either `readAs*` or
/// `canRead`.
final bool isOptional;
/// Where the outputs of this builder should be written.
final BuildTo buildTo;
final TargetBuilderConfigDefaults defaults;
BuilderDefinition({
required this.builderFactories,
required this.buildExtensions,
required this.import,
String? target,
AutoApply? autoApply,
Iterable<String>? requiredInputs,
Iterable<String>? runsBefore,
Iterable<String>? appliesBuilders,
bool? isOptional,
BuildTo? buildTo,
TargetBuilderConfigDefaults? defaults,
}) :
// ignore: deprecated_member_use
target = target != null
? normalizeTargetKeyUsage(target, currentPackage)
: null,
autoApply = autoApply ?? AutoApply.none,
requiredInputs = requiredInputs?.toList() ?? const [],
runsBefore = runsBefore
?.map((builder) =>
normalizeBuilderKeyUsage(builder, currentPackage))
.toList() ??
const [],
appliesBuilders = appliesBuilders
?.map((builder) =>
normalizeBuilderKeyUsage(builder, currentPackage))
.toList() ??
const [],
isOptional = isOptional ?? false,
buildTo = buildTo ?? BuildTo.cache,
defaults = defaults ?? const TargetBuilderConfigDefaults() {
if (builderFactories.isEmpty) {
throw ArgumentError.value(builderFactories, 'builderFactories',
'Must have at least one value.');
}
if (buildExtensions.entries.any((e) => e.value.contains(e.key))) {
throw ArgumentError.value(
buildExtensions,
'buildExtensions',
'May not overwrite an input, '
'the output extensions must not contain the input extension');
}
}
factory BuilderDefinition.fromJson(Map json) {
ArgumentError.checkNotNull(json);
return _$BuilderDefinitionFromJson(json);
}
@override
String toString() => {
'autoApply': autoApply,
'import': import,
'builderFactories': builderFactories,
'buildExtensions': buildExtensions,
'requiredInputs': requiredInputs,
'runsBefore': runsBefore,
'isOptional': isOptional,
'buildTo': buildTo,
'defaults': defaults,
}.toString();
}
/// The definition of a `PostProcessBuilder` in the `post_process_builders`
/// section of a `build.yaml`.
@JsonSerializable()
class PostProcessBuilderDefinition {
/// The package which provides this Builder.
String get package => packageExpando[this]!;
/// A unique key for this Builder in `'$package:$builder'` format.
String get key => builderKeyExpando[this]!;
/// The name of the top-level method in [import] from
/// Map<String, dynamic> -> Builder.
@JsonKey(required: true, disallowNullValue: true)
final String builderFactory;
/// The import to be used to load `clazz`.
@JsonKey(required: true, disallowNullValue: true)
final String import;
/// A list of input extensions for this builder.
///
/// May be null or unreliable and should not be used.
@Deprecated('do not use')
final Iterable<String>? inputExtensions;
/// The name of the dart_library target that contains `import`.
///
/// May be null or unreliable and should not be used.
@Deprecated('do not use')
final String? target;
final TargetBuilderConfigDefaults defaults;
PostProcessBuilderDefinition({
required this.builderFactory,
required this.import,
this.inputExtensions,
this.target,
TargetBuilderConfigDefaults? defaults,
}) : defaults = defaults ?? const TargetBuilderConfigDefaults();
factory PostProcessBuilderDefinition.fromJson(Map json) {
ArgumentError.checkNotNull(json);
return _$PostProcessBuilderDefinitionFromJson(json);
}
@override
String toString() => {
'import': import,
'builderFactory': builderFactory,
'defaults': defaults,
}.toString();
}
/// Default values that builder authors can specify when users don't fill in the
/// corresponding key for [TargetBuilderConfig].
@JsonSerializable()
class TargetBuilderConfigDefaults {
final InputSet generateFor;
final Map<String, dynamic> options;
final Map<String, dynamic> devOptions;
final Map<String, dynamic> releaseOptions;
const TargetBuilderConfigDefaults({
InputSet? generateFor,
Map<String, dynamic>? options,
Map<String, dynamic>? devOptions,
Map<String, dynamic>? releaseOptions,
}) : generateFor = generateFor ?? InputSet.anything,
options = options ?? const {},
devOptions = devOptions ?? const {},
releaseOptions = releaseOptions ?? const {};
factory TargetBuilderConfigDefaults.fromJson(Map json) {
ArgumentError.checkNotNull(json);
return _$TargetBuilderConfigDefaultsFromJson(json);
}
}