Skip to content

Commit ccead81

Browse files
committed
OS.detectPlatform can now take a Function<List<String>, String> which lets you control process execution when detecting the OS.
1 parent c1dfcfb commit ccead81

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# DurianSwt releases
22

33
## [Unreleased]
4+
### Added
5+
- `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))
46

57
## [4.2.2] - 2023-10-26
68
### Fixed

durian-swt.os/src/main/java/com/diffplug/common/swt/os/OS.java

+20-14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.OutputStream;
2222
import java.nio.charset.StandardCharsets;
2323
import java.util.Arrays;
24+
import java.util.List;
2425
import java.util.Locale;
2526
import java.util.function.Function;
2627

@@ -112,12 +113,28 @@ public static OS getRunning() {
112113

113114
/** Eagerly detects the native and running JVM properties. */
114115
public static void detectPlatform(Function<String, String> systemProperty, Function<String, String> environmentVariable) {
116+
detectPlatform(systemProperty, environmentVariable, OS::exec);
117+
}
118+
119+
/** Eagerly detects the native and running JVM properties. */
120+
public static void detectPlatform(Function<String, String> systemProperty, Function<String, String> environmentVariable, Function<List<String>, String> executeCommand) {
115121
if (NATIVE_OS == null) {
116-
NATIVE_OS = calculateNative(systemProperty, environmentVariable);
122+
NATIVE_OS = calculateNative(systemProperty, environmentVariable, executeCommand);
117123
RUNNING_OS = calculateRunning(systemProperty);
118124
}
119125
}
120126

127+
private static String exec(List<String> cmd) {
128+
try {
129+
Process process = Runtime.getRuntime().exec(cmd.toArray(new String[0]));
130+
ByteArrayOutputStream output = new ByteArrayOutputStream();
131+
drain(process.getInputStream(), output);
132+
return new String(output.toByteArray(), StandardCharsets.UTF_8);
133+
} catch (IOException e) {
134+
throw new RuntimeException(e);
135+
}
136+
}
137+
121138
private static void detectPlatform() {
122139
if (NATIVE_OS == null) {
123140
detectPlatform(System::getProperty, System::getenv);
@@ -127,13 +144,13 @@ private static void detectPlatform() {
127144
private static OS NATIVE_OS;
128145

129146
/** Calculates the native OS. */
130-
private static OS calculateNative(Function<String, String> systemProperty, Function<String, String> environmentVariable) {
147+
private static OS calculateNative(Function<String, String> systemProperty, Function<String, String> environmentVariable, Function<List<String>, String> executeCommand) {
131148
String os_name = systemProperty.apply("os.name").toLowerCase(Locale.getDefault());
132149
boolean isWin = os_name.contains("win");
133150
boolean isMac = os_name.contains("mac");
134151
boolean isLinux = Arrays.asList("nix", "nux", "aix").stream().anyMatch(os_name::contains);
135152
if (isMac) {
136-
return exec("uname", "-a").contains("_ARM64_") ? MAC_silicon : MAC_x64;
153+
return executeCommand.apply(List.of("uname", "-a")).contains("_ARM64_") ? MAC_silicon : MAC_x64;
137154
} else if (isWin) {
138155
boolean is64bit = environmentVariable.apply("ProgramFiles(x86)") != null;
139156
return is64bit ? WIN_x64 : WIN_x86;
@@ -154,17 +171,6 @@ private static OS calculateNative(Function<String, String> systemProperty, Funct
154171
}
155172
}
156173

157-
private static String exec(String... cmd) {
158-
try {
159-
Process process = Runtime.getRuntime().exec(cmd);
160-
ByteArrayOutputStream output = new ByteArrayOutputStream();
161-
drain(process.getInputStream(), output);
162-
return new String(output.toByteArray(), StandardCharsets.UTF_8);
163-
} catch (IOException e) {
164-
throw new RuntimeException(e);
165-
}
166-
}
167-
168174
private static void drain(InputStream input, OutputStream output) throws IOException {
169175
byte[] buf = new byte[1024];
170176
int numRead;

0 commit comments

Comments
 (0)