Skip to content

Commit 4ebef71

Browse files
wing328michaelpro1
authored andcommitted
[PS] add file post-processing to the PowerShell generator (OpenAPITools#5864)
* add post process to ps generator * add import * fix merge issue
1 parent 0408b70 commit 4ebef71

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PowerShellExperimentalClientCodegen.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.swagger.v3.oas.models.media.ArraySchema;
2020
import io.swagger.v3.oas.models.media.Schema;
21+
import org.apache.commons.io.FilenameUtils;
2122
import org.apache.commons.lang3.StringUtils;
2223
import org.openapitools.codegen.*;
2324
import org.openapitools.codegen.meta.GeneratorMetadata;
@@ -537,6 +538,11 @@ public void setPowershellGalleryUrl(String powershellGalleryUrl) {
537538
public void processOpts() {
538539
super.processOpts();
539540

541+
if (StringUtils.isEmpty(System.getenv("POWERSHELL_POST_PROCESS_FILE"))) {
542+
LOGGER.info("Environment variable POWERSHELL_POST_PROCESS_FILE not defined so the PowerShell code may not be properly formatted. To define it, try 'export POWERSHELL_POST_PROCESS_FILE=\"Edit-DTWBeautifyScript\"'");
543+
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
544+
}
545+
540546
if (additionalProperties.containsKey("powershellGalleryUrl")) {
541547
setPowershellGalleryUrl((String) additionalProperties.get("powershellGalleryUrl"));
542548
} else {
@@ -1036,6 +1042,34 @@ private String toMethodName(String operationId) {
10361042
return "Invoke-" + apiNamePrefix + methodName;
10371043
}
10381044

1045+
@Override
1046+
public void postProcessFile(File file, String fileType) {
1047+
if (file == null) {
1048+
return;
1049+
}
1050+
String powershellPostProcessFile = System.getenv("POWERSHELL_POST_PROCESS_FILE");
1051+
if (StringUtils.isEmpty(powershellPostProcessFile)) {
1052+
return; // skip if POWERSHELL_POST_PROCESS_FILE env variable is not defined
1053+
}
1054+
1055+
// only process files with ps extension
1056+
if ("ps".equals(FilenameUtils.getExtension(file.toString()))) {
1057+
String command = powershellPostProcessFile + " " + file.toString();
1058+
try {
1059+
Process p = Runtime.getRuntime().exec(command);
1060+
int exitValue = p.waitFor();
1061+
if (exitValue != 0) {
1062+
LOGGER.error("Error running the command ({}). Exit value: {}", command, exitValue);
1063+
} else {
1064+
LOGGER.info("Successfully executed: " + command);
1065+
}
1066+
} catch (Exception e) {
1067+
LOGGER.error("Error running the command ({}). Exception: {}", command, e.getMessage());
1068+
}
1069+
}
1070+
1071+
}
1072+
10391073
@Override
10401074
public String toRegularExpression(String pattern) {
10411075
return escapeText(pattern);

0 commit comments

Comments
 (0)