From 9956f5bc464025e251a16d81f6ad37df6f1633df Mon Sep 17 00:00:00 2001 From: Ian Brandt Date: Mon, 11 Nov 2024 10:11:38 -0800 Subject: [PATCH] Added an example of a project-scoped exec task that depends on a build-scoped exec from a shared build service. --- ...sdkotlin.buildlogic.global-exec.gradle.kts | 64 +++++++++++++++++++ .../build.gradle.kts | 1 + .../kotlin-for-java-devs/build.gradle.kts | 3 +- subprojects/tdd-in-kotlin/build.gradle.kts | 1 + 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts 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 new file mode 100644 index 0000000..c907107 --- /dev/null +++ b/build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts @@ -0,0 +1,64 @@ +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 + } + } +} + +gradle.sharedServices.registerIfAbsent( + BuildScopedGreetingService.SERVICE_NAME, + BuildScopedGreetingService::class.java +) { + parameters.greeting.set("Doctor"); +} + +tasks.register("greetings") diff --git a/subprojects/kotlin-for-java-devs-client/build.gradle.kts b/subprojects/kotlin-for-java-devs-client/build.gradle.kts index d6a14a7..0cd4814 100644 --- a/subprojects/kotlin-for-java-devs-client/build.gradle.kts +++ b/subprojects/kotlin-for-java-devs-client/build.gradle.kts @@ -1,4 +1,5 @@ plugins { + id("org.sdkotlin.buildlogic.global-exec") id("org.sdkotlin.buildlogic.kotlin-project") id("org.sdkotlin.buildlogic.test.unit-test-suite") } diff --git a/subprojects/kotlin-for-java-devs/build.gradle.kts b/subprojects/kotlin-for-java-devs/build.gradle.kts index aaf323a..859f551 100644 --- a/subprojects/kotlin-for-java-devs/build.gradle.kts +++ b/subprojects/kotlin-for-java-devs/build.gradle.kts @@ -1,6 +1,5 @@ -import org.jetbrains.kotlin.gradle.tasks.KotlinCompile - plugins { + id("org.sdkotlin.buildlogic.global-exec") id("org.sdkotlin.buildlogic.kotlin-project") id("org.sdkotlin.buildlogic.test.unit-test-suite") } diff --git a/subprojects/tdd-in-kotlin/build.gradle.kts b/subprojects/tdd-in-kotlin/build.gradle.kts index d6ee3af..76df370 100644 --- a/subprojects/tdd-in-kotlin/build.gradle.kts +++ b/subprojects/tdd-in-kotlin/build.gradle.kts @@ -1,4 +1,5 @@ plugins { + id("org.sdkotlin.buildlogic.global-exec") id("org.sdkotlin.buildlogic.kotlin-project") id("org.sdkotlin.buildlogic.test.test-fixtures-project") id("org.sdkotlin.buildlogic.test.unit-test-suite")