This repository was archived by the owner on Oct 2, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 130
Expand file tree
/
Copy pathbuilder.dart
More file actions
221 lines (179 loc) · 6.46 KB
/
Copy pathbuilder.dart
File metadata and controls
221 lines (179 loc) · 6.46 KB
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
import 'dart:async';
import 'package:artemis/generator/data/data.dart';
import 'package:artemis/transformer/add_typename_transformer.dart';
import 'package:build/build.dart';
import 'package:glob/glob.dart';
import 'package:gql/ast.dart';
import 'package:gql/language.dart';
import './generator.dart';
import './generator/print_helpers.dart';
import './schema/options.dart';
import 'generator/errors.dart';
/// [GraphQLQueryBuilder] instance, to be used by `build_runner`.
GraphQLQueryBuilder graphQLQueryBuilder(BuilderOptions options) =>
GraphQLQueryBuilder(options);
String _addGraphQLExtensionToPathIfNeeded(String path) {
if (!path.endsWith('.graphql.dart')) {
return path.replaceAll(RegExp(r'\.dart$'), '.graphql.dart');
}
return path;
}
List<String> _builderOptionsToExpectedOutputs(BuilderOptions builderOptions) {
final schemaMapping =
GeneratorOptions.fromJson(builderOptions.config).schemaMapping;
if (schemaMapping.isEmpty) {
throw MissingBuildConfigurationException('schema_mapping');
}
if (schemaMapping.any((s) => s.output == null)) {
throw MissingBuildConfigurationException('schema_mapping => output');
}
return schemaMapping
.map((s) {
final outputWithoutLib = s.output!.replaceAll(RegExp(r'^lib/'), '');
return {
outputWithoutLib,
_addGraphQLExtensionToPathIfNeeded(outputWithoutLib),
}.toList();
})
.expand((e) => e)
.toList();
}
/// Main Artemis builder.
class GraphQLQueryBuilder implements Builder {
/// Creates a builder from [BuilderOptions].
GraphQLQueryBuilder(BuilderOptions builderOptions)
: options = GeneratorOptions.fromJson(builderOptions.config),
expectedOutputs = _builderOptionsToExpectedOutputs(builderOptions);
/// This generator options, gathered from `build.yaml` file.
final GeneratorOptions options;
/// List FragmentDefinitionNode in fragments_glob.
// List<FragmentDefinitionNode> fragmentsCommon = [];
/// The generated output file.
final List<String> expectedOutputs;
/// Callback fired when the generator processes a [QueryDefinition].
OnBuildQuery? onBuild;
@override
Map<String, List<String>> get buildExtensions => {
r'$lib$': expectedOutputs,
};
/// read asset files
Future<List<DocumentNode>> readGraphQlFiles(
BuildStep buildStep,
String schema,
) async {
final schemaAssetStream = buildStep.findAssets(Glob(schema));
return await schemaAssetStream
.asyncMap(
(asset) async => parseString(
await buildStep.readAsString(asset),
url: asset.path,
),
)
.toList();
}
@override
Future<void> build(BuildStep buildStep) async {
List<FragmentDefinitionNode> fragmentsCommon = [];
final fragmentsGlob = options.fragmentsGlob;
if (fragmentsGlob != null) {
final commonFragments = (await readGraphQlFiles(buildStep, fragmentsGlob))
.map((e) => e.definitions.whereType<FragmentDefinitionNode>())
.expand((e) => e)
.toList();
if (commonFragments.isEmpty) {
throw MissingFilesException(fragmentsGlob);
}
fragmentsCommon.addAll(commonFragments);
}
for (final schemaMap in options.schemaMapping) {
List<FragmentDefinitionNode> schemaCommonFragments = [
...fragmentsCommon,
];
final schemaFragmentsGlob = schemaMap.fragmentsGlob;
if (schemaFragmentsGlob != null) {
final schemaFragments =
(await readGraphQlFiles(buildStep, schemaFragmentsGlob))
.map((e) => e.definitions.whereType<FragmentDefinitionNode>())
.expand((e) => e)
.toList();
if (schemaFragments.isEmpty) {
throw MissingFilesException(schemaFragmentsGlob);
}
schemaCommonFragments.addAll(schemaFragments);
}
final queriesGlob = schemaMap.queriesGlob;
final schema = schemaMap.schema;
final output = schemaMap.output;
if (schema == null) {
throw MissingBuildConfigurationException('schema_map => schema');
}
if (output == null) {
throw MissingBuildConfigurationException('schema_map => output');
}
// Loop through all files in glob
if (queriesGlob == null) {
throw MissingBuildConfigurationException('schema_map => queries_glob');
} else if (Glob(queriesGlob).matches(schema)) {
throw QueryGlobsSchemaException();
} else if (Glob(queriesGlob).matches(output)) {
throw QueryGlobsOutputException();
}
final gqlSchema = await readGraphQlFiles(buildStep, schema);
if (gqlSchema.isEmpty) {
throw MissingFilesException(schema);
}
var gqlDocs = await readGraphQlFiles(buildStep, queriesGlob);
if (gqlDocs.isEmpty) {
throw MissingFilesException(queriesGlob);
}
if (schemaMap.appendTypeName) {
gqlDocs = gqlDocs.map(
(doc) {
final transformed =
transform(doc, [AppendTypename(schemaMap.typeNameField)]);
// transform makes definitions growable: false so just recreate it again
// as far as we need to add some elements there lately
return DocumentNode(
definitions: List.from(transformed.definitions),
span: transformed.span,
);
},
).toList();
schemaCommonFragments = schemaCommonFragments
.map((fragments) => transform(
fragments,
[AppendTypename(schemaMap.typeNameField)],
))
.toList();
}
final libDefinition = generateLibrary(
_addGraphQLExtensionToPathIfNeeded(output),
gqlDocs,
options,
schemaMap,
schemaCommonFragments,
gqlSchema.first,
);
if (onBuild != null) {
onBuild!(libDefinition);
}
final buffer = StringBuffer();
final outputFileId = AssetId(
buildStep.inputId.package,
_addGraphQLExtensionToPathIfNeeded(output),
);
writeLibraryDefinitionToBuffer(
buffer,
options.ignoreForFile,
libDefinition,
);
await buildStep.writeAsString(outputFileId, buffer.toString());
if (!output.endsWith('.graphql.dart')) {
final forwarderOutputFileId =
AssetId(buildStep.inputId.package, output);
await buildStep.writeAsString(
forwarderOutputFileId, writeLibraryForwarder(libDefinition));
}
}
}
}