Skip to content

[PS] add file post-processing to the PowerShell generator #5864

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
Apr 10, 2020
Merged
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 @@ -18,6 +18,7 @@

import io.swagger.v3.oas.models.media.ArraySchema;
import io.swagger.v3.oas.models.media.Schema;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.*;
import org.openapitools.codegen.meta.GeneratorMetadata;
Expand Down Expand Up @@ -501,6 +502,11 @@ public void setPowershellGalleryUrl(String powershellGalleryUrl) {
public void processOpts() {
super.processOpts();

if (StringUtils.isEmpty(System.getenv("POWERSHELL_POST_PROCESS_FILE"))) {
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\"'");
LOGGER.info("NOTE: To enable file post-processing, 'enablePostProcessFile' must be set to `true` (--enable-post-process-file for CLI).");
}

if (additionalProperties.containsKey("powershellGalleryUrl")) {
setPowershellGalleryUrl((String) additionalProperties.get("powershellGalleryUrl"));
} else {
Expand Down Expand Up @@ -1020,6 +1026,34 @@ private String toMethodName(String operationId) {
return "Invoke-" + apiNamePrefix + methodName;
}

@Override
public void postProcessFile(File file, String fileType) {
if (file == null) {
return;
}
String powershellPostProcessFile = System.getenv("POWERSHELL_POST_PROCESS_FILE");
if (StringUtils.isEmpty(powershellPostProcessFile)) {
return; // skip if POWERSHELL_POST_PROCESS_FILE env variable is not defined
}

// only process files with ps extension
if ("ps".equals(FilenameUtils.getExtension(file.toString()))) {
String command = powershellPostProcessFile + " " + 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());
}
}

}

@Override
public String toRegularExpression(String pattern) {
return escapeText(pattern);
Expand Down