Skip to content

Commit a78124b

Browse files
committed
Bazel/Kotlin: use a wrapper to get the current kotlin version
1 parent 5c2d9fe commit a78124b

2 files changed

Lines changed: 19 additions & 10 deletions

File tree

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import subprocess
2+
import re
3+
import shutil
4+
5+
kotlinc = shutil.which('kotlinc')
6+
if kotlinc is None:
7+
raise Exception("kotlinc not found")
8+
output = subprocess.run([kotlinc, "-version"], text=True, stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
9+
check=True).stderr
10+
m = re.match(r'.* kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) .*', output)
11+
if m is None:
12+
raise Exception(f'Cannot detect version of kotlinc (got {output})')
13+
print(m[1])

java/kotlin-extractor/deps.bzl

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,12 @@ def _get_default_version(repository_ctx):
7575
return default_version
7676
if not repository_ctx.which("kotlinc"):
7777
return DEFAULT_VERSION
78-
res = repository_ctx.execute(["kotlinc", "-version"])
79-
if not res:
80-
fail("kotlinc -version failed: %s" % res.stderr)
81-
out = (res.stdout or res.stderr).split(" ")
82-
if "kotlinc-jvm" not in out:
83-
fail("kotlinc -version output does not contain 'kotlinc-jvm': %s" % out)
84-
kotlinc_jvm_index = out.index("kotlinc-jvm")
85-
if kotlinc_jvm_index + 1 >= len(out):
86-
fail("kotlinc -version output does not contain a version after 'kotlinc-jvm': %s" % out)
87-
return out[kotlinc_jvm_index + 1]
78+
kotlin_plugin_versions = repository_ctx.path(Label("//java/kotlin-extractor:current_kotlin_version.py"))
79+
python = repository_ctx.which("python3") or repository_ctx.which("python")
80+
res = repository_ctx.execute([python, kotlin_plugin_versions])
81+
if res.return_code != 0:
82+
fail(res.stderr)
83+
return res.stdout.strip()
8884

8985
def _get_available_version(version):
9086
for available_version in reversed(VERSIONS):

0 commit comments

Comments
 (0)