Skip to content
Permalink
Browse files
Added [docgen.insertWithOutput ... systemProperties={"SOME_NAME": "so…
…meValue", ... } ... ]
  • Loading branch information
ddekany committed Sep 12, 2021
1 parent f375bd8 commit 69fbe68faff41aa014fb5e8ab444463540c27816
Showing 1 changed file with 64 additions and 3 deletions.
@@ -26,19 +26,24 @@
import java.io.PrintStream;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
@@ -48,7 +53,6 @@
import org.apache.commons.io.IOUtils;
import org.apache.commons.io.input.ClosedInputStream;
import org.apache.commons.io.output.WriterOutputStream;
import org.apache.commons.text.StringEscapeUtils;

import com.google.common.collect.ImmutableList;

@@ -359,9 +363,11 @@ private void insertCommandAndOutput(InsertDirectiveType insertDirectiveType, Ins
})
.collect(Collectors.toList());

Object cmdExitCode;
Map<String, String> systemPropertiesToRestore = new HashMap<>();
try {
cmdExitCode = mainMethod.invoke(null, (Object) cmdArgs.toArray(new String[0]));
Object cmdExitCode = withSystemPropertiesOverridden(
Optional.ofNullable(args.systemProperties).orElse(Collections.emptyMap()),
() -> mainMethod.invoke(null, (Object) cmdArgs.toArray(new String[0])));
if (cmdExitCode instanceof Integer && ((Integer) cmdExitCode) != 0) {
cmdRunExceptionShortMessage = "Command execution has returned with non-0 exit code " + cmdExitCode + ".";
cmdRunException = newErrorInInsertOutputCommandException(
@@ -404,6 +410,30 @@ private void insertCommandAndOutput(InsertDirectiveType insertDirectiveType, Ins
}
}

private <T> T withSystemPropertiesOverridden(Map<String, String> systemPropertyOverrides, Callable<T> body)
throws Exception {
Map<String, String> systemPropertiesToRestore = new HashMap<>();
systemPropertyOverrides.forEach((key, value) -> {
systemPropertiesToRestore.put(key, System.getProperty(key));
if (value != null) {
System.setProperty(key, value);
} else {
System.clearProperty(key);
}
});
try {
return body.call();
} finally {
systemPropertiesToRestore.forEach((key, value) -> {
if (value == null) {
System.clearProperty(key);
} else {
System.setProperty(key, value);
}
});
}
}

private TemplateException newErrorInInsertOutputCommandException(
String specificMessage,
Transform.InsertableOutputCommandProperties cmdProps, List<String> cmdArgs,
@@ -681,6 +711,33 @@ private Boolean fetchOptionalBoolean() throws DocgenTagException {
}
}

private Map<String, String> fetchRequiredStringToStringMap() throws TemplateException {
Map<String, String> result = fetchOptionalStringToStringMap();
if (result == null) {
throw newUnexpectedTokenException("map", env);
}
return result;
}

private Map<String, String> fetchOptionalStringToStringMap() throws TemplateException {
Map<String, String> stringToStringMap = new LinkedHashMap<>();

skipRequiredToken("{");
fetchKeyValuePairs: do {
String key = fetchOptionalString();
if (key == null) {
break fetchKeyValuePairs;
}
skipRequiredToken(":");
String value = fetchRequiredString();

stringToStringMap.put(key, value);
} while (skipOptionalToken(","));
skipRequiredToken("}");

return stringToStringMap;
}

private char charAt(int index) {
return index < text.length() ? text.charAt(index) : 0;
}
@@ -744,6 +801,9 @@ private InsertDirectiveArgs fetchInsertDirectiveArgs(
args.toOptional = fetchRequiredBoolean();
} else if (insertDirectiveType == INSERT_WITH_OUTPUT && paramName.equals("printCommand")) {
args.printCommand = fetchRequiredBoolean();
} else if ((insertDirectiveType == INSERT_WITH_OUTPUT || insertDirectiveType == CHECK_COMMAND)
&& paramName.equals("systemProperties")) {
args.systemProperties = fetchRequiredStringToStringMap();
} else {
throw new DocgenTagException(
"Unsupported docgen." + subvarName + " parameter " + StringUtil.jQuote(paramName) + ".",
@@ -855,6 +915,7 @@ static class InsertDirectiveArgs {
private boolean fromOptional;
private Pattern to;
private boolean toOptional;
private Map<String, String> systemProperties;
private String body;
private int indexAfterDirective;
private boolean printCommand;

0 comments on commit 69fbe68

Please sign in to comment.