From 3764e2b26c5f7761385696d2f3ff52af7a455125 Mon Sep 17 00:00:00 2001 From: Lai Jiang Date: Fri, 20 Sep 2019 13:04:01 -0400 Subject: [PATCH] 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- --- core/build.gradle | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/core/build.gradle b/core/build.gradle index 41d3152c9..3e9279699 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -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) {