Update registryTool task to better handle command line arguments (#273)

* Update registryTool task to better handle command line arguments

Tokens are delimited by pipes and can be escaped. If we use -args
directly, we will not be able to escape the delimiter, if both the
double and single quotes happen to be present in the arguments.

See:

https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/JavaExec.html#setArgsString-java.lang.String-
This commit is contained in:
Lai Jiang 2019-09-20 13:04:01 -04:00 committed by GitHub
parent 8f5bd5da01
commit 3764e2b26c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -592,11 +592,40 @@ task findGoldenImages(type: JavaExec) {
args arguments
}
// To run the nomulus tool:
// gradle registryTool --args="foo --bar"
// To run the nomulus tool with these command line tokens:
// "--foo", "bar baz", "--qux=quz"
// gradle registryTool --args="--foo 'bar baz' --qux=quz"
// or:
// gradle registryTool --PtoolArgs="--foo|bar baz|--qux=quz"
// Note that the delimiting pipe can be backslash escaped if it is part of a
// parameter.
task registryTool(type: JavaExec) {
classpath = sourceSets.main.runtimeClasspath
main = 'google.registry.tools.RegistryTool'
// If "-PtoolArgs=..." is present in the command line, use it to set the args,
// otherwise use the default flag, which is "--args" to set the args.
doFirst {
def toolArgs = rootProject.findProperty("toolArgs")
if (toolArgs != null) {
def delimiter = '|'
toolArgs += delimiter
def argsList = []
def currArg = ''
for (def i = 0; i < toolArgs.length(); i++) {
def currChar = toolArgs[i]
if (currChar != delimiter) {
currArg += currChar
} else if (i != 0 && toolArgs[i - 1] == '\\') {
currArg = currArg.substring(0, currArg.length() - 1) + currChar
} else {
argsList.add(currArg)
currArg = ''
}
}
args = argsList
}
}
}
task generateGoldenImages(type: Test) {