diff --git a/CHANGES.md b/CHANGES.md index ed332cf0..0c461cf8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,6 +1,8 @@ # DurianSwt releases ## [Unreleased] +### Added +- `OS.detectPlatform` can now take a `Function, String>` which lets you control process execution when detecting the OS. ([#26](https://github.com/diffplug/durian-swt/pull/26)) ## [4.2.2] - 2023-10-26 ### Fixed diff --git a/durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java b/durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java index 7507bb45..fbb26c94 100644 --- a/durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java +++ b/durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java @@ -21,6 +21,7 @@ import java.io.OutputStream; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.List; import java.util.Locale; import java.util.function.Function; @@ -112,12 +113,28 @@ public static OS getRunning() { /** Eagerly detects the native and running JVM properties. */ public static void detectPlatform(Function systemProperty, Function environmentVariable) { + detectPlatform(systemProperty, environmentVariable, OS::exec); + } + + /** Eagerly detects the native and running JVM properties. */ + public static void detectPlatform(Function systemProperty, Function environmentVariable, Function, String> executeCommand) { if (NATIVE_OS == null) { - NATIVE_OS = calculateNative(systemProperty, environmentVariable); + NATIVE_OS = calculateNative(systemProperty, environmentVariable, executeCommand); RUNNING_OS = calculateRunning(systemProperty); } } + private static String exec(List cmd) { + try { + Process process = Runtime.getRuntime().exec(cmd.toArray(new String[0])); + ByteArrayOutputStream output = new ByteArrayOutputStream(); + drain(process.getInputStream(), output); + return new String(output.toByteArray(), StandardCharsets.UTF_8); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + private static void detectPlatform() { if (NATIVE_OS == null) { detectPlatform(System::getProperty, System::getenv); @@ -127,13 +144,13 @@ private static void detectPlatform() { private static OS NATIVE_OS; /** Calculates the native OS. */ - private static OS calculateNative(Function systemProperty, Function environmentVariable) { + private static OS calculateNative(Function systemProperty, Function environmentVariable, Function, String> executeCommand) { String os_name = systemProperty.apply("os.name").toLowerCase(Locale.getDefault()); boolean isWin = os_name.contains("win"); boolean isMac = os_name.contains("mac"); boolean isLinux = Arrays.asList("nix", "nux", "aix").stream().anyMatch(os_name::contains); if (isMac) { - return exec("uname", "-a").contains("_ARM64_") ? MAC_silicon : MAC_x64; + return executeCommand.apply(Arrays.asList("uname", "-a")).contains("_ARM64_") ? MAC_silicon : MAC_x64; } else if (isWin) { boolean is64bit = environmentVariable.apply("ProgramFiles(x86)") != null; return is64bit ? WIN_x64 : WIN_x86; @@ -154,17 +171,6 @@ private static OS calculateNative(Function systemProperty, Funct } } - private static String exec(String... cmd) { - try { - Process process = Runtime.getRuntime().exec(cmd); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - drain(process.getInputStream(), output); - return new String(output.toByteArray(), StandardCharsets.UTF_8); - } catch (IOException e) { - throw new RuntimeException(e); - } - } - private static void drain(InputStream input, OutputStream output) throws IOException { byte[] buf = new byte[1024]; int numRead;