Skip to content

[OCaml] Add file post-processing #3583

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

Merged
merged 4 commits into from
Aug 8, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,7 @@ public void processOpts() {
if (StringUtils.isEmpty(System.getenv("TS_POST_PROCESS_FILE"))) {
LOGGER.info("Hint: Environment variable 'TS_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export TS_POST_PROCESS_FILE=\"/usr/local/bin/prettier --write\"' (Linux/Mac)");
LOGGER.info("Note: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
}
else if (!this.isEnablePostProcessFile()) {
} else if (!this.isEnablePostProcessFile()) {
LOGGER.info("Warning: Environment variable 'TS_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@
import io.swagger.v3.oas.models.media.*;
import io.swagger.v3.oas.models.parameters.Parameter;
import io.swagger.v3.oas.models.responses.ApiResponse;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.utils.ModelUtils;
import org.openapitools.codegen.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,6 +34,7 @@
import java.util.stream.Collectors;

import static org.apache.commons.lang3.StringUtils.capitalize;
import static org.openapitools.codegen.utils.StringUtils.camelize;
import static org.openapitools.codegen.utils.StringUtils.escape;
import static org.openapitools.codegen.utils.StringUtils.underscore;

Expand Down Expand Up @@ -409,6 +411,13 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("OCAML_POST_PROCESS_FILE"))) {
LOGGER.info("Hint: Environment variable 'OCAML_POST_PROCESS_FILE' (optional) not defined. E.g. to format the source code, please try 'export OCAML_POST_PROCESS_FILE=\"ocamlformat -i --enable-outside-detected-project\"' (Linux/Mac)");
LOGGER.info("Note: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
} else if (!this.isEnablePostProcessFile()) {
LOGGER.info("Warning: Environment variable 'OCAML_POST_PROCESS_FILE' is set but file post-processing is not enabled. To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
}

if (additionalProperties.containsKey(CodegenConstants.PACKAGE_NAME)) {
setPackageName((String) additionalProperties.get(CodegenConstants.PACKAGE_NAME));
} else {
Expand Down Expand Up @@ -605,11 +614,11 @@ public String toOperationId(String operationId) {

// method name cannot use reserved keyword, e.g. return
if (isReservedWord(sanitizedOperationId) || sanitizedOperationId.matches("^[0-9].*")) {
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + StringUtils.underscore("call_" + operationId));
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore("call_" + operationId));
sanitizedOperationId = "call_" + sanitizedOperationId;
}

return StringUtils.underscore(sanitizedOperationId);
return underscore(sanitizedOperationId);
}

private Map<String, Object> allowableValues(String valueString) {
Expand Down Expand Up @@ -754,4 +763,32 @@ public String toDefaultValue(Schema p) {
return null;
}
}

@Override
public void postProcessFile(File file, String fileType) {
super.postProcessFile(file, fileType);

if (file == null) {
return;
}
String ocamlPostProcessFile = System.getenv("OCAML_POST_PROCESS_FILE");
if (StringUtils.isEmpty(ocamlPostProcessFile)) {
return; // skip if OCAML_POST_PROCESS_FILE env variable is not defined
}
// only process files with ml or mli extension
if ("ml".equals(FilenameUtils.getExtension(file.toString())) || "mli".equals(FilenameUtils.getExtension(file.toString()))) {
String command = ocamlPostProcessFile + " " + file.toString();
try {
Process p = Runtime.getRuntime().exec(command);
int exitValue = p.waitFor();
if (exitValue != 0) {
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
} else {
LOGGER.info("Successfully executed: " + command);
}
} catch (Exception e) {
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
}
}
}
}
23 changes: 0 additions & 23 deletions samples/client/petstore/ocaml-client/.openapi-generator-ignore

This file was deleted.

This file was deleted.

6 changes: 0 additions & 6 deletions samples/client/petstore/ocaml-client/bin/dune

This file was deleted.

19 changes: 0 additions & 19 deletions samples/client/petstore/ocaml-client/bin/test.ml

This file was deleted.

9 changes: 0 additions & 9 deletions samples/client/petstore/ocaml-client/dune

This file was deleted.

2 changes: 0 additions & 2 deletions samples/client/petstore/ocaml-client/dune-project

This file was deleted.

15 changes: 0 additions & 15 deletions samples/client/petstore/ocaml-client/petstore_client.opam

This file was deleted.

80 changes: 0 additions & 80 deletions samples/client/petstore/ocaml-client/src/apis/pet_api.ml

This file was deleted.

15 changes: 0 additions & 15 deletions samples/client/petstore/ocaml-client/src/apis/pet_api.mli

This file was deleted.

38 changes: 0 additions & 38 deletions samples/client/petstore/ocaml-client/src/apis/store_api.ml

This file was deleted.

11 changes: 0 additions & 11 deletions samples/client/petstore/ocaml-client/src/apis/store_api.mli

This file was deleted.

72 changes: 0 additions & 72 deletions samples/client/petstore/ocaml-client/src/apis/user_api.ml

This file was deleted.

Loading