[Rust Server] Run rustfmt over rust-server output (#5558)

* Run rustfmt over rust-server output

Automatically run rustfmt over rust-server output if --enable-post-process-file is set

* Update samples

* Add note about configuring post processing
This commit is contained in:
Richard Whitehouse
2020-03-24 00:09:40 +00:00
committed by GitHub
parent 15a2390fe5
commit 28ea718004
4 changed files with 41 additions and 5 deletions

View File

@@ -28,6 +28,7 @@ import io.swagger.v3.oas.models.media.XML;
import io.swagger.v3.oas.models.parameters.RequestBody;
import io.swagger.v3.oas.models.responses.ApiResponse;
import io.swagger.v3.oas.models.servers.Server;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.features.*;
@@ -254,6 +255,14 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
public void processOpts() {
super.processOpts();
if (StringUtils.isEmpty(System.getenv("RUST_POST_PROCESS_FILE"))) {
LOGGER.info("Environment variable RUST_POST_PROCESS_FILE not defined. rustfmt will be used" +
" by default. To choose a different tool, try" +
" 'export RUST_POST_PROCESS_FILE=\"/usr/local/bin/rustfmt\"' (Linux/Mac)");
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` " +
" (--enable-post-process-file for CLI).");
}
if (!Boolean.TRUE.equals(ModelUtils.isGenerateAliasAsModel())) {
LOGGER.warn("generateAliasAsModel is set to false, which means array/map will be generated as model instead and the resulting code may have issues. Please enable `generateAliasAsModel` to address the issue.");
}
@@ -1566,4 +1575,31 @@ public class RustServerCodegen extends DefaultCodegen implements CodegenConfig {
param.vendorExtensions.put("example", (example != null) ? "Some(" + example + ")" : "None");
}
}
@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}
String commandPrefix = System.getenv("RUST_POST_PROCESS_FILE");
if (StringUtils.isEmpty(commandPrefix)) {
commandPrefix = "rustfmt";
}
// only process files with .rs extension
if ("rs".equals(FilenameUtils.getExtension(file.toString()))) {
try {
Process p = Runtime.getRuntime().exec(new String[] {commandPrefix, file.toString()});
int exitValue = p.waitFor();
if (exitValue != 0) {
LOGGER.error("Error running the command ({} {}). Exit code: {}", commandPrefix, file.toString(), exitValue);
} else {
LOGGER.info("Successfully executed: {} {}", commandPrefix, file.toString());
}
} catch (Exception e) {
LOGGER.error("Error running the command ({} ()). Exception: {}", commandPrefix, file.toString(), e.getMessage());
}
}
}
}

View File

@@ -468,7 +468,7 @@
{
body,
{{/-first}}
{{{name}}}{{^-last}}, {{/-last}}
{{{name}}}{{^-last}},{{/-last}}
{{#-last}}
}
{{/-last}}
@@ -479,7 +479,7 @@
{{#-first}}
{
{{/-first}}
{{{name}}}{{^-last}}, {{/-last}}
{{{name}}}{{^-last}},{{/-last}}
{{#-last}}
}
{{/-last}}

View File

@@ -819,7 +819,7 @@ where
ResponsesWithHeadersGetResponse::Success
{
body,
success_info,
success_info,
object_header
}
=> {
@@ -841,7 +841,7 @@ where
},
ResponsesWithHeadersGetResponse::PreconditionFailed
{
further_info,
further_info,
failure_info
}
=> {

View File

@@ -2887,7 +2887,7 @@ where
LoginUserResponse::SuccessfulOperation
{
body,
x_rate_limit,
x_rate_limit,
x_expires_after
}
=> {