-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathBazelModService.kt
More file actions
50 lines (46 loc) · 1.63 KB
/
BazelModService.kt
File metadata and controls
50 lines (46 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.bazel_diff.bazel
import com.bazel_diff.log.Logger
import com.bazel_diff.process.Redirect
import com.bazel_diff.process.process
import java.nio.file.Path
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
/**
* Service that runs `bazel mod` to detect whether Bzlmod is enabled in the workspace.
* Used to decide whether to query //external:all-targets (disabled when Bzlmod is active).
*/
class BazelModService(
private val workingDirectory: Path,
private val bazelPath: Path,
private val startupOptions: List<String>,
private val noBazelrc: Boolean,
) : KoinComponent {
private val logger: Logger by inject()
/** True if Bzlmod is enabled (e.g. `bazel mod graph` succeeds). When true, //external is not available. */
val isBzlmodEnabled: Boolean by lazy { runBlocking { checkBzlmodEnabled() } }
@OptIn(ExperimentalCoroutinesApi::class)
private suspend fun checkBzlmodEnabled(): Boolean {
val cmd =
mutableListOf<String>().apply {
add(bazelPath.toString())
if (noBazelrc) {
add("--bazelrc=/dev/null")
}
addAll(startupOptions)
add("mod")
add("graph")
}
logger.i { "Executing Bazel mod graph: ${cmd.joinToString()}" }
val result =
process(
*cmd.toTypedArray(),
stdout = Redirect.CAPTURE,
stderr = Redirect.CAPTURE,
workingDirectory = workingDirectory.toFile(),
destroyForcibly = true,
)
return result.resultCode == 0
}
}