Skip to content

Commit

Permalink
Made some fix (epam-cross-platform-lab#762)
Browse files Browse the repository at this point in the history
* Made some fix

* Updated pubspec and changelog

* Updated version

---------

Co-authored-by: Uladzimir Paliukhovich <[email protected]>
  • Loading branch information
2 people authored and AnnaKandel committed Nov 3, 2024
1 parent 4073835 commit c8da9e9
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 50 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# 2.15.3
* ***Breaking*** Added support of Chopper 8.0

# 3.0.0

* Fixed generation of keywords in JsonKeys ([#734](https://github.com/epam-cross-platform-lab/swagger-dart-code-generator/issues/734))

Expand Down
75 changes: 27 additions & 48 deletions lib/swagger_dart_code_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ String normal(String path) {

Iterable<FileSystemEntity> _getInputFolderFilesList(GeneratorOptions options) {
return Directory(normalize(options.inputFolder)).listSync().where(
(FileSystemEntity file) =>
_inputFileExtensions.any((ending) => file.path.endsWith(ending)));
(FileSystemEntity file) => _inputFileExtensions.any((ending) => file.path.endsWith(ending)));
}

String _getAdditionalResultPath(GeneratorOptions options) {
Expand All @@ -47,8 +46,8 @@ String _getAdditionalResultPath(GeneratorOptions options) {

if (options.inputUrls.isNotEmpty) {
final firstUrl = options.inputUrls.first;
final path = normalize(
'${options.inputFolder}${firstUrl.fileName ?? getFileNameBase(firstUrl.url)}');
final path =
normalize('${options.inputFolder}${firstUrl.fileName ?? getFileNameBase(firstUrl.url)}');
File(path).createSync();
return path;
}
Expand All @@ -61,8 +60,7 @@ Map<String, List<String>> _generateExtensions(GeneratorOptions options) {

final filesList = _getInputFolderFilesList(options);

additionalResultPath =
_getAdditionalResultPath(options).replaceAll('\\', '/');
additionalResultPath = _getAdditionalResultPath(options).replaceAll('\\', '/');

File(additionalResultPath).createSync();

Expand Down Expand Up @@ -90,21 +88,16 @@ Map<String, List<String>> _generateExtensions(GeneratorOptions options) {
}

for (var inputUrl in options.inputUrls) {
if (fileNames
.contains(getFileNameBase(inputUrl.fileName ?? inputUrl.url))) {
if (fileNames.contains(getFileNameBase(inputUrl.fileName ?? inputUrl.url))) {
continue;
}

final name =
removeFileExtension(getFileNameBase(inputUrl.fileName ?? inputUrl.url));
final name = removeFileExtension(getFileNameBase(inputUrl.fileName ?? inputUrl.url));

result[additionalResultPath]!.add(join(out, '$name$_outputFileExtension'));
result[additionalResultPath]!
.add(join(out, '$name$_outputEnumsFileExtension'));
result[additionalResultPath]!
.add(join(out, '$name$_outputModelsFileExtension'));
result[additionalResultPath]!
.add(join(out, '$name$_outputResponsesFileExtension'));
result[additionalResultPath]!.add(join(out, '$name$_outputEnumsFileExtension'));
result[additionalResultPath]!.add(join(out, '$name$_outputModelsFileExtension'));
result[additionalResultPath]!.add(join(out, '$name$_outputResponsesFileExtension'));
}

///Register additional outputs in first input
Expand Down Expand Up @@ -235,30 +228,22 @@ class SwaggerDartCodeGenerator implements Builder {

final dateToJson = codeGenerator.generateDateToJson(options);

final copyAssetId = AssetId(
buildStep.inputId.package,
join(options.outputFolder,
'$fileNameWithoutExtension$_outputFileExtension'));
final copyAssetId = AssetId(buildStep.inputId.package,
join(options.outputFolder, '$fileNameWithoutExtension$_outputFileExtension'));

if (!options.separateModels || !options.buildOnlyModels) {
await buildStep.writeAsString(
copyAssetId,
_generateFileContent(
imports,
requests,
options.separateModels ? '' : models,
customDecoder,
options.separateModels ? '' : dateToJson));
_generateFileContent(imports, requests, options.separateModels ? '' : models,
customDecoder, options.separateModels ? '' : dateToJson));
}

if (enums.isNotEmpty) {
///Write enums
final formatterEnums = _tryFormatCode(enums);

final enumsAssetId = AssetId(
buildStep.inputId.package,
join(options.outputFolder,
'$fileNameWithoutExtension$_outputEnumsFileExtension'));
final enumsAssetId = AssetId(buildStep.inputId.package,
join(options.outputFolder, '$fileNameWithoutExtension$_outputEnumsFileExtension'));

await buildStep.writeAsString(enumsAssetId, formatterEnums);
}
Expand All @@ -272,17 +257,15 @@ class SwaggerDartCodeGenerator implements Builder {
enums.isNotEmpty,
));

final enumsAssetId = AssetId(
buildStep.inputId.package,
join(options.outputFolder,
'$fileNameWithoutExtension$_outputModelsFileExtension'));
final enumsAssetId = AssetId(buildStep.inputId.package,
join(options.outputFolder, '$fileNameWithoutExtension$_outputModelsFileExtension'));

await buildStep.writeAsString(enumsAssetId, formattedModels);
}
}

String _generateFileContent(String imports, String requests, String models,
String customDecoder, String dateToJson) {
String _generateFileContent(
String imports, String requests, String models, String customDecoder, String dateToJson) {
final result = """
$imports
Expand Down Expand Up @@ -310,12 +293,11 @@ $dateToJson
}
}

Future<void> _generateAdditionalFiles(AssetId inputId, BuildStep buildStep,
bool hasModels, List<String> allFiles) async {
Future<void> _generateAdditionalFiles(
AssetId inputId, BuildStep buildStep, bool hasModels, List<String> allFiles) async {
final codeGenerator = SwaggerCodeGenerator();

final indexAssetId =
AssetId(inputId.package, join(options.outputFolder, _indexFileName));
final indexAssetId = AssetId(inputId.package, join(options.outputFolder, _indexFileName));

final imports = codeGenerator.generateIndexes(allFiles, options);

Expand All @@ -324,11 +306,9 @@ $dateToJson
}

if (options.withConverter && !options.buildOnlyModels) {
final mappingAssetId = AssetId(
inputId.package, join(options.outputFolder, _mappingFileName));
final mappingAssetId = AssetId(inputId.package, join(options.outputFolder, _mappingFileName));

final mapping =
codeGenerator.generateConverterMappings(hasModels, options);
final mapping = codeGenerator.generateConverterMappings(hasModels, options);

await buildStep.writeAsString(mappingAssetId, formatter.format(mapping));
}
Expand All @@ -340,13 +320,12 @@ $dateToJson
String dateToJson,
bool hasEnums,
) {
final enumsImport = hasEnums
? "import '$fileNameWithoutExtension.enums.swagger.dart' as enums;"
: '';
final enumsImport =
hasEnums ? "import '$fileNameWithoutExtension.enums.swagger.dart' as enums;" : '';

final overridenModels = options.overridenModels.isEmpty
? ''
: 'import \'overriden_models.dart\';';
: options.overridenModels.map((e) => 'import \'${e.importUrl}\';').join('\n');

return '''
// ignore_for_file: type=lint
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: swagger_dart_code_generator

version: 2.15.2
version: 3.0.0

homepage: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
repository: https://github.com/epam-cross-platform-lab/swagger-dart-code-generator
Expand Down

0 comments on commit c8da9e9

Please sign in to comment.