Skip to content

Commit fd76156

Browse files
authored
Make it easy for people to use with Gradle's configuration cache. (#15)
2 parents c0fa165 + 9476569 commit fd76156

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# DurianSwt releases
22

33
## [Unreleased]
4+
### Added
5+
- Added `OS.detectPlatform(Function<String, String> systemProperty, Function<String, String> environmentVariable)`
46

57
## [3.4.1] - 2021-08-21
68
### Fixed

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@
2323
import java.nio.charset.StandardCharsets;
2424
import java.util.Arrays;
2525
import java.util.Locale;
26+
import java.util.function.Function;
2627

2728
/** Enum representing an OS and its underlying CPU architecture. */
2829
public enum OS {
@@ -96,29 +97,45 @@ public String toSwt() {
9697

9798
/** Returns the native OS: 32-bit JVM on 64-bit Windows returns OS.WIN_64. */
9899
public static OS getNative() {
100+
detectPlatform();
99101
return NATIVE_OS;
100102
}
101103

102104
/** Returns the running OS: 32-bit JVM on 64-bit Windows returns OS.WIN_32. */
103105
public static OS getRunning() {
106+
detectPlatform();
104107
return RUNNING_OS;
105108
}
106109

107-
private static final OS NATIVE_OS = calculateNative();
110+
/** Eagerly detects the native and running JVM properties. */
111+
public static void detectPlatform(Function<String, String> systemProperty, Function<String, String> environmentVariable) {
112+
if (NATIVE_OS == null) {
113+
NATIVE_OS = calculateNative(systemProperty, environmentVariable);
114+
RUNNING_OS = calculateRunning(systemProperty);
115+
}
116+
}
117+
118+
private static void detectPlatform() {
119+
if (NATIVE_OS == null) {
120+
detectPlatform(System::getProperty, System::getenv);
121+
}
122+
}
123+
124+
private static OS NATIVE_OS;
108125

109126
/** Calculates the native OS. */
110-
private static OS calculateNative() {
111-
String os_name = System.getProperty("os.name").toLowerCase(Locale.getDefault());
127+
private static OS calculateNative(Function<String, String> systemProperty, Function<String, String> environmentVariable) {
128+
String os_name = systemProperty.apply("os.name").toLowerCase(Locale.getDefault());
112129
boolean isWin = os_name.contains("win");
113130
boolean isMac = os_name.contains("mac");
114131
boolean isLinux = Arrays.asList("nix", "nux", "aix").stream().anyMatch(os_name::contains);
115132
if (isMac) {
116133
return exec("uname", "-a").contains("_ARM64_") ? MAC_silicon : MAC_x64;
117134
} else if (isWin) {
118-
boolean is64bit = System.getenv("ProgramFiles(x86)") != null;
135+
boolean is64bit = environmentVariable.apply("ProgramFiles(x86)") != null;
119136
return is64bit ? WIN_x64 : WIN_x86;
120137
} else if (isLinux) {
121-
String os_arch = System.getProperty("os.arch");
138+
String os_arch = systemProperty.apply("os.arch");
122139
switch (os_arch) {
123140
case "i386":
124141
case "x86":
@@ -153,11 +170,11 @@ private static void drain(InputStream input, OutputStream output) throws IOExcep
153170
}
154171
}
155172

156-
private static final OS RUNNING_OS = calculateRunning();
173+
private static OS RUNNING_OS;
157174

158175
/** Calculates the running OS. */
159-
private static OS calculateRunning() {
160-
Arch runningArch = runningJvm();
176+
private static OS calculateRunning(Function<String, String> systemProperty) {
177+
Arch runningArch = runningJvm(systemProperty);
161178
OS runningOs = NATIVE_OS.winMacLinux(
162179
runningArch.x86x64arm64(OS.WIN_x86, OS.WIN_x64, null),
163180
runningArch.x86x64arm64(null, OS.MAC_x64, OS.MAC_silicon),
@@ -169,13 +186,13 @@ private static OS calculateRunning() {
169186
}
170187

171188
/** Returns the arch of the currently running JVM. */
172-
private static Arch runningJvm() {
173-
String sunArchDataModel = System.getProperty("sun.arch.data.model");
189+
private static Arch runningJvm(Function<String, String> systemProperty) {
190+
String sunArchDataModel = systemProperty.apply("sun.arch.data.model");
174191
switch (sunArchDataModel) {
175192
case "32":
176193
return Arch.x86;
177194
case "64":
178-
return "aarch64".equals(System.getProperty("os.arch")) ? Arch.arm64 : Arch.x64;
195+
return "aarch64".equals(systemProperty.apply("os.arch")) ? Arch.arm64 : Arch.x64;
179196
default:
180197
throw new IllegalArgumentException("Expcted 32 or 64, was " + sunArchDataModel);
181198
}

0 commit comments

Comments
 (0)