-
Notifications
You must be signed in to change notification settings - Fork 47
/
BazelInfo.kt
76 lines (57 loc) · 2.03 KB
/
BazelInfo.kt
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
package org.jetbrains.bsp.bazel.bazelrunner
import java.nio.file.Path
import kotlin.io.path.isReadable
import kotlin.io.path.readText
interface BazelInfo {
val execRoot: String
val outputBase: Path
val workspaceRoot: Path
val release: BazelRelease
val isBzlModEnabled: Boolean
}
data class BazelRelease(
val major: Int
) {
fun mainRepositoryReferencePrefix() = when (major) {
in 0..3 -> throw RuntimeException("Unsupported Bazel version, use Bazel 4 or newer")
in 4..5 -> "//"
else -> "@//"
}
companion object {
fun fromReleaseString(versionString: String): BazelRelease? =
VERSION_REGEX.find(versionString)?.toBazelRelease()
fun fromBazelVersionFile(workspacePath: Path): BazelRelease? {
val versionString = workspacePath.resolve(".bazelversion")
.takeIf { it.isReadable() }
?.readText()
.orEmpty()
return BAZEL_VERSION_MAJOR_REGEX.find(versionString)?.toBazelRelease()
}
private fun MatchResult.toBazelRelease() =
BazelRelease(value.toInt())
internal val LATEST_SUPPORTED_MAJOR = BazelRelease(6)
private val BAZEL_VERSION_MAJOR_REGEX = """^\d+""".toRegex()
private val VERSION_REGEX = """(?<=release )\d+(?=[0-9.]*)""".toRegex()
}
}
fun BazelRelease?.orLatestSupported() = this ?: BazelRelease.LATEST_SUPPORTED_MAJOR
data class BasicBazelInfo(
override val execRoot: String,
override val outputBase: Path,
override val workspaceRoot: Path,
override val release: BazelRelease,
override val isBzlModEnabled: Boolean,
) : BazelInfo
class LazyBazelInfo(bazelInfoSupplier: () -> BazelInfo) : BazelInfo {
private val bazelInfo: BazelInfo by lazy { bazelInfoSupplier() }
override val execRoot: String
get() = bazelInfo.execRoot
override val outputBase: Path
get() = bazelInfo.outputBase
override val workspaceRoot: Path
get() = bazelInfo.workspaceRoot
override val release: BazelRelease
get() = bazelInfo.release
override val isBzlModEnabled: Boolean
get() = bazelInfo.isBzlModEnabled
}