-
-
Notifications
You must be signed in to change notification settings - Fork 120
/
NodePlugin.kt
155 lines (142 loc) · 6.05 KB
/
NodePlugin.kt
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
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.pnpm.task.PnpmInstallTask
import com.github.gradle.node.pnpm.task.PnpmSetupTask
import com.github.gradle.node.pnpm.task.PnpmTask
import com.github.gradle.node.task.NodeSetupTask
import com.github.gradle.node.task.NodeTask
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 org.gradle.util.GradleVersion
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)
project.extensions.create<PackageJsonExtension>(PackageJsonExtension.NAME, project)
addGlobalTypes()
addTasks()
addNpmRule()
addPnpmRule()
addYarnRule()
project.afterEvaluate {
if (nodeExtension.download.get()) {
nodeExtension.distBaseUrl.orNull?.let { addRepository(it, nodeExtension.allowInsecureProtocol.orNull) }
configureNodeSetupTask(nodeExtension)
}
}
}
private fun addGlobalTypes() {
addGlobalType<NodeTask>()
addGlobalType<NpmTask>()
addGlobalType<NpxTask>()
addGlobalType<PnpmTask>()
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<PnpmInstallTask>(PnpmInstallTask.NAME)
project.tasks.register<YarnInstallTask>(YarnInstallTask.NAME)
project.tasks.register<NodeSetupTask>(NodeSetupTask.NAME)
project.tasks.register<NpmSetupTask>(NpmSetupTask.NAME)
project.tasks.register<PnpmSetupTask>(PnpmSetupTask.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 addPnpmRule() { // note this rule also makes it possible to specify e.g. "dependsOn npm_install"
project.tasks.addRule("Pattern: \"pnpm_<command>\": Executes an PNPM command.") {
val taskName = this
if (taskName.startsWith("pnpm_")) {
project.tasks.register<PnpmTask>(taskName) {
val tokens = taskName.split("_").drop(1) // all except first
pnpmCommand.set(tokens)
if (tokens.first().equals("run", ignoreCase = true)) {
dependsOn(PnpmInstallTask.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, allowInsecureProtocol: Boolean?) {
project.repositories.ivy {
name = "Node.js"
setUrl(distUrl)
patternLayout {
artifact("v[revision]/[artifact](-v[revision]-[classifier]).[ext]")
}
metadataSources {
artifact()
}
content {
includeModule("org.nodejs", "node")
}
if (GradleVersion.current() >= GradleVersion.version("6.0")) {
allowInsecureProtocol?.let { isAllowInsecureProtocol = it }
}
}
}
private fun configureNodeSetupTask(nodeExtension: NodeExtension) {
project.tasks.named<NodeSetupTask>(NodeSetupTask.NAME) {
val nodeArchiveDependencyProvider = variantComputer.computeNodeArchiveDependency(nodeExtension)
val archiveFileProvider = nodeArchiveDependencyProvider
.map { nodeArchiveDependency ->
resolveNodeArchiveFile(nodeArchiveDependency)
}
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 PNPM_GROUP = "pnpm"
const val YARN_GROUP = "Yarn"
}
}