Skip to content

Commit

Permalink
OS.detectPlatform can now take a Function<List<String>, String> (#26
Browse files Browse the repository at this point in the history
)
  • Loading branch information
nedtwigg authored Dec 8, 2023
2 parents c1dfcfb + d2fccc7 commit e4122df
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# DurianSwt releases

## [Unreleased]
### Added
- `OS.detectPlatform` can now take a `Function<List<String>, 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
Expand Down
34 changes: 20 additions & 14 deletions durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -112,12 +113,28 @@ public static OS getRunning() {

/** Eagerly detects the native and running JVM properties. */
public static void detectPlatform(Function<String, String> systemProperty, Function<String, String> environmentVariable) {
detectPlatform(systemProperty, environmentVariable, OS::exec);
}

/** Eagerly detects the native and running JVM properties. */
public static void detectPlatform(Function<String, String> systemProperty, Function<String, String> environmentVariable, Function<List<String>, 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<String> 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);
Expand All @@ -127,13 +144,13 @@ private static void detectPlatform() {
private static OS NATIVE_OS;

/** Calculates the native OS. */
private static OS calculateNative(Function<String, String> systemProperty, Function<String, String> environmentVariable) {
private static OS calculateNative(Function<String, String> systemProperty, Function<String, String> environmentVariable, Function<List<String>, 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;
Expand All @@ -154,17 +171,6 @@ private static OS calculateNative(Function<String, String> 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;
Expand Down

0 comments on commit e4122df

Please sign in to comment.