Skip to content

Commit 0bda51f

Browse files
committed
Improve platform inference in shell initialisation.
1 parent 54d2ab8 commit 0bda51f

15 files changed

+155
-55
lines changed

src/main/bash/sdkman-init.sh

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,67 @@ if [ -z "$SDKMAN_DIR" ]; then
3030
fi
3131

3232
# infer platform
33-
SDKMAN_PLATFORM="$(uname)"
34-
if [[ "$SDKMAN_PLATFORM" == 'Linux' ]]; then
35-
if [[ "$(uname -m)" == 'i686' ]]; then
36-
SDKMAN_PLATFORM+='32'
37-
elif [[ "$(uname -m)" == 'aarch64' ]]; then
38-
SDKMAN_PLATFORM+='ARM64'
39-
else
40-
SDKMAN_PLATFORM+='64'
41-
fi
42-
fi
33+
34+
function infer_platform() {
35+
local kernel
36+
local machine
37+
38+
kernel="$(uname -s)"
39+
machine="$(uname -m)"
40+
41+
case $kernel in
42+
Linux)
43+
case $machine in
44+
i686)
45+
echo "LinuxX32"
46+
;;
47+
X86_64)
48+
echo "LinuxX64"
49+
;;
50+
armv7l)
51+
echo "LinuxARM32"
52+
;;
53+
armv8l)
54+
echo "LinuxARM64"
55+
;;
56+
aarch64)
57+
echo "LinuxARM64"
58+
;;
59+
*)
60+
echo "LinuxX64"
61+
;;
62+
esac
63+
;;
64+
Darwin)
65+
case $machine in
66+
X86_64)
67+
echo "DarwinX64"
68+
;;
69+
arm64)
70+
echo "DarwinARM64"
71+
;;
72+
*)
73+
echo "DarwinX64"
74+
;;
75+
esac
76+
;;
77+
*)
78+
echo "$kernel"
79+
esac
80+
}
81+
82+
SDKMAN_PLATFORM="$(infer_platform)"
4383
export SDKMAN_PLATFORM
4484

85+
__sdkman_echo_debug "Inferred platform: $SDKMAN_PLATFORM"
86+
4587
# OS specific support (must be 'true' or 'false').
4688
cygwin=false
4789
darwin=false
4890
solaris=false
4991
freebsd=false
50-
case "${SDKMAN_PLATFORM}" in
92+
SDKMAN_KERNEL="$(uname -s)"
93+
case "${SDKMAN_KERNEL}" in
5194
CYGWIN*)
5295
cygwin=true
5396
;;

src/test/groovy/sdkman/env/SdkmanBashEnvBuilder.groovy

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sdkman.env
22

33
import groovy.transform.ToString
44
import sdkman.stubs.CurlStub
5+
import sdkman.stubs.UnameStub
56

