Skip to content

Commit

Permalink
fix: determine the version of java correctly #1068 (#1069)
Browse files Browse the repository at this point in the history
  • Loading branch information
yesudeep authored Mar 14, 2024
1 parent 7ba59df commit 2282f2c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
47 changes: 34 additions & 13 deletions private/java_utilities.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@
# Utilites for working with java versions
#

# Get numeric java version from `java -version` output e.g:
#
# openjdk version "11.0.9" 2020-10-20
# OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9+11)
# OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9+11, mixed mode)
#
def parse_java_version(java_version_output):
# Look for 'version "x.y.z"' in the output'
if len(java_version_output.split("version ")) > 1:
java_version = java_version_output.split("version ")[1].partition("\n")[0].split(" ")[0].replace("\"", "")
else:
return None
def is_version_character(c):
"""Returns True if the character is a version character, otherwise False."""
return c.isdigit() or c == "."

def index_of_version_character(s):
"""Determines index of 1st occurrence of version character in string."""
for i in range(len(s)):
if is_version_character(s[i]):
return i
return None

def index_of_non_version_character_from(s, index):
"""Determines index of 1st occurrence of non-version character in string."""
for i in range(index, len(s)):
if not is_version_character(s[i]):
return i
return None

def get_major_version(java_version):
"""Returns the major version from Java version string."""
if not java_version:
return None
elif "." not in java_version:
Expand All @@ -24,7 +31,21 @@ def parse_java_version(java_version_output):
else:
return int(java_version.split(".")[0])

return None
# Get numeric java version from `java -version` output e.g:
#
# openjdk version "11.0.9" 2020-10-20
# OpenJDK Runtime Environment AdoptOpenJDK (build 11.0.9+11)
# OpenJDK 64-Bit Server VM AdoptOpenJDK (build 11.0.9+11, mixed mode)
#
def parse_java_version(java_version_output):
first_line = java_version_output.strip().split("\n")[0]
if not first_line:
return None
i = index_of_version_character(first_line)
if i == None:
return None
j = index_of_non_version_character_from(first_line, i + 1)
return get_major_version(first_line[i:j])

# Build the contents of a java Command-Line Argument File from a list of
# arguments.
Expand Down
10 changes: 10 additions & 0 deletions tests/unit/java_utilities_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ OpenJDK 64-Bit Server VM (build 15+36-1562, mixed mode, sharing)
"""),
)

asserts.equals(
env,
22,
parse_java_version("""
openjdk 22-ea 2024-03-19
OpenJDK Runtime Environment (Red_Hat-22.0.0.0.36-1) (build 22-ea+36)
OpenJDK 64-Bit Server VM (Red_Hat-22.0.0.0.36-1) (build 22-ea+36, mixed mode, sharing)
"""),
)

return unittest.end(env)

parse_java_version_test = unittest.make(_parse_java_version_test_impl)
Expand Down

0 comments on commit 2282f2c

Please sign in to comment.