-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathNodeExtension.kt
More file actions
262 lines (224 loc) · 9.59 KB
/
NodeExtension.kt
File metadata and controls
262 lines (224 loc) · 9.59 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.github.gradle.node
import com.github.gradle.node.npm.exec.NpmExecResult
import com.github.gradle.node.npm.exec.NpmExecSource
import com.github.gradle.node.npm.exec.NpmExecSpec
import com.github.gradle.node.npm.proxy.ProxySettings
import com.github.gradle.node.util.Platform
import com.github.gradle.node.variant.VariantComputer
import com.github.gradle.node.variant.computeNodeExec
import org.gradle.api.Project
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.kotlin.dsl.create
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.of
import org.gradle.kotlin.dsl.property
import javax.inject.Inject
abstract class NodeExtension
@Inject
internal constructor(
project: Project,
private val providers: ProviderFactory,
) {
private val cacheDir = project.layout.projectDirectory.dir(".gradle")
/**
* The directory where Node.js is unpacked (when download is true)
*/
val workDir = project.objects.directoryProperty().convention(cacheDir.dir("nodejs"))
/**
* The directory where npm is installed (when a specific version is defined)
*/
val npmWorkDir: DirectoryProperty = project.objects.directoryProperty().convention(cacheDir.dir("npm"))
/**
* The directory where pnpm is installed (when a pnpm task is used)
*/
val pnpmWorkDir = project.objects.directoryProperty().convention(cacheDir.dir("pnpm"))
/**
* The directory where yarn is installed (when a Yarn task is used)
*/
val yarnWorkDir = project.objects.directoryProperty().convention(cacheDir.dir("yarn"))
/**
* The directory where Bun is installed (when a Bun task is used)
*/
val bunWorkDir = project.objects.directoryProperty().convention(cacheDir.dir("bun"))
/**
* The Node.js project directory location
* This is where the package.json file and node_modules directory are located
* By default it is at the root of the current project
*/
val nodeProjectDir = project.objects.directoryProperty().convention(project.layout.projectDirectory)
/**
* Version of node to download and install (only used if download is true)
* It will be unpacked in the workDir
*/
val version = project.objects.property<String>().convention(DEFAULT_NODE_VERSION)
/**
* Version of npm to use
* If specified, installs it in the npmWorkDir
* If empty, the plugin will use the npm command bundled with Node.js
*/
val npmVersion: Property<String> = project.objects.property<String>().convention("")
/**
* Version of pnpm to use
* Any pnpm task first installs pnpm in the pnpmWorkDir
* It uses the specified version if defined and the latest version otherwise (by default)
*/
val pnpmVersion = project.objects.property<String>().convention("")
/**
* Version of Yarn to use
* Any Yarn task first installs Yarn in the yarnWorkDir
* It uses the specified version if defined and the latest version otherwise (by default)
*/
val yarnVersion = project.objects.property<String>().convention("")
/**
* Version of Bun to use
* Any Bun task first installs Bun in the bunWorkDir
* It uses the specified version if defined and the latest version otherwise (by default)
*/
val bunVersion = project.objects.property<String>().convention("")
/**
* Base URL for fetching node distributions
* Only used if download is true
* Change it if you want to use a mirror
* Or set to null if you want to add the repository on your own.
*/
val distBaseUrl = project.objects.property<String>()
/**
* Specifies whether it is acceptable to communicate with the Node.js repository over an insecure HTTP connection.
* Only used if download is true
* Change it to true if you use a mirror that uses HTTP rather than HTTPS
* Or set to null if you want to use Gradle's default behaviour.
*/
val allowInsecureProtocol = project.objects.property<Boolean>()
val npmCommand = project.objects.property<String>().convention("npm")
val npxCommand = project.objects.property<String>().convention("npx")
val pnpmCommand = project.objects.property<String>().convention("pnpm")
val yarnCommand = project.objects.property<String>().convention("yarn")
val bunCommand = project.objects.property<String>().convention("bun")
val bunxCommand = project.objects.property<String>().convention("bunx")
/**
* The npm command executed by the npmInstall task
* By default it is install but it can be changed to ci
*/
val npmInstallCommand = project.objects.property<String>().convention("install")
/**
* Whether to download and install a specific Node.js version or not
* If false, it will use the globally installed Node.js
* If true, it will download node using above parameters
* Note that npm is bundled with Node.js
*/
val download: Property<Boolean> = project.objects.property<Boolean>().convention(false)
/**
* Whether the plugin automatically should add the proxy configuration to npm and yarn commands
* according the proxy configuration defined for Gradle
*
* Disable this option if you want to configure the proxy for npm or yarn on your own
* (in the .npmrc file for instance)
*
*/
val nodeProxySettings = project.objects.property<ProxySettings>().convention(ProxySettings.SMART)
/**
* Use fast NpmInstall logic, excluding node_modules for output tracking resulting in a significantly faster
* npm install/ci configuration at the cost of slightly decreased correctness in certain circumstances.
*
* In practice this means that if you change node_modules through other means than npm install/ci
* NpmInstall tasks will continue being up-to-date, but if you're modifying node_modules through
* other tools you may have other correctness problems and surfacing them here may be preferred.
*
* https://docs.npmjs.com/cli/v8/configuring-npm/package-lock-json#hidden-lockfiles
*
* Requires npm 7 or later
* This will become the default in 4.x
*/
val fastNpmInstall = project.objects.property<Boolean>().convention(false)
/**
* Disable functionality that requires newer versions of npm
*
* If you're not downloading Node.js and using old version of Node or npm
* set this to true to disable functionality that makes use of newer functionality.
*
* This will be removed in 4.x
*/
val oldNpm = project.objects.property<Boolean>().convention(false)
/**
* Create rules for automatic task creation
*
* Disabling this will prevent the npm_ npx_ yarn_ pnpm_ tasks from being
* automatically created.
* It's recommended to turn this off after you've gotten comfortable
* with the plugin and register your own tasks instead of relying on the rule.
*/
val enableTaskRules = project.objects.property<Boolean>().convention(true)
/**
* Computed path to nodejs directory
*/
@Deprecated(message = "replaced with resolvedNodeDir", replaceWith = ReplaceWith("resolvedNodeDir"))
val computedNodeDir = project.objects.directoryProperty()
/**
* Computed path to nodejs directory
*/
val resolvedNodeDir: DirectoryProperty = project.objects.directoryProperty()
/**
* Operating system and architecture
*/
@Deprecated(message = "replaced with resolvedPlatform", replaceWith = ReplaceWith("resolvedPlatform"))
val computedPlatform = project.objects.property<Platform>()
/**
* Operating system and architecture
*/
val resolvedPlatform: Property<Platform> = project.objects.property<Platform>()
init {
distBaseUrl.set("https://nodejs.org/dist")
}
@Deprecated(
"useGradleProxySettings has been replaced with nodeProxySettings",
replaceWith = ReplaceWith("nodeProxySettings.set(i)")
)
fun setUseGradleProxySettings(value: Boolean) {
nodeProxySettings.set(if (value) ProxySettings.SMART else ProxySettings.OFF)
}
@Suppress("UnstableApiUsage")
fun npmExec(
configuration: NpmExecSpec.() -> Unit
): Provider<NpmExecResult> {
val vc = VariantComputer()
val nodeDirProvider = resolvedNodeDir
val npmDirProvider = vc.computeNpmDir(this, nodeDirProvider)
val nodeBinDirProvider = vc.computeNodeBinDir(nodeDirProvider, resolvedPlatform)
val npmBinDirProvider = vc.computeNpmBinDir(npmDirProvider, resolvedPlatform)
val nodeExecProvider = computeNodeExec(this, nodeBinDirProvider)
vc.computeNpmExec(this, npmBinDirProvider)
return providers.of(NpmExecSource::class) {
parameters.apply(configuration)
// parameters {
// executable
// ignoreExitValue
// workingDir
// }
}
}
companion object {
/**
* Extension name in Gradle
*/
const val NAME = "node"
/**
* Default version of Node to download if none is set
*/
const val DEFAULT_NODE_VERSION = "18.17.1"
/**
* Default version of npm to download if none is set
*/
const val DEFAULT_NPM_VERSION = "9.6.7"
@JvmStatic
operator fun get(project: Project): NodeExtension {
return project.extensions.getByType()
}
@JvmStatic
fun create(project: Project): NodeExtension {
return project.extensions.create<NodeExtension>(NAME, project)
}
}
}