Skip to content

Commit

Permalink
wire up node executable between tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
dconeybe committed Nov 20, 2024
1 parent 0c41373 commit 8ef8480
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,42 @@ import com.google.firebase.example.dataconnect.gradle.tasks.GenerateDataConnectS
import com.google.firebase.example.dataconnect.gradle.tasks.SetupFirebaseToolsTask
import com.google.firebase.example.dataconnect.gradle.tasks.configureFrom
import java.util.Locale
import org.gradle.api.DefaultTask
import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.tasks.InputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.getByType
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.register

abstract class FooTask : DefaultTask() {

@get:InputFile
abstract val nodeExecutable: RegularFileProperty

@TaskAction
fun run() {
throw Exception("in task $name, nodeExecutable=${nodeExecutable.get().asFile}")
}
}

@Suppress("unused")
abstract class DataConnectGradlePlugin : Plugin<Project> {

override fun apply(project: Project) {
project.extensions.create("dataconnect", DataConnectExtension::class.java)
val providers = project.objects.newInstance<MyProjectProviders>()

project.tasks.register<DownloadNodeJsTask>("downloadNodeJs") {
val x = project.tasks.register<DownloadNodeJsTask>("downloadNodeJs") {
configureFrom(providers)
}

project.tasks.register<FooTask>("foo") {
nodeExecutable.set(x.flatMap { it.nodeExecutable })
}

project.tasks.register<SetupFirebaseToolsTask>("setupFirebaseToolsForDataConnect") {
configureFrom(providers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ import org.gradle.api.DefaultTask
import org.gradle.api.GradleException
import org.gradle.api.Task
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.RegularFile
import org.gradle.api.provider.Property
import org.gradle.api.provider.Provider
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.Internal
import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.property
import org.pgpainless.sop.SOPImpl

abstract class DownloadNodeJsTask : DefaultTask() {
Expand All @@ -63,10 +66,18 @@ abstract class DownloadNodeJsTask : DefaultTask() {
@get:OutputDirectory
abstract val outputDirectory: DirectoryProperty

@get:Internal
val nodeExecutable: Provider<RegularFile> by lazy {
outputDirectory.map { it.file(nodeExecutablePathUnderOutputDirectory.get()) }
}

private val nodeExecutablePathUnderOutputDirectory: Property<String> = project.objects.property()

@TaskAction
fun run() {
val source = source.get()
val outputDirectory = outputDirectory.get().asFile
val outputDirectoryRegularFile = outputDirectory.get()
val outputDirectory = outputDirectoryRegularFile.asFile

logger.info("source: {}", Source.describe(source))
logger.info("outputDirectory: {}", outputDirectory.absolutePath)
Expand All @@ -76,6 +87,21 @@ abstract class DownloadNodeJsTask : DefaultTask() {
when (source) {
is DownloadOfficialVersion -> downloadOfficialVersion(source, outputDirectory)
}

val nodeExecutableFiles = outputDirectory.walk().filter {
it.isFile && it.name == "node"
}.toList()
val nodeExecutableFile = nodeExecutableFiles.singleOrNull() ?: throw GradleException(
"Found ${nodeExecutableFiles.size} node executable files " +
"in ${outputDirectory.absolutePath}, but expected exactly 1: " +
nodeExecutableFiles.joinToString(", ") { it.absolutePath } +
"(error code v6n2g6my3y)"
)

nodeExecutablePathUnderOutputDirectory.apply {
set(outputDirectory.toPath().relativize(nodeExecutableFile.toPath()).toString())
finalizeValue()
}
}

sealed interface Source : java.io.Serializable {
Expand Down

0 comments on commit 8ef8480

Please sign in to comment.