Skip to content

Commit febf536

Browse files
committed
Move unsupported platform failure from os.name to separate method.
Previously we've accidentally depended on the isWindows check to see if we're on a supported platform or not, this had the unfortunate side-effect that even with `download = false` we triggered the error(). See #178
1 parent 4d083a7 commit febf536

File tree

3 files changed

+32
-13
lines changed

3 files changed

+32
-13
lines changed

src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt

+5
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,16 @@ abstract class NodeSetupTask : DefaultTask() {
4949

5050
@TaskAction
5151
fun exec() {
52+
failIfUnsupportedPlatform()
5253
deleteExistingNode()
5354
unpackNodeArchive()
5455
setExecutableFlag()
5556
}
5657

58+
private fun failIfUnsupportedPlatform() {
59+
PlatformHelper.INSTANCE.failOnUnsupportedOs()
60+
}
61+
5762
private fun deleteExistingNode() {
5863
projectHelper.delete {
5964
delete(nodeDir.get().dir("../"))

src/main/kotlin/com/github/gradle/node/util/PlatformHelper.kt

+14-2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ open class PlatformHelper constructor(private val props: Properties = System.get
99
name.contains("windows") -> "win"
1010
name.contains("mac") -> "darwin"
1111
name.contains("linux") -> "linux"
12-
name.contains("freebsd") -> "linux"
1312
name.contains("sunos") -> "sunos"
14-
else -> error("Unsupported OS: $name")
13+
else -> "unsupported"
1514
}
1615
}
1716

@@ -32,6 +31,14 @@ open class PlatformHelper constructor(private val props: Properties = System.get
3231

3332
open val isWindows: Boolean by lazy { osName == "win" }
3433

34+
open val isSupported: Boolean by lazy { osName != "unsupported" }
35+
36+
fun failOnUnsupportedOs() {
37+
if (!isSupported) {
38+
error("Unsupported OS")
39+
}
40+
}
41+
3542
private fun property(name: String): String {
3643
val value = props.getProperty(name)
3744
return value ?: System.getProperty(name) ?:
@@ -48,6 +55,11 @@ open class PlatformHelper constructor(private val props: Properties = System.get
4855
fun main(args: Array<String>) {
4956
println("Your os.name is: '${System.getProperty("os.name")}' and is parsed as: ${PlatformHelper.INSTANCE.osName}")
5057
println("Your os.arch is: '${System.getProperty("os.arch")}' and is parsed as: ${PlatformHelper.INSTANCE.osArch}")
58+
if (!PlatformHelper.INSTANCE.isSupported) {
59+
println("Your platform is \"unsupported\" (isSupported == false)")
60+
println("Your platform does not support 'download = true' as there's no official Node.js binaries" +
61+
" being published for it. You can still use the plugin, but you need to install Node.js manually")
62+
}
5163
if (PlatformHelper.INSTANCE.isWindows) {
5264
println("You're on windows (isWindows == true)")
5365
} else {

src/test/groovy/com/github/gradle/node/util/PlatformHelperTest.groovy

+13-11
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,20 @@ class PlatformHelperTest extends Specification {
2222
this.helper.getOsName() == osName
2323
this.helper.getOsArch() == osArch
2424
this.helper.isWindows() == isWindows
25+
this.helper.isSupported() == isSupported
2526

2627
where:
27-
osProp | archProp | osName | osArch | isWindows
28-
'Windows 8' | 'x86' | 'win' | 'x86' | true
29-
'Windows 8' | 'x86_64' | 'win' | 'x64' | true
30-
'Mac OS X' | 'x86' | 'darwin' | 'x86' | false
31-
'Mac OS X' | 'x86_64' | 'darwin' | 'x64' | false
32-
'Linux' | 'x86' | 'linux' | 'x86' | false
33-
'Linux' | 'x86_64' | 'linux' | 'x64' | false
34-
'Linux' | 'ppc64le' | 'linux' | 'ppc64le' | false
35-
'SunOS' | 'x86' | 'sunos' | 'x86' | false
36-
'SunOS' | 'x86_64' | 'sunos' | 'x64' | false
28+
osProp | archProp | osName | osArch | isWindows | isSupported
29+
'Windows 8' | 'x86' | 'win' | 'x86' | true | true
30+
'Windows 8' | 'x86_64' | 'win' | 'x64' | true | true
31+
'Mac OS X' | 'x86' | 'darwin' | 'x86' | false | true
32+
'Mac OS X' | 'x86_64' | 'darwin' | 'x64' | false | true
33+
'Linux' | 'x86' | 'linux' | 'x86' | false | true
34+
'Linux' | 'x86_64' | 'linux' | 'x64' | false | true
35+
'Linux' | 'ppc64le' | 'linux' | 'ppc64le' | false | true
36+
'SunOS' | 'x86' | 'sunos' | 'x86' | false | true
37+
'SunOS' | 'x86_64' | 'sunos' | 'x64' | false | true
38+
'FreeBSD' | 'amd64' | 'unsupported' | 'x64' | false | false
3739
}
3840

3941
@Unroll
@@ -62,7 +64,7 @@ class PlatformHelperTest extends Specification {
6264
this.props.setProperty("os.name", 'Nonsense')
6365

6466
when:
65-
this.helper.getOsName()
67+
this.helper.failOnUnsupportedOs()
6668

6769
then:
6870
thrown(IllegalStateException)

0 commit comments

Comments
 (0)