-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathPlatformHelper.kt
More file actions
68 lines (59 loc) · 2.63 KB
/
PlatformHelper.kt
File metadata and controls
68 lines (59 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package com.github.gradle.node.util
import java.util.*
open class PlatformHelper constructor(private val props: Properties = System.getProperties()) {
open val osName: String by lazy {
val name = property("os.name").toLowerCase()
when {
name.contains("windows") -> "win"
name.contains("mac") -> "darwin"
name.contains("linux") -> "linux"
name.contains("sunos") -> "sunos"
else -> "unsupported"
}
}
open val osArch: String by lazy {
val arch = property("os.arch").toLowerCase()
when {
/*
* As Java just returns "arm" on all ARM variants, we need a system call to determine the exact arch. Unfortunately some JVMs say aarch32/64, so we need an additional
* conditional. Additionally, the node binaries for 'armv8l' are called 'arm64', so we need to distinguish here.
*/
arch == "arm" || arch.startsWith("aarch") -> property("uname")
.mapIf({ it == "armv8l" || it == "aarch64" }) { "arm64" }
arch == "ppc64le" -> "ppc64le"
arch.contains("64") -> "x64"
else -> "x86"
}
}
open val isWindows: Boolean by lazy { osName == "win" }
open val isSupported: Boolean by lazy { osName != "unsupported" }
fun failOnUnsupportedOs() {
if (!isSupported) {
error("Unsupported OS")
}
}
private fun property(name: String): String {
val value = props.getProperty(name)
return value ?: System.getProperty(name) ?:
// Added so that we can test osArch on Windows and on non-arm systems
if (name == "uname") execute("uname", "-m")
else error("Unable to find a value for property [$name].")
}
companion object {
var INSTANCE = PlatformHelper()
}
}
fun main(args: Array<String>) {
println("Your os.name is: '${System.getProperty("os.name")}' and is parsed as: ${PlatformHelper.INSTANCE.osName}")
println("Your os.arch is: '${System.getProperty("os.arch")}' and is parsed as: ${PlatformHelper.INSTANCE.osArch}")
if (!PlatformHelper.INSTANCE.isSupported) {
println("Your platform is \"unsupported\" (isSupported == false)")
println("Your platform does not support 'download = true' as there's no official Node.js binaries" +
" being published for it. You can still use the plugin, but you need to install Node.js manually")
}
if (PlatformHelper.INSTANCE.isWindows) {
println("You're on windows (isWindows == true)")
} else {
println("You're not on windows (isWindows == false)")
}
}