Skip to content

WIP: Move unsupported platform failure from os.name to separate method. #179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/kotlin/com/github/gradle/node/NodePlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.github.gradle.node.npm.task.NpmTask
import com.github.gradle.node.npm.task.NpxTask
import com.github.gradle.node.task.NodeSetupTask
import com.github.gradle.node.task.NodeTask
import com.github.gradle.node.util.PlatformHelper
import com.github.gradle.node.variant.VariantComputer
import com.github.gradle.node.yarn.task.YarnInstallTask
import com.github.gradle.node.yarn.task.YarnSetupTask
Expand All @@ -30,6 +31,9 @@ class NodePlugin : Plugin<Project> {
addYarnRule()
project.afterEvaluate {
if (nodeExtension.download.get()) {
// Ideally we wouldn't have to do this here, but if we don't
// then we're going to have some interesting failures down the line
PlatformHelper.INSTANCE.failOnUnsupportedOs()
nodeExtension.distBaseUrl.orNull?.let { addRepository(it) }
configureNodeSetupTask(nodeExtension)
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/kotlin/com/github/gradle/node/task/NodeSetupTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,16 @@ abstract class NodeSetupTask : DefaultTask() {

@TaskAction
fun exec() {
failIfUnsupportedPlatform()
deleteExistingNode()
unpackNodeArchive()
setExecutableFlag()
}

private fun failIfUnsupportedPlatform() {
PlatformHelper.INSTANCE.failOnUnsupportedOs()
}

private fun deleteExistingNode() {
projectHelper.delete {
delete(nodeDir.get().dir("../"))
Expand Down
16 changes: 14 additions & 2 deletions src/main/kotlin/com/github/gradle/node/util/PlatformHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ open class PlatformHelper constructor(private val props: Properties = System.get
name.contains("windows") -> "win"
name.contains("mac") -> "darwin"
name.contains("linux") -> "linux"
name.contains("freebsd") -> "linux"
name.contains("sunos") -> "sunos"
else -> error("Unsupported OS: $name")
else -> "unsupported"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like this since if we're extending this or someone uses their own distribution URL which does have binaries it's going to fail, and we're providing no way to override it.

}
}

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

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

open val isSupported: Boolean by lazy { osName != "unsupported" }

fun failOnUnsupportedOs() {
if (!isSupported) {
error("Unsupported OS for `download = true`")
}
}

private fun property(name: String): String {
val value = props.getProperty(name)
return value ?: System.getProperty(name) ?:
Expand All @@ -48,6 +55,11 @@ open class PlatformHelper constructor(private val props: Properties = System.get
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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ class PlatformHelperTest extends Specification {
this.helper.getOsName() == osName
this.helper.getOsArch() == osArch
this.helper.isWindows() == isWindows
this.helper.isSupported() == isSupported

where:
osProp | archProp | osName | osArch | isWindows
'Windows 8' | 'x86' | 'win' | 'x86' | true
'Windows 8' | 'x86_64' | 'win' | 'x64' | true
'Mac OS X' | 'x86' | 'darwin' | 'x86' | false
'Mac OS X' | 'x86_64' | 'darwin' | 'x64' | false
'Linux' | 'x86' | 'linux' | 'x86' | false
'Linux' | 'x86_64' | 'linux' | 'x64' | false
'Linux' | 'ppc64le' | 'linux' | 'ppc64le' | false
'SunOS' | 'x86' | 'sunos' | 'x86' | false
'SunOS' | 'x86_64' | 'sunos' | 'x64' | false
osProp | archProp | osName | osArch | isWindows | isSupported
'Windows 8' | 'x86' | 'win' | 'x86' | true | true
'Windows 8' | 'x86_64' | 'win' | 'x64' | true | true
'Mac OS X' | 'x86' | 'darwin' | 'x86' | false | true
'Mac OS X' | 'x86_64' | 'darwin' | 'x64' | false | true
'Linux' | 'x86' | 'linux' | 'x86' | false | true
'Linux' | 'x86_64' | 'linux' | 'x64' | false | true
'Linux' | 'ppc64le' | 'linux' | 'ppc64le' | false | true
'SunOS' | 'x86' | 'sunos' | 'x86' | false | true
'SunOS' | 'x86_64' | 'sunos' | 'x64' | false | true
'FreeBSD' | 'amd64' | 'unsupported' | 'x64' | false | false
}

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

when:
this.helper.getOsName()
this.helper.failOnUnsupportedOs()

then:
thrown(IllegalStateException)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ class VariantComputerTest extends Specification {
'Linux' | 'ppc64le' | 'node-v5.12.0-linux-ppc64le' | 'org.nodejs:node:5.12.0:[email protected]'
'Mac OS X' | 'x86' | 'node-v5.12.0-darwin-x86' | 'org.nodejs:node:5.12.0:[email protected]'
'Mac OS X' | 'x86_64' | 'node-v5.12.0-darwin-x64' | 'org.nodejs:node:5.12.0:[email protected]'
'FreeBSD' | 'x86' | 'node-v5.12.0-linux-x86' | 'org.nodejs:node:5.12.0:[email protected]'
'FreeBSD' | 'x86_64' | 'node-v5.12.0-linux-x64' | 'org.nodejs:node:5.12.0:[email protected]'
'SunOS' | 'x86' | 'node-v5.12.0-sunos-x86' | 'org.nodejs:node:5.12.0:[email protected]'
'SunOS' | 'x86_64' | 'node-v5.12.0-sunos-x64' | 'org.nodejs:node:5.12.0:[email protected]'
}
Expand Down