Skip to content

Commit

Permalink
Remove Project usage during execution for configuration cache #111
Browse files Browse the repository at this point in the history
  • Loading branch information
deepy committed Nov 28, 2020
1 parent ef44a69 commit 9a08c18
Show file tree
Hide file tree
Showing 17 changed files with 289 additions and 97 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ tasks.test {
useJUnitPlatform()
if (project.hasProperty("skipIT")) {
exclude("**/*_integTest*")
} else if (project.hasProperty("onlyIT")) {
include("**/*_integTest*")
}
if (project.hasProperty("testAllSupportedGradleVersions")) {
systemProperty("testAllSupportedGradleVersions", "true")
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-6.6.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8-rc-1-all.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
12 changes: 6 additions & 6 deletions src/main/kotlin/com/github/gradle/node/exec/ExecRunner.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.github.gradle.node.exec

import com.github.gradle.node.NodeExtension
import org.gradle.api.Project
import com.github.gradle.node.util.ProjectApiHelper
import org.gradle.api.file.DirectoryProperty
import java.io.File

internal class ExecRunner {
fun execute(project: Project, execConfiguration: ExecConfiguration) {
fun execute(project: ProjectApiHelper, extension: NodeExtension, execConfiguration: ExecConfiguration) {
project.exec {
executable = execConfiguration.executable
args = execConfiguration.args
environment = computeEnvironment(execConfiguration)
isIgnoreExitValue = execConfiguration.ignoreExitValue
workingDir = computeWorkingDir(project, execConfiguration)
workingDir = computeWorkingDir(extension.nodeProjectDir, execConfiguration)
execConfiguration.execOverrides?.execute(this)
}
}
Expand All @@ -32,9 +33,8 @@ internal class ExecRunner {
return execEnvironment
}

private fun computeWorkingDir(project: Project, execConfiguration: ExecConfiguration): File? {
val nodeExtension = NodeExtension[project]
val workingDir = execConfiguration.workingDir ?: nodeExtension.nodeProjectDir.get().asFile
private fun computeWorkingDir(nodeProjectDir: DirectoryProperty, execConfiguration: ExecConfiguration): File? {
val workingDir = execConfiguration.workingDir ?: nodeProjectDir.get().asFile
workingDir.mkdirs()
return workingDir
}
Expand Down
11 changes: 5 additions & 6 deletions src/main/kotlin/com/github/gradle/node/exec/NodeExecRunner.kt
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
package com.github.gradle.node.exec

import com.github.gradle.node.NodeExtension
import com.github.gradle.node.util.ProjectApiHelper
import com.github.gradle.node.util.zip
import com.github.gradle.node.variant.VariantComputer
import org.gradle.api.Project
import org.gradle.api.file.Directory
import org.gradle.api.provider.Provider

internal class NodeExecRunner {
fun execute(project: Project, nodeExecConfiguration: NodeExecConfiguration) {
val execConfiguration = buildExecConfiguration(project, nodeExecConfiguration).get()
fun execute(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration) {
val execConfiguration = buildExecConfiguration(extension, nodeExecConfiguration).get()
val execRunner = ExecRunner()
execRunner.execute(project, execConfiguration)
execRunner.execute(project, extension, execConfiguration)
}

private fun buildExecConfiguration(project: Project, nodeExecConfiguration: NodeExecConfiguration):
private fun buildExecConfiguration(nodeExtension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration):
Provider<ExecConfiguration> {
val nodeExtension = NodeExtension[project]
val variantComputer = VariantComputer()
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
Expand Down
36 changes: 19 additions & 17 deletions src/main/kotlin/com/github/gradle/node/npm/exec/NpmExecRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,24 @@ import com.github.gradle.node.exec.ExecRunner
import com.github.gradle.node.exec.NodeExecConfiguration
import com.github.gradle.node.npm.proxy.NpmProxy.Companion.computeNpmProxyEnvironmentVariables
import com.github.gradle.node.npm.proxy.NpmProxy.Companion.hasProxyConfiguration
import com.github.gradle.node.util.ProjectApiHelper
import com.github.gradle.node.util.zip
import com.github.gradle.node.variant.VariantComputer
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import java.io.File
import javax.inject.Inject

internal abstract class NpmExecRunner {
@get:Inject
abstract val providers: ProviderFactory

internal class NpmExecRunner {
private val variantComputer = VariantComputer()

fun executeNpmCommand(project: Project, nodeExecConfiguration: NodeExecConfiguration) {
fun executeNpmCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration) {
val npmExecConfiguration = NpmExecConfiguration("npm"
) { variantComputer, nodeExtension, npmBinDir -> variantComputer.computeNpmExec(nodeExtension, npmBinDir) }
val nodeExtension = NodeExtension[project]
executeCommand(project, addProxyEnvironmentVariables(nodeExtension, nodeExecConfiguration),
executeCommand(project, extension, addProxyEnvironmentVariables(extension, nodeExecConfiguration),
npmExecConfiguration)
}

Expand All @@ -37,27 +41,25 @@ internal class NpmExecRunner {
return nodeExecConfiguration
}

fun executeNpxCommand(project: Project, nodeExecConfiguration: NodeExecConfiguration) {
fun executeNpxCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration) {
val npxExecConfiguration = NpmExecConfiguration("npx") { variantComputer, nodeExtension, npmBinDir ->
variantComputer.computeNpxExec(nodeExtension, npmBinDir)
}
executeCommand(project, nodeExecConfiguration, npxExecConfiguration)
executeCommand(project, extension, nodeExecConfiguration, npxExecConfiguration)
}

private fun executeCommand(project: Project, nodeExecConfiguration: NodeExecConfiguration,
private fun executeCommand(project: ProjectApiHelper, extension: NodeExtension, nodeExecConfiguration: NodeExecConfiguration,
npmExecConfiguration: NpmExecConfiguration) {
val execConfiguration =
computeExecConfiguration(project, npmExecConfiguration, nodeExecConfiguration).get()
computeExecConfiguration(extension, npmExecConfiguration, nodeExecConfiguration).get()
val execRunner = ExecRunner()
execRunner.execute(project, execConfiguration)
execRunner.execute(project, extension, execConfiguration)
}

private fun computeExecConfiguration(project: Project, npmExecConfiguration: NpmExecConfiguration,
private fun computeExecConfiguration(extension: NodeExtension, npmExecConfiguration: NpmExecConfiguration,
nodeExecConfiguration: NodeExecConfiguration): Provider<ExecConfiguration> {
val nodeExtension = NodeExtension[project]
val additionalBinPathProvider = computeAdditionalBinPath(project, nodeExtension)
val executableAndScriptProvider =
computeExecutable(nodeExtension, npmExecConfiguration)
val additionalBinPathProvider = computeAdditionalBinPath(extension)
val executableAndScriptProvider = computeExecutable(extension, npmExecConfiguration)
return zip(additionalBinPathProvider, executableAndScriptProvider)
.map { (additionalBinPath, executableAndScript) ->
val argsPrefix =
Expand Down Expand Up @@ -102,10 +104,10 @@ internal class NpmExecRunner {
val script: String? = null
)

private fun computeAdditionalBinPath(project: Project, nodeExtension: NodeExtension): Provider<List<String>> {
private fun computeAdditionalBinPath(nodeExtension: NodeExtension): Provider<List<String>> {
return nodeExtension.download.flatMap { download ->
if (!download) {
project.providers.provider { listOf<String>() }
providers.provider { listOf<String>() }
}
val nodeDirProvider = variantComputer.computeNodeDir(nodeExtension)
val nodeBinDirProvider = variantComputer.computeNodeBinDir(nodeDirProvider)
Expand Down
23 changes: 11 additions & 12 deletions src/main/kotlin/com/github/gradle/node/npm/task/NpmInstallTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import java.io.File
/**
* npm install that only gets executed if gradle decides so.
*/
open class NpmInstallTask : NpmTask() {
private val nodeExtension by lazy { NodeExtension[project] }

abstract class NpmInstallTask : NpmTask() {
@get:Internal
val nodeModulesOutputFilter =
project.objects.property<Action<ConfigurableFileTree>>()
objects.property<Action<ConfigurableFileTree>>()

init {
group = NodePlugin.NPM_GROUP
Expand All @@ -48,21 +46,21 @@ open class NpmInstallTask : NpmTask() {
@InputFile
protected fun getPackageLockFileAsInput(): Provider<File> {
return npmCommand.flatMap { command ->
if (command[0] == "ci") projectFileIfExists("package-lock.json") else project.provider { null }
if (command[0] == "ci") projectFileIfExists("package-lock.json") else providers.provider { null }
}
}

@Optional
@OutputFile
protected fun getPackageLockFileAsOutput(): Provider<File> {
return npmCommand.flatMap { command ->
if (command[0] == "install") projectFileIfExists("package-lock.json") else project.provider { null }
if (command[0] == "install") projectFileIfExists("package-lock.json") else providers.provider { null }
}
}

private fun projectFileIfExists(name: String): Provider<File> {
return nodeExtension.nodeProjectDir.map { it.file(name).asFile }
.flatMap { if (it.exists()) project.providers.provider { it } else project.providers.provider { null } }
.flatMap { if (it.exists()) providers.provider { it } else providers.provider { null } }
}

@Optional
Expand All @@ -71,7 +69,7 @@ open class NpmInstallTask : NpmTask() {
protected fun getNodeModulesDirectory(): Provider<Directory> {
val filter = nodeModulesOutputFilter.orNull
return if (filter == null) nodeExtension.nodeProjectDir.dir("node_modules")
else project.providers.provider { null }
else providers.provider { null }
}

@Optional
Expand All @@ -82,10 +80,11 @@ open class NpmInstallTask : NpmTask() {
return zip(nodeModulesDirectoryProvider, nodeModulesOutputFilter)
.flatMap { (nodeModulesDirectory, nodeModulesOutputFilter) ->
if (nodeModulesOutputFilter != null) {
val fileTree = project.fileTree(nodeModulesDirectory)
nodeModulesOutputFilter.execute(fileTree)
project.providers.provider { fileTree }
} else project.providers.provider { null }
val fileTree = projectHelper.fileTree(nodeModulesDirectory)
//TODO: have a closer look at this.
nodeModulesOutputFilter.execute(fileTree!!)
providers.provider { fileTree }
} else providers.provider { null }
}
}

Expand Down
24 changes: 19 additions & 5 deletions src/main/kotlin/com/github/gradle/node/npm/task/NpmSetupTask.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,38 @@ import com.github.gradle.node.NodePlugin
import com.github.gradle.node.exec.NodeExecConfiguration
import com.github.gradle.node.npm.exec.NpmExecRunner
import com.github.gradle.node.task.NodeSetupTask
import com.github.gradle.node.util.ProjectApiHelper
import com.github.gradle.node.variant.VariantComputer
import org.gradle.api.DefaultTask
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Provider
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.listProperty
import javax.inject.Inject

/**
* npm install that only gets executed if gradle decides so.
*/
open class NpmSetupTask : DefaultTask() {
abstract class NpmSetupTask : DefaultTask() {

@get:Inject
abstract val objects: ObjectFactory

@get:Inject
abstract val providers: ProviderFactory

@get:Internal
protected val nodeExtension = NodeExtension[project]

@get:Internal
protected val nodeExtension by lazy { NodeExtension[project] }
val projectHelper = ProjectApiHelper.newInstance(project)

@get:Input
val args = project.objects.listProperty<String>()
val args = objects.listProperty<String>()

@get:Input
val download by lazy { nodeExtension.download }
Expand Down Expand Up @@ -57,8 +71,8 @@ open class NpmSetupTask : DefaultTask() {
fun exec() {
val command = computeCommand()
val nodeExecConfiguration = NodeExecConfiguration(command)
val npmExecRunner = NpmExecRunner()
npmExecRunner.executeNpmCommand(project, nodeExecConfiguration)
val npmExecRunner = objects.newInstance(NpmExecRunner::class.java)
npmExecRunner.executeNpmCommand(projectHelper, nodeExtension, nodeExecConfiguration)
}

protected open fun computeCommand(): List<String> {
Expand Down
35 changes: 26 additions & 9 deletions src/main/kotlin/com/github/gradle/node/npm/task/NpmTask.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package com.github.gradle.node.npm.task

import com.github.gradle.node.NodeExtension
import com.github.gradle.node.NodePlugin
import com.github.gradle.node.exec.NodeExecConfiguration
import com.github.gradle.node.npm.exec.NpmExecRunner
import com.github.gradle.node.util.ProjectApiHelper
import org.gradle.api.Action
import org.gradle.api.DefaultTask
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ProviderFactory
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Optional
Expand All @@ -13,27 +17,40 @@ import org.gradle.kotlin.dsl.listProperty
import org.gradle.kotlin.dsl.mapProperty
import org.gradle.kotlin.dsl.property
import org.gradle.process.ExecSpec
import javax.inject.Inject

abstract class NpmTask : DefaultTask() {
@get:Inject
abstract val objects: ObjectFactory

@get:Inject
abstract val providers: ProviderFactory

open class NpmTask : DefaultTask() {
@get:Optional
@get:Input
val npmCommand = project.objects.listProperty<String>()
val npmCommand = objects.listProperty<String>()

@get:Optional
@get:Input
val args = project.objects.listProperty<String>()
val args = objects.listProperty<String>()

@get:Input
val ignoreExitValue = project.objects.property<Boolean>().convention(false)
val ignoreExitValue = objects.property<Boolean>().convention(false)

@get:Internal
val workingDir = project.objects.fileProperty()
val workingDir = objects.fileProperty()

@get:Input
val environment = project.objects.mapProperty<String, String>()
val environment = objects.mapProperty<String, String>()

@get:Internal
val execOverrides = objects.property<Action<ExecSpec>>()

@get:Internal
val projectHelper = ProjectApiHelper.newInstance(project)

@get:Internal
val execOverrides = project.objects.property<Action<ExecSpec>>()
val nodeExtension = NodeExtension[project]

init {
group = NodePlugin.NPM_GROUP
Expand All @@ -52,7 +69,7 @@ open class NpmTask : DefaultTask() {
val nodeExecConfiguration =
NodeExecConfiguration(command, environment.get(), workingDir.asFile.orNull, ignoreExitValue.get(),
execOverrides.orNull)
val npmExecRunner = NpmExecRunner()
npmExecRunner.executeNpmCommand(project, nodeExecConfiguration)
val npmExecRunner = objects.newInstance(NpmExecRunner::class.java)
npmExecRunner.executeNpmCommand(projectHelper, nodeExtension, nodeExecConfiguration)
}
}
Loading

0 comments on commit 9a08c18

Please sign in to comment.