Skip to content

Commit

Permalink
Added an example of a project-scoped exec task that depends on a buil…
Browse files Browse the repository at this point in the history
…d-scoped exec from a shared build service.
  • Loading branch information
ianbrandt committed Nov 11, 2024
1 parent aae491d commit 9956f5b
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
abstract class BuildScopedGreetingService @Inject constructor(
execOperations: ExecOperations,
) : BuildService<BuildScopedGreetingService.Params> {

companion object {
const val SERVICE_NAME = "buildScopedGreetingService"
}

interface Params : BuildServiceParameters {
val greeting: Property<String>
}

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<BuildScopedGreetingService>

@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<ProjectScopedGreetingTask>("greetings")
1 change: 1 addition & 0 deletions subprojects/kotlin-for-java-devs-client/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
}
Expand Down
3 changes: 1 addition & 2 deletions subprojects/kotlin-for-java-devs/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
}
Expand Down
1 change: 1 addition & 0 deletions subprojects/tdd-in-kotlin/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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")
Expand Down

0 comments on commit 9956f5b

Please sign in to comment.