Complete OpenAPI 3.0/3.1 generation, diagnostics, lifecycle safety, enum and naming mappings, bilingual documentation, examples, and pub.dev release validation.
68 lines
2.0 KiB
Dart
68 lines
2.0 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:koi_swagger_parser/koi_swagger_parser.dart';
|
|
import 'package:path/path.dart' as p;
|
|
|
|
Future<void> main() async {
|
|
final tempDir = await Directory.systemTemp.createTemp(
|
|
'koi_swagger_parser_example_',
|
|
);
|
|
try {
|
|
final exampleDirectory = _findExampleDirectory();
|
|
final openApiPath = p.join(exampleDirectory.path, 'openapi.yaml');
|
|
final outputDirectory = p.join(tempDir.path, 'generated');
|
|
final configFile = File(p.join(tempDir.path, 'koi_swagger_parser.yaml'));
|
|
await configFile.writeAsString('''
|
|
input:
|
|
swagger_urls:
|
|
- ${_yamlString(openApiPath)}
|
|
|
|
output:
|
|
base_dir: ${_yamlString(outputDirectory)}
|
|
api_dir: ${_yamlString(p.join(outputDirectory, 'api'))}
|
|
models_dir: ${_yamlString(p.join(outputDirectory, 'models'))}
|
|
manifest_path: ${_yamlString(p.join(tempDir.path, 'manifest.json'))}
|
|
|
|
generation:
|
|
api:
|
|
response_wrapper:
|
|
mode: passthrough
|
|
|
|
formatting:
|
|
run_dart_format: true
|
|
''');
|
|
|
|
final result = await const KoiSwaggerParser().generate(
|
|
configPath: configFile.path,
|
|
);
|
|
|
|
print(
|
|
'Loaded ${result.sourceDocuments.length} source(s), '
|
|
'parsed ${result.operationsCount} operation(s), and '
|
|
'generated ${result.writtenFiles.length} file(s) for '
|
|
'${result.modelsCount} model(s):',
|
|
);
|
|
for (final file in result.writtenFiles) {
|
|
print(' ${p.relative(file, from: outputDirectory)}');
|
|
}
|
|
} finally {
|
|
await tempDir.delete(recursive: true);
|
|
}
|
|
}
|
|
|
|
Directory _findExampleDirectory() {
|
|
final scriptDirectory = File.fromUri(Platform.script).parent;
|
|
final workingDirectory = Directory(p.join(Directory.current.path, 'example'));
|
|
for (final directory in <Directory>[scriptDirectory, workingDirectory]) {
|
|
if (File(p.join(directory.path, 'openapi.yaml')).existsSync()) {
|
|
return directory;
|
|
}
|
|
}
|
|
throw StateError(
|
|
'Could not find example/openapi.yaml. Run this example from the package '
|
|
'root with "dart run example/koi_swagger_parser_example.dart".',
|
|
);
|
|
}
|
|
|
|
String _yamlString(String value) => "'${value.replaceAll("'", "''")}'";
|