-
Notifications
You must be signed in to change notification settings - Fork 40
/
api_parser.dart
104 lines (81 loc) · 3.22 KB
/
api_parser.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
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:build/build.dart';
import 'package:recase/recase.dart';
import 'package:flutter_deriv_api/tools/schema_parser/json_schema_parser.dart';
import 'package:flutter_deriv_api/tools/schema_parser/schema_model.dart';
Builder apiParser(final BuilderOptions _) => APIParser();
/// APIParser Class which handles generating API response classes.
class APIParser extends Builder {
@override
FutureOr<void> build(BuildStep buildStep) {
try {
final String path = buildStep.inputId.path;
final String fileBaseName =
(path.split('/').last.split('_')..removeLast()).join('_');
final String className = '${ReCase(fileBaseName).pascalCase}Response';
final List<SchemaModel> rootChildren = JsonSchemaParser.getClassTypesFor(
JsonSchemaParser.preProcessModels(
json.decode(File(path).readAsStringSync()),
),
);
final String leftPartPath =
(path.split('.').first.split('/')..removeLast()).join('/');
final String rightPartPath = path.split('.').first.split('/').last;
final File methodsFile =
File('$leftPartPath/methods/${rightPartPath}_methods.json');
if (!methodsFile.existsSync()) {
methodsFile
..createSync()
..writeAsStringSync(
'{\n"methods": "",\n"imports": "import \'package:flutter_deriv_api/helpers/helpers.dart\';\\n"\n}');
}
final Map<String, dynamic> methodsJson =
json.decode(methodsFile.readAsStringSync());
final List<StringBuffer> source = JsonSchemaParser().getClasses(
SchemaModel.newModelWithChildren(
children: rootChildren,
className: className,
),
methodsString: methodsJson['methods'] ?? '',
isRoot: true,
);
List<StringBuffer> result =
_addImports(source: source, imports: methodsJson['imports']);
result = _addLinterSilencers(result);
final File output =
File('lib/api/response/${fileBaseName}_response_result.dart');
!output.existsSync()
? output.createSync(recursive: true)
: output.writeAsStringSync('');
for (final StringBuffer item in result) {
output.writeAsStringSync('$item', mode: FileMode.append);
}
JsonSchemaParser.classNamesArray.clear();
} on Exception catch (e, stack) {
log
..severe('Failed to process ${buildStep.inputId} - $e')
..severe('Stack trace $stack');
}
}
@override
Map<String, List<String>> get buildExtensions => const <String, List<String>>{
'.json': <String>['_result.dart']
};
}
List<StringBuffer> _addImports({
required List<StringBuffer> source,
required String imports,
}) {
final String extraImports =
source.isNotEmpty ? "import 'package:equatable/equatable.dart';\n\n" : '';
final StringBuffer baseImports = StringBuffer('$extraImports$imports\n');
return <StringBuffer>[baseImports, ...source];
}
List<StringBuffer> _addLinterSilencers(List<StringBuffer> source) {
final StringBuffer silencers = StringBuffer(
'// ignore_for_file: prefer_single_quotes, unnecessary_import, unused_import\n\n',
);
return <StringBuffer>[silencers, ...source];
}