-
-
Notifications
You must be signed in to change notification settings - Fork 122
Expand file tree
/
Copy pathNodePlugin.kt
More file actions
133 lines (121 loc) · 5 KB
/
Copy pathNodePlugin.kt
File metadata and controls
133 lines (121 loc) · 5 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.github.gradle.node
import com.github.gradle.node.npm.proxy.ProxySettings
import com.github.gradle.node.npm.task.NpmInstallTask
import com.github.gradle.node.npm.task.NpmSetupTask
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
import com.github.gradle.node.yarn.task.YarnTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.named
import org.gradle.kotlin.dsl.register
import java.io.File
class NodePlugin : Plugin<Project> {
private lateinit var project: Project
override fun apply(project: Project) {
this.project = project
val nodeExtension = NodeExtension.create(project)
addGlobalTypes()
addTasks()
addNpmRule()
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)
}
}
}
private fun addGlobalTypes() {
addGlobalType<NodeTask>()
addGlobalType<NpmTask>()
addGlobalType<NpxTask>()
addGlobalType<YarnTask>()
addGlobalType<ProxySettings>()
}
private inline fun <reified T> addGlobalType() {
project.extensions.extraProperties[T::class.java.simpleName] = T::class.java
}
private fun addTasks() {
project.tasks.register<NpmInstallTask>(NpmInstallTask.NAME)
project.tasks.register<YarnInstallTask>(YarnInstallTask.NAME)
project.tasks.register<NodeSetupTask>(NodeSetupTask.NAME)
project.tasks.register<NpmSetupTask>(NpmSetupTask.NAME)
project.tasks.register<YarnSetupTask>(YarnSetupTask.NAME)
}
private fun addNpmRule() { // note this rule also makes it possible to specify e.g. "dependsOn npm_install"
project.tasks.addRule("Pattern: \"npm_<command>\": Executes an NPM command.") {
val taskName = this
if (taskName.startsWith("npm_")) {
project.tasks.create<NpmTask>(taskName) {
val tokens = taskName.split("_").drop(1) // all except first
npmCommand.set(tokens)
if (tokens.first().equals("run", ignoreCase = true)) {
dependsOn(NpmInstallTask.NAME)
}
}
}
}
}
private fun addYarnRule() { // note this rule also makes it possible to specify e.g. "dependsOn yarn_install"
project.tasks.addRule("Pattern: \"yarn_<command>\": Executes an Yarn command.") {
val taskName = this
if (taskName.startsWith("yarn_")) {
project.tasks.create<YarnTask>(taskName) {
val tokens = taskName.split("_").drop(1) // all except first
yarnCommand.set(tokens)
if (tokens.first().equals("run", ignoreCase = true)) {
dependsOn(YarnInstallTask.NAME)
}
}
}
}
}
private fun addRepository(distUrl: String) {
project.repositories.ivy {
name = "Node.js"
setUrl(distUrl)
patternLayout {
artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]")
}
metadataSources {
artifact()
}
content {
includeModule("org.nodejs", "node")
}
}
}
private fun configureNodeSetupTask(nodeExtension: NodeExtension) {
val variantComputer = VariantComputer()
val nodeArchiveDependencyProvider = variantComputer.computeNodeArchiveDependency(nodeExtension)
val archiveFileProvider = nodeArchiveDependencyProvider
.map { nodeArchiveDependency ->
resolveNodeArchiveFile(nodeArchiveDependency)
}
project.tasks.named<NodeSetupTask>(NodeSetupTask.NAME) {
nodeArchiveFile.set(project.layout.file(archiveFileProvider))
}
}
private fun resolveNodeArchiveFile(name: String): File {
val dependency = project.dependencies.create(name)
val configuration = project.configurations.detachedConfiguration(dependency)
configuration.isTransitive = false
return configuration.resolve().single()
}
companion object {
const val NODE_GROUP = "Node"
const val NPM_GROUP = "npm"
const val YARN_GROUP = "Yarn"
}
}