Merge pull request #173 from gnodet/cygwin

Cygwin support, fixes #156
This commit is contained in:
Guillaume Nodet
2020-10-29 11:52:12 +01:00
committed by GitHub
3 changed files with 36 additions and 3 deletions

View File

@@ -20,6 +20,7 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collection; import java.util.Collection;
import java.util.Locale;
import java.util.Map; import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Properties; import java.util.Properties;
@@ -139,7 +140,7 @@ public enum Environment {
return MVND_PROPERTIES_PATH return MVND_PROPERTIES_PATH
.environmentVariable() .environmentVariable()
.orSystemProperty() .orSystemProperty()
.orDefault(() -> System.getProperty("user.home") + "/.m2/mvnd.properties") .orDefault(() -> Paths.get(System.getProperty("user.home"), ".m2", "mvnd.properties").toString())
.asPath() .asPath()
.toAbsolutePath().normalize(); .toAbsolutePath().normalize();
} }
@@ -325,10 +326,22 @@ public enum Environment {
} }
public Path asPath() { public Path asPath() {
final String result = get(); String result = get();
if (result != null && Os.current().isCygwin()) {
result = cygpath(result);
}
return result == null ? null : Paths.get(result); return result == null ? null : Paths.get(result);
} }
static String cygpath(String result) {
String path = result.replace('/', '\\');
if (path.matches("\\\\cygdrive\\\\[a-z]\\\\.*")) {
String s = path.substring("\\cygdrive\\".length());
result = s.substring(0, 1).toUpperCase(Locale.ENGLISH) + ":" + s.substring(1);
}
return result;
}
public boolean asBoolean() { public boolean asBoolean() {
return Boolean.parseBoolean(get()); return Boolean.parseBoolean(get());
} }

View File

@@ -20,7 +20,18 @@ import java.util.Locale;
public enum Os { public enum Os {
LINUX(true), LINUX(true),
MAC(true), MAC(true),
WINDOWS(false), WINDOWS(false) {
private boolean cygwin;
{
String pwd = System.getenv("PWD");
cygwin = pwd != null && pwd.startsWith("/");
}
@Override
public boolean isCygwin() {
return cygwin;
}
},
UNKNOWN(false) { UNKNOWN(false) {
@Override @Override
@@ -58,4 +69,8 @@ public enum Os {
return unixLike; return unixLike;
} }
public boolean isCygwin() {
return false;
}
} }

View File

@@ -87,6 +87,11 @@ public class EnvironmentTest {
} }
} }
@Test
void cygwin() {
Assertions.assertEquals("C:\\jdk-11.0.2\\", Environment.EnvValue.cygpath("/cygdrive/c/jdk-11.0.2/"));
}
static class EnvironmentResource implements AutoCloseable { static class EnvironmentResource implements AutoCloseable {
private final Properties props = new Properties(); private final Properties props = new Properties();