Skip to content

Commit

Permalink
fix version parse
Browse files Browse the repository at this point in the history
  • Loading branch information
restingbull committed Sep 6, 2024
1 parent abc1644 commit e5de023
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
2 changes: 0 additions & 2 deletions examples/jetpack_compose/WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ _KOTLIN_COMPILER_SHA = "88b39213506532c816ff56348c07bbeefe0c8d18943bffbad11063cf

# Setup Kotlin

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

http_archive(
name = "rules_kotlin",
sha256 = "34e8c0351764b71d78f76c8746e98063979ce08dcf1a91666f3f3bc2949a533d",
Expand Down
49 changes: 33 additions & 16 deletions src/main/kotlin/io/bazel/kotlin/test/BazelIntegrationTestRunner.kt
Original file line number Diff line number Diff line change
Expand Up @@ -65,22 +65,22 @@ object BazelIntegrationTestRunner {
workspace,
"--bazelrc=$bazelrc",
"info",
bzlmod.asFlag(),
version.workspaceFlag(bzlmod),
"--override_repository=rules_kotlin=$unpack",
).onFailThrow()
bazel.run(
workspace,
"--bazelrc=$bazelrc",
"build",
bzlmod.asFlag(),
version.workspaceFlag(bzlmod),
"--override_repository=rules_kotlin=$unpack",
"//...",
).onFailThrow()
bazel.run(
workspace,
"--bazelrc=$bazelrc",
"query",
bzlmod.asFlag(),
version.workspaceFlag(bzlmod),
"--override_repository=rules_kotlin=$unpack",
"kind(\".*_test\", \"//...\")",
).ok { process ->
Expand All @@ -89,27 +89,22 @@ object BazelIntegrationTestRunner {
workspace,
"--bazelrc=$bazelrc",
"test",
bzlmod.asFlag(),
version.workspaceFlag(bzlmod),
"--override_repository=rules_kotlin=$unpack",
"//...",
).onFailThrow()
}
}
}
}

fun workspaceConfiguration(enableBzlMod:Boolean) {

}

private fun Boolean.asFlag() : String = if (this) { "--enable_bzlmod=true" } else { "--enable_workspace=true" }

private fun Path.hasModule() = resolve("MODULE").exists() || resolve("MODULE.bazel").exists()
fun Path.hasModule() = resolve("MODULE").exists() || resolve("MODULE.bazel").exists()
private fun Path.hasWorkspace() = resolve("WORKSPACE").exists() || resolve("WORKSPACE.bazel").exists()

sealed class Version{
sealed class Version {
abstract fun resolveBazelRc(workspace: Path): Path;


abstract fun workspaceFlag(isBzlMod: Boolean): String

class Head : Version() {
override fun resolveBazelRc(workspace: Path): Path {
workspace.resolve(".bazelrc.head").takeIf(Path::exists)?.let {
Expand All @@ -120,6 +115,12 @@ object BazelIntegrationTestRunner {
}
return workspace.resolve("/dev/null")
}

override fun workspaceFlag(isBzlMod: Boolean): String = if (isBzlMod) {
"--enable_bzlmod=true"
} else {
"--enable_workspace=true"
}
}

class Known(private val major: Int, private val minor: Int, private val patch: Int) : Version() {
Expand All @@ -142,9 +143,19 @@ object BazelIntegrationTestRunner {
}
return workspace.resolve("/dev/null")
}

override fun workspaceFlag(isBzlMod: Boolean): String = if (isBzlMod) {
"--enable_bzlmod=true"
} else if (major >= 7) {
"--enable_workspace=true"
} else {
"--enable_bzlmod=false"
}
}
}

private val VERSION_REGEX = Regex("(?<major>\\d+)\\.(?<minor>\\d+)\\.(?<patch>\\d+)([^.]*)")

private fun Result<ProcessResult>.parseVersion(): Version {
ok { result ->
result.stdOut.toString(UTF_8).split("\n")
Expand All @@ -154,8 +165,14 @@ object BazelIntegrationTestRunner {
if ("no_version" in line) {
return Version.Head()
}
val parts = line.split(" ")[1].split(".")
return Version.Known(parts[0].toInt(), parts[1].toInt(), parts[2].toInt())
VERSION_REGEX.find(line.trim())?.let { result ->
return Version.Known(
major = result.groups["major"]?.value?.toInt() ?: 0,
minor = result.groups["minor"]?.value?.toInt() ?: 0,
patch = result.groups["patch"]?.value?.toInt() ?: 0,
)
}

}
throw IllegalStateException("Bazel version not available")
}
Expand Down

0 comments on commit e5de023

Please sign in to comment.