diff --git a/build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts b/build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts index 863c0e8..7b9bbdf 100644 --- a/build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts +++ b/build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts @@ -1,58 +1,5 @@ -abstract class BuildScopedGreetingService @Inject constructor( - execOperations: ExecOperations, -) : BuildService { - - companion object { - const val SERVICE_NAME = "buildScopedGreetingService" - } - - interface Params : BuildServiceParameters { - val greeting: Property - } - - val exitValue: Int = - execOperations.exec { - commandLine("echo", "Hello, ${parameters.greeting.get()}") - standardOutput = System.out - }.exitValue -} - -abstract class ProjectScopedGreetingTask @Inject constructor( - private val execOperations: ExecOperations, -) : DefaultTask() { - - init { - group = "demo" - description = "A project-scoped exec task that depends on a " + - "build-scoped exec from a shared build service." - } - - @Suppress("UnstableApiUsage") - @get:ServiceReference(BuildScopedGreetingService.SERVICE_NAME) - abstract val buildScopedGreetingService: Property - - @TaskAction - fun doTaskAction() { - - val buildScopedGreetingServiceInstance: BuildScopedGreetingService = - buildScopedGreetingService.get() - - val exitValue = buildScopedGreetingServiceInstance.exitValue - - if (exitValue != 0) - throw GradleException( - "Build service exec resulted in a non-zero exit value $exitValue!" - ) - - val greeting = - buildScopedGreetingServiceInstance.parameters.greeting.get() - - execOperations.exec { - commandLine("echo", greeting) - standardOutput = System.out - } - } -} +import org.sdkotlin.buildlogic.globalexec.BuildScopedGreetingService +import org.sdkotlin.buildlogic.globalexec.ProjectScopedGreetingTask gradle.sharedServices.registerIfAbsent( BuildScopedGreetingService.SERVICE_NAME, diff --git a/build-logic/src/main/kotlin/org/sdkotlin/buildlogic/globalexec/GlobalExec.kt b/build-logic/src/main/kotlin/org/sdkotlin/buildlogic/globalexec/GlobalExec.kt new file mode 100644 index 0000000..ed47c34 --- /dev/null +++ b/build-logic/src/main/kotlin/org/sdkotlin/buildlogic/globalexec/GlobalExec.kt @@ -0,0 +1,87 @@ +package org.sdkotlin.buildlogic.globalexec + +import org.gradle.api.DefaultTask +import org.gradle.api.GradleException +import org.gradle.api.provider.Property +import org.gradle.api.services.BuildService +import org.gradle.api.services.BuildServiceParameters +import org.gradle.api.services.ServiceReference +import org.gradle.api.tasks.TaskAction +import org.gradle.kotlin.dsl.provideDelegate +import org.gradle.process.ExecOperations +import javax.inject.Inject + +abstract class BuildScopedGreetingService : + BuildService { + interface Params : BuildServiceParameters { + val greeting: Property + } + + companion object { + const val SERVICE_NAME = "buildScopedGreetingService" + } + + @get:Inject + abstract val execOperations: ExecOperations + + val exitValue: Int by lazy { + execOperations.exec { + commandLine( + buildCurrentOsCommand( + "echo", "Hello, ${parameters.greeting.get()}" + ) + ) + standardOutput = System.out + }.exitValue + } +} + +abstract class ProjectScopedGreetingTask : DefaultTask() { + init { + group = "demo" + description = "A project-scoped exec task that depends on a " + + "build-scoped exec from a shared build service." + } + + @Suppress("UnstableApiUsage") + @get:ServiceReference(BuildScopedGreetingService.SERVICE_NAME) + abstract val buildScopedGreetingService: Property + + @get:Inject + abstract val execOperations: ExecOperations + + @TaskAction + fun doTaskAction() { + + val buildScopedGreetingServiceInstance: BuildScopedGreetingService = + buildScopedGreetingService.get() + + val exitValue = buildScopedGreetingServiceInstance.exitValue + + if (exitValue != 0) + throw GradleException( + "Build service exec resulted in a non-zero exit value $exitValue!" + ) + + val greeting = + buildScopedGreetingServiceInstance.parameters.greeting.get() + + execOperations.exec { + commandLine( + buildCurrentOsCommand("echo", greeting) + ) + standardOutput = System.out + } + } +} + +fun buildCurrentOsCommand(vararg arguments: String): List = + buildList { + val isWindows = System.getProperty("os.name") + .contains("windows", ignoreCase = true) + if (isWindows) { + add("cmd.exe") + add("/c") + } + addAll(arguments) + }