|
| 1 | +package org.sdkotlin.buildlogic.globalexec |
| 2 | + |
| 3 | +import org.gradle.api.DefaultTask |
| 4 | +import org.gradle.api.GradleException |
| 5 | +import org.gradle.api.provider.Property |
| 6 | +import org.gradle.api.services.BuildService |
| 7 | +import org.gradle.api.services.BuildServiceParameters |
| 8 | +import org.gradle.api.services.ServiceReference |
| 9 | +import org.gradle.api.tasks.TaskAction |
| 10 | +import org.gradle.kotlin.dsl.provideDelegate |
| 11 | +import org.gradle.process.ExecOperations |
| 12 | +import javax.inject.Inject |
| 13 | + |
| 14 | +abstract class BuildScopedGreetingService : |
| 15 | + BuildService<BuildScopedGreetingService.Params> { |
| 16 | + interface Params : BuildServiceParameters { |
| 17 | + val greeting: Property<String> |
| 18 | + } |
| 19 | + |
| 20 | + companion object { |
| 21 | + const val SERVICE_NAME = "buildScopedGreetingService" |
| 22 | + } |
| 23 | + |
| 24 | + @get:Inject |
| 25 | + abstract val execOperations: ExecOperations |
| 26 | + |
| 27 | + val exitValue: Int by lazy { |
| 28 | + execOperations.exec { |
| 29 | + commandLine( |
| 30 | + buildCurrentOsCommand( |
| 31 | + "echo", "Hello, ${parameters.greeting.get()}" |
| 32 | + ) |
| 33 | + ) |
| 34 | + standardOutput = System.out |
| 35 | + }.exitValue |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +abstract class ProjectScopedGreetingTask : DefaultTask() { |
| 40 | + init { |
| 41 | + group = "demo" |
| 42 | + description = "A project-scoped exec task that depends on a " + |
| 43 | + "build-scoped exec from a shared build service." |
| 44 | + } |
| 45 | + |
| 46 | + @Suppress("UnstableApiUsage") |
| 47 | + @get:ServiceReference(BuildScopedGreetingService.SERVICE_NAME) |
| 48 | + abstract val buildScopedGreetingService: Property<BuildScopedGreetingService> |
| 49 | + |
| 50 | + @get:Inject |
| 51 | + abstract val execOperations: ExecOperations |
| 52 | + |
| 53 | + @TaskAction |
| 54 | + fun doTaskAction() { |
| 55 | + |
| 56 | + val buildScopedGreetingServiceInstance: BuildScopedGreetingService = |
| 57 | + buildScopedGreetingService.get() |
| 58 | + |
| 59 | + val exitValue = buildScopedGreetingServiceInstance.exitValue |
| 60 | + |
| 61 | + if (exitValue != 0) |
| 62 | + throw GradleException( |
| 63 | + "Build service exec resulted in a non-zero exit value $exitValue!" |
| 64 | + ) |
| 65 | + |
| 66 | + val greeting = |
| 67 | + buildScopedGreetingServiceInstance.parameters.greeting.get() |
| 68 | + |
| 69 | + execOperations.exec { |
| 70 | + commandLine( |
| 71 | + buildCurrentOsCommand("echo", greeting) |
| 72 | + ) |
| 73 | + standardOutput = System.out |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +fun buildCurrentOsCommand(vararg arguments: String): List<String> = |
| 79 | + buildList { |
| 80 | + val isWindows = System.getProperty("os.name") |
| 81 | + .contains("windows", ignoreCase = true) |
| 82 | + if (isWindows) { |
| 83 | + add("cmd.exe") |
| 84 | + add("/c") |
| 85 | + } |
| 86 | + addAll(arguments) |
| 87 | + } |
0 commit comments