-
Notifications
You must be signed in to change notification settings - Fork 82
Expand file tree
/
Copy pathBazelModService.kt
More file actions
135 lines (125 loc) · 3.98 KB
/
Copy pathBazelModService.kt
File metadata and controls
135 lines (125 loc) · 3.98 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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() } }
/**
* Returns the module dependency graph as a string for hashing purposes. This captures all module
* dependencies and their versions, allowing bazel-diff to detect when MODULE.bazel changes (e.g.,
* when a module version is updated).
*
* @return The output of `bazel mod graph` if bzlmod is enabled, or null if disabled/error.
*/
@OptIn(ExperimentalCoroutinesApi::class)
suspend fun getModuleGraph(): String? {
if (!isBzlmodEnabled) {
return null
}
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 for hashing: ${cmd.joinToString()}" }
val result =
process(
*cmd.toTypedArray(),
stdout = Redirect.CAPTURE,
stderr = Redirect.SILENT,
workingDirectory = workingDirectory.toFile(),
destroyForcibly = true,
)
return if (result.resultCode == 0) {
result.output.joinToString("\n").trim()
} else {
logger.w { "Failed to get module graph" }
null
}
}
/**
* Returns the module dependency graph in JSON format for precise change detection.
*
* @return The JSON output of `bazel mod graph --output=json` if bzlmod is enabled, or null if
* disabled/error.
*/
@OptIn(ExperimentalCoroutinesApi::class)
suspend fun getModuleGraphJson(): String? {
if (!isBzlmodEnabled) {
return null
}
val cmd =
mutableListOf<String>().apply {
add(bazelPath.toString())
if (noBazelrc) {
add("--bazelrc=/dev/null")
}
addAll(startupOptions)
add("mod")
add("graph")
add("--output=json")
}
logger.i { "Executing Bazel mod graph JSON: ${cmd.joinToString()}" }
val result =
process(
*cmd.toTypedArray(),
stdout = Redirect.CAPTURE,
stderr = Redirect.SILENT,
workingDirectory = workingDirectory.toFile(),
destroyForcibly = true,
)
return if (result.resultCode == 0) {
result.output.joinToString("\n").trim()
} else {
logger.w { "Failed to get module graph JSON" }
null
}
}
@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
}
}