From 293187704f59750da7eb60b76aaf2566b0bf462d Mon Sep 17 00:00:00 2001 From: StarProxima <34741787+StarProxima@users.noreply.github.com> Date: Tue, 9 Jan 2024 23:43:29 +0300 Subject: [PATCH] Extract schemas from url (#150) --- .../bin/extract_schemas_from_url.dart | 22 +++++++++++++++++++ .../lib/src/generator/generator.dart | 6 ++--- swagger_parser/lib/src/utils/utils.dart | 4 ++++ 3 files changed, 29 insertions(+), 3 deletions(-) create mode 100644 swagger_parser/bin/extract_schemas_from_url.dart diff --git a/swagger_parser/bin/extract_schemas_from_url.dart b/swagger_parser/bin/extract_schemas_from_url.dart new file mode 100644 index 00000000..2972b797 --- /dev/null +++ b/swagger_parser/bin/extract_schemas_from_url.dart @@ -0,0 +1,22 @@ +import 'package:swagger_parser/src/config/yaml_config.dart'; +import 'package:swagger_parser/src/utils/utils.dart'; +import 'package:swagger_parser/swagger_parser.dart'; + +/// Used for run `dart run swagger_parser` +Future main(List arguments) async { + introMessage(); + try { + /// Run generate from YAML config + final configs = YamlConfig.parseConfigsFromYamlFile(arguments); + + generateMessage(); + for (final config in configs) { + final generator = Generator.fromYamlConfig(config); + await generator.fetchSchemaContent(); + } + + doneExtractMessage(); + } on Exception catch (e) { + exitWithError('Failed to extract schemas from url.\n$e'); + } +} diff --git a/swagger_parser/lib/src/generator/generator.dart b/swagger_parser/lib/src/generator/generator.dart index 98d6142c..78752779 100644 --- a/swagger_parser/lib/src/generator/generator.dart +++ b/swagger_parser/lib/src/generator/generator.dart @@ -196,7 +196,7 @@ final class Generator { Future<(OpenApiInfo, GenerationStatistics)> generateFiles() async { final stopwatch = Stopwatch()..start(); - await _fetchSchemaContent(); + await fetchSchemaContent(); _parseOpenApiDefinitionFile(); await _generateFiles(); @@ -219,12 +219,12 @@ final class Generator { /// Generates content of files based on OpenApi definition file /// and return list of [GeneratedFile] Future> generateContent() async { - await _fetchSchemaContent(); + await fetchSchemaContent(); _parseOpenApiDefinitionFile(); return _fillContent(); } - Future _fetchSchemaContent() async { + Future fetchSchemaContent() async { final url = _schemaUrl; final path = _schemaPath; diff --git a/swagger_parser/lib/src/utils/utils.dart b/swagger_parser/lib/src/utils/utils.dart index 8af637f5..d176cfb7 100644 --- a/swagger_parser/lib/src/utils/utils.dart +++ b/swagger_parser/lib/src/utils/utils.dart @@ -197,6 +197,10 @@ void doneMessage({ } } +void doneExtractMessage() { + stdout.writeln('${_green}The extraction was completed successfully.$_reset'); +} + void exitWithError(String message) { stderr.writeln('${_red}ERROR: $message$_reset'); exit(2);