Completely remove the usage of hardcoded command line options

This commit is contained in:
Guillaume Nodet
2020-12-10 00:27:41 +01:00
parent 8b9ba24713
commit 438e76c370
3 changed files with 66 additions and 57 deletions

View File

@@ -29,6 +29,7 @@ import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
@@ -83,24 +84,18 @@ public class DefaultClient implements Client {
// System properties
for (Iterator<String> it = args.iterator(); it.hasNext();) {
String arg = it.next();
String val;
if (arg.startsWith("-D")) {
val = arg.substring(2);
} else if (arg.equals("--define")) {
if (it.hasNext()) {
val = it.next();
} else {
throw new IllegalArgumentException("Missing argument for option --define");
String val = Environment.MAVEN_DEFINE.removeCommandLineOption(new ArrayList<>(Collections.singletonList(arg)));
if (val != null) {
if (val.isEmpty()) {
throw new IllegalArgumentException("Missing argument for option " + arg);
}
/* This needs to be done very early, otherwise various DaemonParameters do not work properly */
final int eqPos = val.indexOf('=');
if (eqPos >= 0) {
System.setProperty(val.substring(2, eqPos), val.substring(eqPos + 1));
} else {
System.setProperty(val.substring(2), "");
}
} else {
continue;
}
/* This needs to be done very early, otherwise various DaemonParameters do not work properly */
final int eqPos = val.indexOf('=');
if (eqPos >= 0) {
System.setProperty(val.substring(2, eqPos), val.substring(eqPos + 1));
} else {
System.setProperty(val.substring(2), "");
}
}
@@ -136,31 +131,10 @@ public class DefaultClient implements Client {
LOGGER.debug("Starting client");
final List<String> args = new ArrayList<>(argv);
boolean version = false;
boolean showVersion = false;
boolean debug = false;
boolean batchMode = false;
for (String arg : args) {
switch (arg) {
case "-v":
case "-version":
case "--version":
version = true;
break;
case "-V":
case "--show-version":
showVersion = true;
break;
case "-X":
case "--debug":
debug = true;
break;
case "-B":
case "--batch-mode":
batchMode = true;
break;
}
}
boolean version = Environment.MAVEN_VERSION.hasCommandLineOption(args);
boolean showVersion = Environment.MAVEN_SHOW_VERSION.hasCommandLineOption(args);
boolean debug = Environment.MAVEN_DEBUG.hasCommandLineOption(args);
boolean batchMode = Environment.MAVEN_BATCH_MODE.hasCommandLineOption(args);
// Print version if needed
if (version || showVersion || debug) {

View File

@@ -16,20 +16,46 @@
package org.mvndaemon.mvnd.client;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mvndaemon.mvnd.common.Environment;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EnvironmentTest {
@Test
void arguments() {
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-Dfoo=bar")));
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D", "foo=bar")));
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("--define", "foo=bar")));
assertEquals("foo=bar", Environment.MAVEN_DEFINE.removeCommandLineOption(list("--define=foo=bar")));
assertEquals("foo", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D=foo")));
assertEquals("foo", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-Dfoo")));
assertEquals("foo", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D", "foo")));
assertEquals("foo=", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-Dfoo=")));
assertEquals("", Environment.MAVEN_DEFINE.removeCommandLineOption(list("-D")));
assertEquals("", Environment.MAVEN_DEFINE.removeCommandLineOption(list("--define")));
}
private List<String> list(String... items) {
return new ArrayList<>(Arrays.asList(items));
}
@Test
void prop() {
try (EnvironmentResource env = new EnvironmentResource()) {
env.props("mvnd.home", "/maven/home/prop");
Assertions.assertEquals("/maven/home/prop", DaemonParameters.systemProperty(Environment.MVND_HOME).asString());
assertEquals("/maven/home/prop", DaemonParameters.systemProperty(Environment.MVND_HOME).asString());
}
}
@@ -37,7 +63,7 @@ public class EnvironmentTest {
void env() {
try (EnvironmentResource env = new EnvironmentResource()) {
env.env("MVND_HOME", "/maven/home/env");
Assertions.assertEquals("/maven/home/env", DaemonParameters.environmentVariable(Environment.MVND_HOME).asString());
assertEquals("/maven/home/env", DaemonParameters.environmentVariable(Environment.MVND_HOME).asString());
}
}
@@ -46,7 +72,7 @@ public class EnvironmentTest {
try (EnvironmentResource env = new EnvironmentResource()) {
final Properties localProps = new Properties();
localProps.put("mvnd.home", "/maven/home/local");
Assertions.assertEquals(Paths.get("/maven/home/local"),
assertEquals(Paths.get("/maven/home/local"),
DaemonParameters
.environmentVariable(Environment.MVND_HOME)
.orSystemProperty()
@@ -61,7 +87,7 @@ public class EnvironmentTest {
try (EnvironmentResource env = new EnvironmentResource()) {
env.props("mvnd.home", "/maven/home/prop");
env.env("MVND_HOME", "/maven/home/env");
Assertions.assertEquals("/maven/home/env",
assertEquals("/maven/home/env",
DaemonParameters
.environmentVariable(Environment.MVND_HOME)
.orSystemProperty()
@@ -73,7 +99,7 @@ public class EnvironmentTest {
void fail() {
try (EnvironmentResource env = new EnvironmentResource()) {
try {
Assertions.assertEquals("/maven/home/env",
assertEquals("/maven/home/env",
DaemonParameters
.environmentVariable(Environment.MVND_HOME)
.orSystemProperty()
@@ -81,7 +107,7 @@ public class EnvironmentTest {
.asString());
Assertions.fail("IllegalStateException expected");
} catch (IllegalStateException e) {
Assertions.assertEquals(
assertEquals(
"Could not get value for Environment.MVND_HOME from any of the following sources: environment variable MVND_HOME, system property mvnd.home",
e.getMessage());
}
@@ -90,7 +116,7 @@ public class EnvironmentTest {
@Test
void cygwin() {
Assertions.assertEquals("C:\\jdk-11.0.2\\", Environment.cygpath("/cygdrive/c/jdk-11.0.2/"));
assertEquals("C:\\jdk-11.0.2\\", Environment.cygpath("/cygdrive/c/jdk-11.0.2/"));
}
static class EnvironmentResource implements AutoCloseable {

View File

@@ -92,6 +92,14 @@ public enum Environment {
MAVEN_LOG_FILE(null, null, null, OptionType.PATH, Flags.INTERNAL, "-l", "--log-file"),
/** Batch mode */
MAVEN_BATCH_MODE(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-B", "--batch-mode"),
/** Debug */
MAVEN_DEBUG(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-X", "--debug"),
/** Version */
MAVEN_VERSION(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-v", "-version", "--version"),
/** Show version */
MAVEN_SHOW_VERSION(null, null, null, OptionType.BOOLEAN, Flags.INTERNAL, "-V", "--show-version"),
/** Define */
MAVEN_DEFINE(null, null, null, OptionType.STRING, Flags.INTERNAL, "-D", "--define"),
//
// mvnd properties
@@ -342,21 +350,22 @@ public enum Environment {
for (Iterator<String> it = args.iterator(); it.hasNext();) {
String arg = it.next();
if (Stream.of(prefixes).anyMatch(arg::startsWith)) {
it.remove();
if (type == OptionType.VOID) {
value = "";
it.remove();
} else {
int idx = arg.indexOf('=');
if (idx >= 0) {
value = arg.substring(idx + 1);
it.remove();
} else {
value = "";
it.remove();
String opt = Stream.of(prefixes).filter(arg::startsWith)
.max(Comparator.comparing(String::length)).get();
value = arg.substring(opt.length());
if (value.isEmpty()) {
if (it.hasNext()) {
value = it.next();
it.remove();
}
} else {
if (value.charAt(0) == '=') {
value = value.substring(1);
}
}
}
}