Skip to content

Commit

Permalink
1.12.0 Export file (#138)
Browse files Browse the repository at this point in the history
  • Loading branch information
StarProxima authored Nov 15, 2023
1 parent 2ad78e7 commit 9f82c37
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 25 deletions.
3 changes: 3 additions & 0 deletions swagger_parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.12.0
- Add new config parameter `export_file`

## 1.11.3
- Fix: annotating client methods with the first specified content type header in OpenAPI V2 schemas if the specified one is not the default.

Expand Down
14 changes: 8 additions & 6 deletions swagger_parser/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,19 @@ swagger_parser:
# Sets the OpenApi schema path directory for api definition.
schema_path: schemas/openapi.json
# Sets the url of the OpenApi schema
# Sets the url of the OpenApi schema.
schema_url: https://petstore.swagger.io/v2/swagger.json
# Required. Sets output directory for generated files (Clients and DTOs).
output_directory: lib/api
# Optional. Sets the programming language.
# Current available languages are: dart, kotlin
# Current available languages are: dart, kotlin.
language: dart
# Optional. If 'schema_path' and 'schema_url' are specified, what will be used.
# Current available options are: path, url.
prefer_schema_source: path
prefer_schema_source: url
# Optional (dart only). Set 'true' to generate data classes using freezed package.
freezed: false
Expand All @@ -74,16 +73,19 @@ swagger_parser:
# with interface and all clients instances.
root_client: true
# Optional (dart only). Set root client name
# Optional (dart only). Set root client name.
root_client_name: RestClient
# Optional. Set default content-type for all requests
# Optional. Set default content-type for all requests.
default_content_type: "application/json"
# Optional. Set API name for folder and export file
# If not specified, the file name is used.
name: null
# Optional (dart only). Set 'true' to generate export file.
export_file: true
# Optional. Set to 'true' to put the all api in its folder.
put_in_folder: false
Expand Down
10 changes: 6 additions & 4 deletions swagger_parser/example/swagger_parser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ swagger_parser:

# Sets the OpenApi schema path directory for api definition.
# schema_path: schemas/openapi.json

# Sets the url of the OpenApi schema
# Sets the url of the OpenApi schema.
# schema_url: https://petstore.swagger.io/v2/swagger.json

# Required. Sets output directory for generated files (Clients and DTOs).
Expand All @@ -28,13 +27,16 @@ swagger_parser:
# Optional (dart only). Set root client name
root_client_name: RestClient

# Optional. Set default content-type for all requests
# Optional. Set default content-type for all requests.
default_content_type: "application/json"

# Optional. Set API name for folder and export file (coming soon).
# Optional. Set API name for folder and export file.
# If not specified, the file name is used.
name: null

# Optional (dart only). Set 'true' to generate export file.
export_file: true

# Optional. Set to 'true' to put the all api in its folder.
put_in_folder: false

Expand Down
10 changes: 10 additions & 0 deletions swagger_parser/lib/src/config/yaml_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ final class YamlConfig {
this.freezed,
this.rootClient,
this.rootClientName,
this.exportFile,
this.clientPostfix,
this.putClientsInFolder,
this.squashClients,
Expand Down Expand Up @@ -157,6 +158,13 @@ final class YamlConfig {
);
}

final exportFile = yamlConfig['export_file'];
if (exportFile is! bool?) {
throw const ConfigException(
"Config parameter 'export_file' must be bool.",
);
}

final rawClientPostfix = yamlConfig['client_postfix']?.toString().trim();

final clientPostfix =
Expand Down Expand Up @@ -273,6 +281,7 @@ final class YamlConfig {
freezed: freezed ?? rootConfig?.freezed,
rootClient: rootClient ?? rootConfig?.rootClient,
rootClientName: rootClientName ?? rootConfig?.rootClientName,
exportFile: exportFile ?? rootConfig?.exportFile,
clientPostfix: clientPostfix ?? rootConfig?.clientPostfix,
putInFolder: putInFolder ?? rootConfig?.putInFolder,
putClientsInFolder: putClientsInFolder ?? rootConfig?.putClientsInFolder,
Expand Down Expand Up @@ -373,6 +382,7 @@ final class YamlConfig {
final String? clientPostfix;
final bool? rootClient;
final String? rootClientName;
final bool? exportFile;
final bool? squashClients;
final bool? pathMethodName;
final bool? putClientsInFolder;
Expand Down
22 changes: 20 additions & 2 deletions swagger_parser/lib/src/generator/fill_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,18 @@ final class FillController {
ProgrammingLanguage programmingLanguage = ProgrammingLanguage.dart,
String clientPostfix = 'Client',
String rootClientName = 'RestClient',
String exportFileName = 'export',
bool putClientsInFolder = false,
bool freezed = false,
bool enumsToJson = false,
bool unknownEnumValue = true,
bool markFilesAsGenerated = false,
String defaultContentType = 'application/json',
}) : _openApiInfo = openApiInfo,
_clientPostfix = clientPostfix,
_programmingLanguage = programmingLanguage,
_clientPostfix = clientPostfix,
_rootClientName = rootClientName,
_exportFileName = exportFileName,
_putClientsInFolder = putClientsInFolder,
_freezed = freezed,
_enumsToJson = enumsToJson,
Expand All @@ -32,8 +34,9 @@ final class FillController {

final OpenApiInfo _openApiInfo;
final ProgrammingLanguage _programmingLanguage;
final String _rootClientName;
final String _clientPostfix;
final String _rootClientName;
final String _exportFileName;
final bool _freezed;
final bool _putClientsInFolder;
final bool _enumsToJson;
Expand Down Expand Up @@ -89,4 +92,19 @@ final class FillController {
),
);
}

GeneratedFile fillExportFile({
required List<GeneratedFile> restClients,
required List<GeneratedFile> dataClasses,
GeneratedFile? rootClient,
}) =>
GeneratedFile(
name: '$_exportFileName.${_programmingLanguage.fileExtension}',
contents: _programmingLanguage.exportFileContent(
restClients: restClients,
dataClasses: dataClasses,
rootClient: rootClient,
markFileAsGenerated: _markFilesAsGenerated,
),
);
}
48 changes: 36 additions & 12 deletions swagger_parser/lib/src/generator/generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ final class Generator {
bool? freezed,
bool? rootClient,
String? clientPostfix,
bool? exportFile,
String? rootClientName,
bool? putClientsInFolder,
bool? squashClients,
Expand All @@ -61,6 +62,7 @@ final class Generator {
_freezed = freezed ?? false,
_rootClient = rootClient ?? true,
_rootClientName = rootClientName ?? 'RestClient',
_exportFile = exportFile ?? true,
_clientPostfix = clientPostfix ?? 'Client',
_putClientsInFolder = putClientsInFolder ?? false,
_squashClients = squashClients ?? false,
Expand All @@ -87,6 +89,7 @@ final class Generator {
freezed: yamlConfig.freezed,
rootClient: yamlConfig.rootClient,
rootClientName: yamlConfig.rootClientName,
exportFile: yamlConfig.exportFile,
clientPostfix: yamlConfig.clientPostfix,
putClientsInFolder: yamlConfig.putClientsInFolder,
squashClients: yamlConfig.squashClients,
Expand Down Expand Up @@ -137,6 +140,9 @@ final class Generator {
/// Root client name
final String _rootClientName;

/// Generate export file
final bool _exportFile;

/// Client postfix
final String _clientPostfix;

Expand Down Expand Up @@ -302,25 +308,43 @@ final class Generator {
programmingLanguage: _programmingLanguage,
rootClientName: _rootClientName,
clientPostfix: _clientPostfix,
exportFileName: _name ?? 'export',
freezed: _freezed,
putClientsInFolder: _putClientsInFolder,
enumsToJson: _enumsToJson,
unknownEnumValue: _unknownEnumValue,
markFilesAsGenerated: _markFilesAsGenerated,
defaultContentType: _defaultContentType,
);
final files = <GeneratedFile>[];
for (final client in _restClients) {
files.add(fillController.fillRestClientContent(client));
}
for (final dataClass in _dataClasses) {
files.add(fillController.fillDtoContent(dataClass));
}
if (_rootClient &&
_programmingLanguage == ProgrammingLanguage.dart &&
_restClients.isNotEmpty) {
files.add(fillController.fillRootClient(_restClients));
}

final restClientFiles =
_restClients.map(fillController.fillRestClientContent).toList();

final dataClassesFiles =
_dataClasses.map(fillController.fillDtoContent).toList();

final rootClientFile = _programmingLanguage == ProgrammingLanguage.dart &&
_rootClient &&
_restClients.isNotEmpty
? fillController.fillRootClient(_restClients)
: null;

final exportFile =
_programmingLanguage == ProgrammingLanguage.dart && _exportFile
? fillController.fillExportFile(
restClients: restClientFiles,
dataClasses: dataClassesFiles,
rootClient: rootClientFile,
)
: null;

final files = [
...restClientFiles,
...dataClassesFiles,
if (rootClientFile != null) rootClientFile,
if (exportFile != null) exportFile,
];

return files;
}
}
18 changes: 18 additions & 0 deletions swagger_parser/lib/src/generator/models/programming_language.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:collection/collection.dart';

import '../generator_exception.dart';
import '../templates/dart_enum_dto_template.dart';
import '../templates/dart_export_file_template.dart';
import '../templates/dart_freezed_dto_template.dart';
import '../templates/dart_json_serializable_dto_template.dart';
import '../templates/dart_retrofit_client_template.dart';
Expand All @@ -11,6 +12,7 @@ import '../templates/kotlin_enum_dto_template.dart';
import '../templates/kotlin_moshi_dto_template.dart';
import '../templates/kotlin_retrofit_client_template.dart';
import '../templates/kotlin_typedef_template.dart';
import 'generated_file.dart';
import 'open_api_info.dart';
import 'universal_data_class.dart';
import 'universal_rest_client.dart';
Expand Down Expand Up @@ -132,4 +134,20 @@ enum ProgrammingLanguage {
),
kotlin => '',
};

String exportFileContent({
required bool markFileAsGenerated,
required List<GeneratedFile> restClients,
required List<GeneratedFile> dataClasses,
GeneratedFile? rootClient,
}) =>
switch (this) {
dart => dartExportFileTemplate(
markFileAsGenerated: markFileAsGenerated,
restClients: restClients,
dataClasses: dataClasses,
rootClient: rootClient,
),
kotlin => '',
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import '../../utils/utils.dart';
import '../models/generated_file.dart';

/// Provides template for generating dart export file
String dartExportFileTemplate({
required bool markFileAsGenerated,
required List<GeneratedFile> restClients,
required List<GeneratedFile> dataClasses,
GeneratedFile? rootClient,
}) {
final restClientsNames = restClients.map((e) => e.name).toSet();
final dataClassesNames = dataClasses.map((e) => e.name).toSet();
final rootClientName = rootClient?.name;

return '${generatedFileComment(
markFileAsGenerated: markFileAsGenerated,
)}'
'${rootClientName != null ? '// Root client\n' : ''}'
'${rootClientName != null ? "export '$rootClientName';" : ''}'
'${rootClientName != null ? '\n\n' : ''}'
'${restClientsNames.isNotEmpty ? '// Clients\n' : ''}'
'${restClientsNames.map((e) => "export '$e';").join('\n')}'
'${restClientsNames.isNotEmpty ? '\n\n' : ''}'
'${dataClassesNames.isNotEmpty ? '// Data classes\n' : ''}'
'${dataClassesNames.map((e) => "export '$e';").join('\n')}';
}
2 changes: 1 addition & 1 deletion swagger_parser/lib/src/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void summaryStatisticsMessage({
'${formatNumber(statistics.totalRestClients)} clients, '
'${formatNumber(statistics.totalRequests)} requests, '
'${formatNumber(statistics.totalDataClasses)} data classes.\n'
'${formatNumber(statistics.totalFiles)} files with ${formatNumber(statistics.totalLines)} lines of code.',
'${formatNumber(statistics.totalFiles)} files with ${formatNumber(statistics.totalLines)} lines of code.\n',
);
}

Expand Down

0 comments on commit 9f82c37

Please sign in to comment.