Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a format stage to the end of nextGen #100

Merged
merged 3 commits into from
Aug 30, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 63 additions & 30 deletions openapi-generator/lib/src/openapi_generator_runner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
communication: OutputMessage(
message: [
'',
':::::::::::::::::::::::::::::::::::::::::::',
':: Openapi generator for dart ::',
':::::::::::::::::::::::::::::::::::::::::::',
' Openapi generator for dart ',
gibahjoe marked this conversation as resolved.
Show resolved Hide resolved
'',
].join('\n'),
),
Expand Down Expand Up @@ -99,7 +97,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
late OutputMessage communication;
if (e is! OutputMessage) {
communication = OutputMessage(
message: ' - There was an error generating the spec.',
message: '- There was an error generating the spec.',
level: Level.SEVERE,
additionalContext: e,
stackTrace: st,
Expand All @@ -109,13 +107,6 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
}

logOutputMessage(log: log, communication: communication);
} finally {
// logOutputMessage(
// log: log,
// communication: OutputMessage(
// message: ':::::::::::::::::::::::::::::::::::::::::::',
// ),
// );
}
return '';
}
Expand Down Expand Up @@ -158,7 +149,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
if (result.exitCode != 0) {
return Future.error(
OutputMessage(
message: 'Codegen Failed. Generator output: ',
message: 'Codegen Failed. Generator output:',
level: Level.SEVERE,
additionalContext: result.stderr,
stackTrace: StackTrace.current,
Expand All @@ -170,7 +161,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
communication: OutputMessage(
message: [
if (arguments.isDebug) result.stdout,
'Openapi generator completed successfully. ',
'Openapi generator completed successfully.',
].join('\n'),
),
);
Expand Down Expand Up @@ -253,6 +244,18 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
),
);
} finally {
await formatCode(args: args).then(
(_) {},
onError: (e, st) => logOutputMessage(
log: log,
communication: OutputMessage(
message: 'Failed to format generated code.',
additionalContext: e,
stackTrace: st,
level: Level.SEVERE,
),
),
);
await updateAnnotatedFile(annotatedPath: annotatedPath).then(
(_) => logOutputMessage(
log: log,
Expand All @@ -264,7 +267,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
onError: (e, st) => logOutputMessage(
log: log,
communication: OutputMessage(
message: 'Failed to update annotated class file. Failing silently.',
message: 'Failed to update annotated class file.',
level: Level.WARNING,
additionalContext: e,
stackTrace: st,
Expand Down Expand Up @@ -301,29 +304,28 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Skipping source gen step due to flag being set. ::',
message: 'Skipping source gen step due to flag being set.',
level: Level.WARNING,
),
);
} else if (!args.shouldGenerateSources) {
logOutputMessage(
log: log,
communication: OutputMessage(
message:
':: Skipping source gen because generator does not need it. ::',
message: 'Skipping source gen because generator does not need it.',
),
);
} else {
return await runSourceGen(baseCommand: baseCommand, args: args).then(
(_) => logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Sources generated successfully. ::',
message: 'Sources generated successfully.',
),
),
onError: (e, st) => Future.error(
OutputMessage(
message: ':: Could not complete source generation ::',
message: 'Could not complete source generation',
additionalContext: e,
stackTrace: st,
level: Level.SEVERE,
Expand All @@ -339,7 +341,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Running source code generation. ::',
message: 'Running source code generation.',
),
);
final command = Command(
Expand All @@ -352,7 +354,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: ${command.executable} ${command.arguments.join(' ')} ::',
message: '${command.executable} ${command.arguments.join(' ')}',
),
);

Expand All @@ -371,8 +373,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
if (results.exitCode != 0) {
return Future.error(
OutputMessage(
message:
':: Failed to generate source code. Build Command output: ::',
message: 'Failed to generate source code. Build Command output:',
level: Level.SEVERE,
additionalContext: results.stderr,
stackTrace: StackTrace.current,
Expand All @@ -382,7 +383,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Codegen completed successfully. ::',
message: 'Codegen completed successfully.',
),
);
}
Expand All @@ -395,7 +396,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Skipping install step because flag was set. ::',
message: 'Skipping install step because flag was set.',
level: Level.WARNING,
),
);
Expand All @@ -409,7 +410,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
log: log,
communication: OutputMessage(
message:
':: Installing dependencies with generated source. ${command.executable} ${command.arguments.join(' ')} ::',
'Installing dependencies with generated source. ${command.executable} ${command.arguments.join(' ')}',
),
);

Expand All @@ -428,7 +429,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
if (results.exitCode != 0) {
return Future.error(
OutputMessage(
message: ':: Install within generated sources failed. ::',
message: 'Install within generated sources failed.',
level: Level.SEVERE,
additionalContext: results.stderr,
stackTrace: StackTrace.current,
Expand All @@ -440,7 +441,7 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
communication: OutputMessage(
message: [
if (args.isDebug) results.stdout,
':: Install completed successfully. ::',
'Install completed successfully.',
].join('\n'),
),
);
Expand Down Expand Up @@ -468,14 +469,14 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Found generated timestamp. Updating with $now ::',
message: 'Found generated timestamp. Updating with $now',
),
);
} else {
logOutputMessage(
log: log,
communication: OutputMessage(
message: ':: Creating generated timestamp with $now ::',
message: 'Creating generated timestamp with $now',
),
);
}
Expand All @@ -493,4 +494,36 @@ class OpenapiGenerator extends GeneratorForAnnotation<annots.Openapi> {
);
}
}

/// Format the generated code in the output directory.
Future<void> formatCode({required GeneratorArguments args}) async {
final command = Command(executable: 'dart', arguments: ['format', './']);
ProcessResult result;
if (!testMode) {
result = await Process.run(
gibahjoe marked this conversation as resolved.
Show resolved Hide resolved
command.executable,
command.arguments,
workingDirectory: args.outputDirectory,
runInShell: Platform.isWindows,
);
} else {
result = ProcessResult(99999, 0, null, null);
}

if (result.exitCode != 0) {
return Future.error(
OutputMessage(
message: 'Failed to format generated code.',
additionalContext: result.stderr,
stackTrace: StackTrace.current,
level: Level.SEVERE,
),
);
} else {
logOutputMessage(
log: log,
communication:
OutputMessage(message: 'Successfully formatted code.'));
}
}
}