-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubsonic_api_generator.dart
460 lines (388 loc) · 14.5 KB
/
subsonic_api_generator.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
// ignore_for_file: avoid_print
import 'dart:io';
import 'package:xml/xml.dart';
import 'dart:convert';
import 'package:html/parser.dart' as parser;
void main() async {
final inputFile = File('subsonic-rest-api-1.16.1.xsd');
final outputFile = File('lib/models/subsonic.dart');
final xmlContent = await inputFile.readAsString();
final document = XmlDocument.parse(xmlContent);
final schema = document.findAllElements('xs:schema').first;
final dartCode = generateDartCode(schema);
await outputFile.writeAsString(dartCode);
print('Generated Dart file at ${outputFile.path}');
await generateApiDocsJson(File('Subsonic.html'), File('subsonic_api.json'));
print(
'API documentation has been successfully parsed and saved to subsonic_api.json');
await generateProviders(
File('subsonic_api.json'),
File('lib/providers/subsonic_apis.dart'),
);
print(
'Providers have been generated succesfully and saved to lib/providers/subsonic_apis.dart');
}
String generateDartCode(XmlElement schema) {
final buffer = StringBuffer();
// Write file header
buffer.writeln('''
import 'package:freezed_annotation/freezed_annotation.dart';
part 'subsonic.g.dart';
part 'subsonic.freezed.dart';
''');
// Process all simple types (enums) first
for (final simpleType in schema.findAllElements('xs:simpleType')) {
final typeName = simpleType.getAttribute('name');
if (typeName != null) {
buffer.writeln(processSimpleType(simpleType, typeName));
}
}
// Process all complex types
for (final complexType in schema.findAllElements('xs:complexType')) {
final typeName = complexType.getAttribute('name');
if (typeName != null) {
buffer.writeln(processComplexType(schema, complexType, typeName));
}
}
return buffer.toString();
}
String processSimpleType(XmlElement type, String typeName) {
final buffer = StringBuffer();
final enumName = toPascalCase(typeName);
final restriction = type.findAllElements('xs:restriction').firstOrNull;
if (restriction != null) {
final baseType = restriction.getAttribute('base');
final enumerations = restriction.findAllElements('xs:enumeration').toList();
if (enumerations.isNotEmpty) {
// Generate enum
buffer.writeln('@JsonEnum()');
buffer.writeln('enum $enumName {');
// Add enum values
for (final enumValue in enumerations) {
final value = enumValue.getAttribute('value');
if (value != null) {
final enumConstant = toEnumConstant(value);
buffer.writeln(' @JsonValue(\'$value\')');
buffer.writeln(
' ${toCamelCase(enumName + toPascalCase(enumConstant))},');
}
}
// Close enum and add fromJson/toJson extension
buffer.writeln('}');
buffer.writeln();
} else if (baseType == 'xs:string' &&
restriction.findAllElements('xs:pattern').firstOrNull != null) {
// Handle pattern-based string types (like Version)
buffer.writeln('typedef $enumName = String;');
buffer.writeln();
} else if (baseType == 'xs:int' || baseType == 'xs:double') {
// Handle numeric restrictions
final min = restriction
.findAllElements('xs:minInclusive')
.firstOrNull
?.getAttribute('value');
final max = restriction
.findAllElements('xs:maxInclusive')
.firstOrNull
?.getAttribute('value');
buffer.writeln('/// Numeric type with constraints:');
if (min != null) buffer.writeln('/// - Minimum value: $min');
if (max != null) buffer.writeln('/// - Maximum value: $max');
buffer.writeln('typedef $enumName = ${getDartType(baseType!)};');
buffer.writeln();
}
}
return buffer.toString();
}
String toEnumConstant(String value) {
// Handle special cases and convert to valid Dart enum constant
final sanitized = value
.replaceAll(RegExp(r'[^a-zA-Z0-9_]'), '_')
.replaceAll(RegExp(r'_+'), '_')
.toLowerCase();
// Ensure it doesn't start with a number
if (RegExp(r'^[0-9]').hasMatch(sanitized)) {
return 'value_$sanitized';
}
return sanitized;
}
String processComplexType(XmlElement doc, XmlElement type, String typeName) {
final buffer = StringBuffer();
final className = toPascalCase(typeName);
// Check for inheritance
final baseType = type
.getElement('xs:complexContent')
?.getElement('xs:extension')
?.getAttribute('base')
?.replaceAll('sub:', '');
buffer.writeln('@freezed');
buffer.writeln('class $className with _\$$className {');
buffer.writeln(' const factory $className({');
final baseTypeElementIndex =
doc.children.indexWhere((test) => test.getAttribute('name') == baseType);
final baseTypeElement =
baseTypeElementIndex > 0 ? doc.children[baseTypeElementIndex] : null;
final elements = [
...type.findAllElements('xs:attribute'),
if (baseTypeElement != null)
...baseTypeElement.findAllElements('xs:attribute'),
];
// Process attributes
for (final attribute in elements) {
final attrName = attribute.getAttribute('name')!;
final attrType = attribute.getAttribute('type')!;
final required = attribute.getAttribute('use') == 'required';
final fieldName = toCamelCase(attrName);
final dartType = getDartType(attrType);
buffer.writeln(' @JsonKey(name: \'$attrName\')');
buffer.writeln(
' ${required ? 'required ' : ''}$dartType${required ? '' : '?'} $fieldName,');
}
for (final choice in type.findAllElements('xs:choice')) {
for (final attribute in choice.findAllElements('xs:element')) {
final attrName = attribute.getAttribute('name')!;
final attrType = attribute.getAttribute('type')!;
final fieldName = toCamelCase(attrName);
final dartType = getDartType(attrType);
buffer.writeln(' @JsonKey(name: \'$attrName\')');
buffer.writeln(' $dartType? $fieldName,');
}
}
// Process elements
final sequence = type.findAllElements('xs:sequence').firstOrNull;
final baseTypeSequence =
baseTypeElement?.findAllElements('xs:sequence').firstOrNull;
final sequences = <XmlElement>[];
if (sequence != null) sequences.add(sequence);
if (baseTypeSequence != null) sequences.add(baseTypeSequence);
for (final sequence in sequences) {
for (final element in sequence.findAllElements('xs:element')) {
final elemName = element.getAttribute('name')!;
final elemType = element.getAttribute('type')!.replaceFirst('sub:', '');
final minOccurs = element.getAttribute('minOccurs');
final maxOccurs = element.getAttribute('maxOccurs');
final fieldName = toCamelCase(elemName);
final dartType = getDartType(elemType);
final required = minOccurs == '1';
final isList = maxOccurs == 'unbounded';
buffer.writeln(' @JsonKey(name: \'$elemName\')');
if (isList) {
buffer.writeln(
' ${required ? 'required ' : ''}List<$dartType>${required ? '' : '?'} $fieldName,');
} else {
buffer.writeln(
' ${required ? 'required ' : ''}$dartType${required ? '' : '?'} $fieldName,');
}
}
}
// Close class definition
buffer.writeln(' }) = _$className;');
buffer.writeln();
buffer.writeln(' factory $className.fromJson(Map<String, dynamic> json) =>');
buffer.writeln(' _\$${className}FromJson(json);');
buffer.writeln('}');
buffer.writeln();
return buffer.toString();
}
String getDartType(String xmlType) {
switch (xmlType.trim()) {
case 'xs:string':
return 'String';
case 'xs:int':
case 'xs:long':
return 'int';
case 'xs:boolean':
return 'bool';
case 'xs:dateTime':
return 'DateTime';
case 'xs:float':
case 'xs:double':
return 'double';
default:
return toPascalCase(xmlType.replaceFirst('sub:', ''));
}
}
String toPascalCase(String input) {
final words = input.split(RegExp(r'[-_]|(?<=[a-z])(?=[A-Z])'));
final pascalCase = words
.map((word) => word.isNotEmpty
? word[0].toUpperCase() + word.substring(1).toLowerCase()
: '')
.join();
return pascalCase;
}
String toCamelCase(String input) {
final pascal = toPascalCase(input);
return pascal[0].toLowerCase() + pascal.substring(1);
}
String toSnakeCase(String input) {
// Insert underscores before uppercase letters and convert to lowercase
final snakeCase = input
.replaceAllMapped(RegExp(r'(?<=[a-z0-9])(?=[A-Z])'), (match) => '_')
.toLowerCase();
return snakeCase;
}
Future<void> generateApiDocsJson(File file, File output) async {
final content = await file.readAsString();
final document = parser.parse(content);
// Find all section elements with class 'box'
final sections = document.querySelectorAll('section.box');
final List<Map<String, dynamic>> methods = [];
for (var section in sections) {
// Get the method name from h3
final methodNameElement = section.querySelector('h3');
if (methodNameElement == null) continue;
final methodName = methodNameElement.text.trim();
final comment = section
.querySelectorAll('p')
.map((e) => e.text
.trim()
.replaceAll('\n', ' ')
.replaceAll('\t', ' ')
.replaceAll(RegExp(r'\s+'), ' '))
.join("\n");
// Initialize method object
final Map<String, dynamic> methodData = {
'method': methodName,
'comment': comment,
'args': <String, Map<String, dynamic>>{},
'response':
['getCoverArt', 'stream'].contains(methodName) ? 'string' : 'json',
};
// Find parameter table
final table = section.querySelector('table');
if (table != null) {
// Process table rows
final rows = table.querySelectorAll('tr');
// Skip header row
for (var i = 1; i < rows.length; i++) {
final cells = rows[i].querySelectorAll('td');
if (cells.length >= 4) {
// Extract parameter information
final paramName =
cells[0].text.replaceAll(RegExp(r'[^\w]'), '').trim();
final required = cells[1].text.trim().toLowerCase() == 'yes';
final comment = cells[3]
.text
.trim()
.replaceAll('\n', ' ')
.replaceAll('\t', ' ')
.replaceAll(RegExp(r'\s+'), ' ');
final rawDefaultValue = cells[2].text.trim();
final defaultValue = bool.tryParse(rawDefaultValue) ??
num.tryParse(rawDefaultValue) ??
(rawDefaultValue.isEmpty ? null : rawDefaultValue);
methodData['args'][paramName] = {
'required': required,
'default': defaultValue,
};
methodData['comment'] =
"${methodData['comment']}\n@param $paramName ${defaultValue == null ? '' : 'Default($defaultValue) '}$comment";
}
}
}
methods.add(methodData);
}
// Convert to JSON and save to file
final jsonOutput = const JsonEncoder.withIndent(' ').convert(methods);
await output.writeAsString(jsonOutput);
}
Future<void> generateProviders(File file, File output) async {
// Read the JSON file
final jsonString = await file.readAsString();
final List<dynamic> apis = json.decode(jsonString);
// Generate the Dart code
final StringBuffer buffer = StringBuffer();
// Write imports
buffer.writeln('''
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:dururu/providers/auth.dart';
import 'package:dururu/providers/dio.dart';
import 'package:dururu/models/subsonic.dart';
part 'subsonic_apis.g.dart';
part 'subsonic_apis.freezed.dart';
''');
// Process each API endpoint
for (final api in apis) {
final method = api['method'] as String;
final comment = (api['comment'] as String).replaceAll('\n', '\n/// ');
final args = api['args'] as Map<String, dynamic>;
final response = api['response'];
// Convert method name to camelCase
final methodName = _toCamelCase(method);
// Generate function parameters
final queryParameters = _generateQueryParameters(args);
final requestClassName = toPascalCase("${methodName}_Request");
final requestClass = _generateRequestClass(args, requestClassName);
final responseClassName =
response == 'json' ? 'Future<Response>' : 'String';
// Generate the API function
buffer.writeln('''
$requestClass
/// $comment
@riverpod
$responseClassName $methodName(Ref ref${args.isNotEmpty ? ', $requestClassName request' : ''}) ${response.toString().startsWith('Future<') ? 'async ' : ''}{
${response == 'json' ? '''return ref.read(dioProvider).get(
'\${ref.read(authProvider).value!.serverUrl}/rest/$method',
queryParameters: {
...ref.read(authProvider.notifier).getAuthorization(),
$queryParameters
},
).then(
(value) => Response.fromJson(value.data['subsonic-response'] as Map<String, dynamic>),
);''' : '''final params = {
...ref.read(authProvider.notifier).getAuthorization(),
$queryParameters
};
final url =
'\${ref.read(authProvider).value!.serverUrl}/rest/$method?\${params.entries.map((entry) => '\${entry.key}=\${entry.value}').join('&')}';
return Uri.encodeFull(url);
'''}
}
''');
}
// Write the generated code to a file
await output.writeAsString(buffer.toString());
}
String _toCamelCase(String input) {
return input[0].toLowerCase() + input.substring(1);
}
String _generateRequestClass(Map<String, dynamic> args, requestClassName) {
final params = <String>[];
args.forEach((key, value) {
if (value is Map<String, dynamic>) {
const type = 'dynamic';
final required = value['required'] as bool?;
final defaultValue = value['default'] is String
? '"${value['default']}"'
: value['default'];
if (defaultValue != null) params.add('@Default($defaultValue)');
params.add(' ${required ?? false ? ' required ' : ' '}$type $key,');
}
});
if (params.isEmpty) return '';
return '''
@freezed
class $requestClassName with _\$$requestClassName {
const factory $requestClassName({
${params.join('\n')}
}) = _$requestClassName;
}
''';
}
String _generateQueryParameters(Map<String, dynamic> args) {
final params = <String>[];
args.forEach((key, value) {
if (value is Map<String, dynamic>) {
final required = value['required'] as bool?;
if (required == true) {
params.add("'$key': request.$key");
} else {
params.add("if (request.$key != null) '$key': request.$key");
}
}
});
return params.join(',\n ');
}