Skip to content

Commit dde175e

Browse files
committed
Fixed global exec example to be cross-platform.
1 parent 8fc9b17 commit dde175e

File tree

2 files changed

+89
-55
lines changed

2 files changed

+89
-55
lines changed

build-logic/src/main/kotlin/org.sdkotlin.buildlogic.global-exec.gradle.kts

Lines changed: 2 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,5 @@
1-
abstract class BuildScopedGreetingService @Inject constructor(
2-
execOperations: ExecOperations,
3-
) : BuildService<BuildScopedGreetingService.Params> {
4-
5-
companion object {
6-
const val SERVICE_NAME = "buildScopedGreetingService"
7-
}
8-
9-
interface Params : BuildServiceParameters {
10-
val greeting: Property<String>
11-
}
12-
13-
val exitValue: Int =
14-
execOperations.exec {
15-
commandLine("echo", "Hello, ${parameters.greeting.get()}")
16-
standardOutput = System.out
17-
}.exitValue
18-
}
19-
20-
abstract class ProjectScopedGreetingTask @Inject constructor(
21-
private val execOperations: ExecOperations,
22-
) : DefaultTask() {
23-
24-
init {
25-
group = "demo"
26-
description = "A project-scoped exec task that depends on a " +
27-
"build-scoped exec from a shared build service."
28-
}
29-
30-
@Suppress("UnstableApiUsage")
31-
@get:ServiceReference(BuildScopedGreetingService.SERVICE_NAME)
32-
abstract val buildScopedGreetingService: Property<BuildScopedGreetingService>
33-
34-
@TaskAction
35-
fun doTaskAction() {
36-
37-
val buildScopedGreetingServiceInstance: BuildScopedGreetingService =
38-
buildScopedGreetingService.get()
39-
40-
val exitValue = buildScopedGreetingServiceInstance.exitValue
41-
42-
if (exitValue != 0)
43-
throw GradleException(
44-
"Build service exec resulted in a non-zero exit value $exitValue!"
45-
)
46-
47-
val greeting =
48-
buildScopedGreetingServiceInstance.parameters.greeting.get()
49-
50-
execOperations.exec {
51-
commandLine("echo", greeting)
52-
standardOutput = System.out
53-
}
54-
}
55-
}
1+
import org.sdkotlin.buildlogic.globalexec.BuildScopedGreetingService
2+
import org.sdkotlin.buildlogic.globalexec.ProjectScopedGreetingTask
563

574
gradle.sharedServices.registerIfAbsent(
585
BuildScopedGreetingService.SERVICE_NAME,
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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

Comments
 (0)