Skip to content

Commit cfced59

Browse files
authored
Add Arch.unknown (#22 fixes #20)
2 parents 76f6f37 + 9315a06 commit cfced59

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
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+
- When `OS.getNative` or `getRunning` is called on an OS which is unsupported by SWT, instead of throwing an exception, we now return `Arch.unknown`. You can't get platform-specific artifacts for it, but if you don't need them then you get to keep running instead of dying. ([#21](https://github.com/diffplug/durian-swt/pull/21))
46

57
## [4.1.1] - 2022-10-31
68
### Fixed

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

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2023 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.
@@ -17,7 +17,7 @@
1717

1818
/** Enum for handling different processor architectures supported by SWT. */
1919
public enum Arch {
20-
x86, x64, arm64;
20+
x86, x64, arm64, unknown;
2121

2222
/** Returns the appropriate value depending on the arch. */
2323
public <T> T x86x64(T val86, T val64) {
@@ -57,6 +57,22 @@ public <T> T x86x64arm64(T val86, T val64, T arm64) {
5757
}
5858
}
5959

60+
/** Returns the appropriate value depending on the arch. */
61+
public <T> T x86x64arm64unknown(T val86, T val64, T arm64, T unknown) {
62+
switch (this) {
63+
case x86:
64+
return val86;
65+
case x64:
66+
return val64;
67+
case arm64:
68+
return arm64;
69+
case unknown:
70+
return unknown;
71+
default:
72+
throw unsupportedException(this);
73+
}
74+
}
75+
6076
/** Returns the Arch for the native platform: 32-bit JVM on 64-bit Windows returns Arch.x64. */
6177
public static Arch getNative() {
6278
return OS.getNative().getArch();

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

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.common.swt.os;
1717

18-
1918
import java.io.ByteArrayOutputStream;
2019
import java.io.IOException;
2120
import java.io.InputStream;
@@ -27,18 +26,18 @@
2726

2827
/** Enum representing an OS and its underlying CPU architecture. */
2928
public enum OS {
30-
WIN_x64, WIN_x86, LINUX_x64, LINUX_x86, MAC_x64, MAC_silicon;
29+
WIN_x64, WIN_x86, LINUX_x64, LINUX_x86, MAC_x64, MAC_silicon, WIN_unknown, LINUX_unknown, MAC_unknown;
3130

3231
public boolean isWindows() {
33-
return this == WIN_x64 || this == WIN_x86;
32+
return this == WIN_x64 || this == WIN_x86 || this == WIN_unknown;
3433
}
3534

3635
public boolean isLinux() {
37-
return this == LINUX_x64 || this == LINUX_x86;
36+
return this == LINUX_x64 || this == LINUX_x86 || this == LINUX_unknown;
3837
}
3938

4039
public boolean isMac() {
41-
return this == MAC_x64 || this == MAC_silicon;
40+
return this == MAC_x64 || this == MAC_silicon || this == MAC_unknown;
4241
}
4342

4443
public boolean isMacOrLinux() {
@@ -70,6 +69,10 @@ public Arch getArch() {
7069
return Arch.x86;
7170
case MAC_silicon:
7271
return Arch.arm64;
72+
case WIN_unknown:
73+
case MAC_unknown:
74+
case LINUX_unknown:
75+
return Arch.unknown;
7376
default:
7477
throw unsupportedException(this);
7578
}
@@ -82,7 +85,7 @@ public String os() {
8285

8386
/** SWT-style x86/x86_64 */
8487
public String arch() {
85-
return getArch().x86x64arm64("x86", "x86_64", "aarch64");
88+
return getArch().x86x64arm64unknown("x86", "x86_64", "aarch64", "unknown");
8689
}
8790

8891
/** os().arch() */
@@ -92,7 +95,7 @@ public String osDotArch() {
9295

9396
/** windowing.os.arch */
9497
public String toSwt() {
95-
return winMacLinux("win32", "cocoa", "gtk") + "." + winMacLinux("win32", "macosx", "linux") + "." + getArch().x86x64arm64("x86", "x86_64", "aarch64");
98+
return SwtPlatform.fromOS(this).toString();
9699
}
97100

98101
/** Returns the native OS: 32-bit JVM on 64-bit Windows returns OS.WIN_64. */
@@ -144,7 +147,7 @@ private static OS calculateNative(Function<String, String> systemProperty, Funct
144147
case "amd64":
145148
return LINUX_x64;
146149
default:
147-
throw new IllegalArgumentException("Unknown os.arch " + os_arch + "'.");
150+
return LINUX_unknown;
148151
}
149152
} else {
150153
throw new IllegalArgumentException("Unknown os.name '" + os_name + "'.");
@@ -175,14 +178,10 @@ private static void drain(InputStream input, OutputStream output) throws IOExcep
175178
/** Calculates the running OS. */
176179
private static OS calculateRunning(Function<String, String> systemProperty) {
177180
Arch runningArch = runningJvm(systemProperty);
178-
OS runningOs = NATIVE_OS.winMacLinux(
179-
runningArch.x86x64arm64(OS.WIN_x86, OS.WIN_x64, null),
180-
runningArch.x86x64arm64(null, OS.MAC_x64, OS.MAC_silicon),
181-
runningArch.x86x64arm64(OS.LINUX_x86, OS.LINUX_x64, null));
182-
if (runningOs == null) {
183-
throw new IllegalArgumentException("Unsupported OS/Arch combo: " + runningOs + " " + runningArch);
184-
}
185-
return runningOs;
181+
return NATIVE_OS.winMacLinux(
182+
runningArch.x86x64arm64unknown(OS.WIN_x86, OS.WIN_x64, OS.WIN_unknown, OS.WIN_unknown),
183+
runningArch.x86x64arm64unknown(OS.MAC_unknown, OS.MAC_x64, OS.MAC_silicon, OS.MAC_unknown),
184+
runningArch.x86x64arm64unknown(OS.LINUX_x86, OS.LINUX_x64, OS.LINUX_unknown, OS.LINUX_unknown));
186185
}
187186

188187
/** Returns the arch of the currently running JVM. */
@@ -194,7 +193,7 @@ private static Arch runningJvm(Function<String, String> systemProperty) {
194193
case "64":
195194
return "aarch64".equals(systemProperty.apply("os.arch")) ? Arch.arm64 : Arch.x64;
196195
default:
197-
throw new IllegalArgumentException("Expcted 32 or 64, was " + sunArchDataModel);
196+
return Arch.unknown;
198197
}
199198
}
200199

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 DiffPlug
2+
* Copyright (C) 2020-2023 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.
@@ -15,7 +15,6 @@
1515
*/
1616
package com.diffplug.common.swt.os;
1717

18-
1918
import java.util.Arrays;
2019
import java.util.HashMap;
2120
import java.util.LinkedHashMap;
@@ -129,7 +128,7 @@ public static SwtPlatform getRunning() {
129128
public static SwtPlatform fromOS(OS raw) {
130129
String ws = raw.winMacLinux("win32", "cocoa", "gtk");
131130
String os = raw.winMacLinux("win32", "macosx", "linux");
132-
String arch = raw.getArch().x86x64arm64("x86", "x86_64", "aarch64");
131+
String arch = raw.getArch().x86x64arm64unknown("x86", "x86_64", "aarch64", "unknown");
133132
return new SwtPlatform(ws, os, arch);
134133
}
135134

0 commit comments

Comments
 (0)