67
@ToString(includeNames = true)
78
class SdkmanBashEnvBuilder {
@@ -13,6 +14,7 @@ class SdkmanBashEnvBuilder {
1314

1415
//optional fields with sensible defaults
1516
private Optional<CurlStub> curlStub = Optional.empty()
17+
private Optional<UnameStub> unameStub = Optional.empty()
1618
private List candidates = ['groovy', 'grails', 'java']
1719
private boolean offlineMode = false
1820
private String broadcast = "This is a LIVE broadcast!"
@@ -43,6 +45,11 @@ class SdkmanBashEnvBuilder {
4345
this
4446
}
4547

48+
SdkmanBashEnvBuilder withUnameStub(UnameStub unameStub) {
49+
this.unameStub = Optional.of(unameStub)
50+
this
51+
}
52+
4653
SdkmanBashEnvBuilder withCandidates(List candidates) {
4754
this.candidates = candidates
4855
this
@@ -104,7 +111,8 @@ class SdkmanBashEnvBuilder {
104111
sdkmanTmpDir = prepareDirectory(sdkmanDir, "tmp")
105112
sdkmanCandidatesDir = prepareDirectory(sdkmanDir, "candidates")
106113

107-
curlStub.map { cs -> cs.build() }
114+
curlStub.map { it.build() }
115+
unameStub.map { it.build() }
108116

109117
initializeCandidates(sdkmanCandidatesDir, candidates)
110118
initializeCandidatesCache(sdkmanVarDir, candidates)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package sdkman.specs
2+
3+
import sdkman.support.SdkmanEnvSpecification
4+
5+
class PlatformSpec extends SdkmanEnvSpecification {
6+
def setup() {
7+
sdkmanBashEnvBuilder.withCandidates(["groovy"])
8+
}
9+
10+
def "should set platform based on uname"() {
11+
given:
12+
unameStub.forKernel(kernel).forMachine(machine)
13+
bash = sdkmanBashEnvBuilder.withUnameStub(unameStub).build()
14+
bash.start()
15+
bash.execute("source $bootstrapScript")
16+
bash.execute('echo $SDKMAN_PLATFORM')
17+
18+
expect:
19+
bash.output.contains(platform)
20+
21+
where:
22+
kernel | machine | platform
23+
"Linux" | "i686" | "LinuxX32"
24+
"Linux" | "X86_64" | "LinuxX64"
25+
"Linux" | "armv7l" | "LinuxARM32"
26+
"Linux" | "armv8l" | "LinuxARM64"
27+
"Linux" | "aarch64" | "LinuxARM64"
28+
"Linux" | "" | "LinuxX64"
29+
"Darwin" | "X86_64" | "DarwinX64"
30+
"Darwin" | "arm64" | "DarwinARM64"
31+
"Darwin" | "" | "DarwinX64"
32+
"MSYS64" | "i686" | "MSYS64"
33+
"MSYS64" | "" | "MSYS64"
34+
}
35+
}

src/test/groovy/sdkman/steps/initialisation_steps.groovy

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ And(~'^offline mode is enabled with unreachable internet$') { ->
7878
javaHome = FAKE_JDK_PATH
7979
}
8080

81-
And(~'^a machine with "(.*)" installed$') { String platform ->
81+
And(~'^an "(.*)" machine with "(.*)" installed$') { String machine, String kernel ->
8282
def binFolder = "$sdkmanBaseDir/bin" as File
8383
UnameStub.prepareIn(binFolder)
84-
.forPlatform(asSdkmanPlatform(platform))
84+
.forKernel(kernel)
85+
.forMachine(machine)
8586
.build()
8687
}
8788

src/test/groovy/sdkman/stubs/UnameStub.groovy

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ package sdkman.stubs
33
class UnameStub {
44

55
private File file
6-
private uname
6+
private platform = "Linux"
7+
private kernel = "Linux"
8+
private machine = "X86_64"
79

810
static UnameStub prepareIn(File folder) {
911
folder.mkdirs()
@@ -16,12 +18,30 @@ class UnameStub {
1618
new UnameStub(file: file)
1719
}
1820

21+
UnameStub forKernel(String kernel) {
22+
this.kernel = kernel
23+
this
24+
}
25+
26+
UnameStub forMachine(String machine) {
27+
this.machine = machine
28+
this
29+
}
30+
1931
UnameStub forPlatform(String uname) {
20-
this.uname = uname
32+
this.platform = uname
2133
this
2234
}
2335

2436
void build() {
25-
file << "echo $uname"
37+
file << """
38+
|if [[ "\$1" == '-m' ]]; then
39+
| echo "$machine"
40+
|elif [[ "\$1" == '-s' ]]; then
41+
| echo "$kernel"
42+
|else
43+
| echo "$platform"
44+
|fi
45+
""".stripMargin('|')
2646
}
2747
}

src/test/groovy/sdkman/support/SdkmanEnvSpecification.groovy

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sdkman.support
22

33
import sdkman.env.SdkmanBashEnvBuilder
44
import sdkman.stubs.CurlStub
5+
import sdkman.stubs.UnameStub
56

67
import static sdkman.support.FilesystemUtils.prepareBaseDir
78

@@ -10,6 +11,7 @@ abstract class SdkmanEnvSpecification extends BashEnvSpecification {
1011
SdkmanBashEnvBuilder sdkmanBashEnvBuilder
1112

1213
CurlStub curlStub
14+
UnameStub unameStub
1315

1416
File sdkmanBaseDirectory
1517
File sdkmanDotDirectory
@@ -20,6 +22,7 @@ abstract class SdkmanEnvSpecification extends BashEnvSpecification {
2022
def setup() {
2123
sdkmanBaseDirectory = prepareBaseDir()
2224
curlStub = CurlStub.prepareIn(new File(sdkmanBaseDirectory, "bin"))
25+
unameStub = UnameStub.prepareIn(new File(sdkmanBaseDirectory, "bin"))
2326
sdkmanBashEnvBuilder = SdkmanBashEnvBuilder
2427
.create(sdkmanBaseDirectory)
2528
.withCurlStub(curlStub)

src/test/groovy/sdkman/support/UnixUtils.groovy

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,27 @@ package sdkman.support
22

33
class UnixUtils {
44

5+
private static platforms = [
6+
"Linux": [
7+
"amd64": "LinuxX64"
8+
],
9+
"Darwin": [
10+
"X86_64": "DarwinX64",
11+
],
12+
"FreeBSD": [
13+
"amd64": "FreeBSD_X64"
14+
]
15+
]
16+
517
static getPlatform() {
6-
asSdkmanPlatform(System.getProperty("os.name"), System.getProperty("os.arch"))
18+
def sdkmanPlatform = asSdkmanPlatform(System.getProperty("os.name"), System.getProperty("os.arch"))
19+
println("Setting platform for test: $sdkmanPlatform")
20+
sdkmanPlatform
721
}
8-
9-
static asSdkmanPlatform(platform, architecture = "") {
10-
11-
def platformArch = architecture == "aarch64" ? "$platform $architecture" : platform
12-
13-
def result
14-
switch (platformArch) {
15-
case "Mac OS X":
16-
result = "Darwin"
17-
break
18-
case "Linux":
19-
result = "Linux64"
20-
break
21-
case "Linux 64":
22-
result = "Linux64"
23-
break
24-
case "Linux 32":
25-
result = "Linux32"
26-
break
27-
case "Linux aarch64":
28-
result = "LinuxARM64"
29-
break
30-
case "FreeBSD":
31-
result = "FreeBSD"
32-
break
33-
default:
34-
result = platform
35-
}
36-
result
22+
23+
static asSdkmanPlatform(
24+
platform = System.getProperty("os.name"),
25+
architecture = System.getProperty("os.arch")) {
26+
platforms[platform][architecture] ?: platform
3727
}
3828
}

0 commit comments

Comments
 (0)