-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an example of a project-scoped exec task that depends on a buil…
…d-scoped exec from a shared build service.
- Loading branch information
Showing
4 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters