Kotlin: Build: Refactor version handling

We now have a proper class to represent versions, rather than using
tuples. The version is passed deeper down, so we can now have
version-dependent compilation flags.
This commit is contained in:
Ian Lynagh
2023-11-16 13:34:14 +00:00
parent a46dc55e84
commit e9800d11b6
2 changed files with 40 additions and 28 deletions

View File

@@ -14,19 +14,40 @@ def is_windows():
return True
return False
def version_tuple_to_string(version):
return f'{version[0]}.{version[1]}.{version[2]}{version[3]}'
class Version:
def __init__(self, major, minor, patch, tag):
self.major = major
self.minor = minor
self.patch = patch
self.tag = tag
def version_string_to_tuple(version):
def toTupleWithTag(self):
return [self.major, self.minor, self.patch, self.tag]
def toTupleNoTag(self):
return [self.major, self.minor, self.patch]
def lessThanOrEqual(self, other):
return self.toTupleNoTag() <= other.toTupleNoTag()
def toString(self):
return f'{self.major}.{self.minor}.{self.patch}{self.tag}'
def toLanguageVersionString(self):
return f'{self.major}.{self.minor}'
def version_string_to_version(version):
m = re.match(r'([0-9]+)\.([0-9]+)\.([0-9]+)(.*)', version)
return tuple([int(m.group(i)) for i in range(1, 4)] + [m.group(4)])
return Version(int(m.group(1)), int(m.group(2)), int(m.group(3)), m.group(4))
# Version number used by CI.
ci_version = '1.9.0'
many_versions = [ '1.5.0', '1.5.10', '1.5.20', '1.5.30', '1.6.0', '1.6.20', '1.7.0', '1.7.20', '1.8.0', '1.9.0-Beta', '1.9.20-Beta' ]
many_versions_tuples = [version_string_to_tuple(v) for v in many_versions]
many_versions_versions = [version_string_to_version(v) for v in many_versions]
many_versions_versions_asc = sorted(many_versions_versions, key = lambda v: v.toTupleWithTag())
many_versions_versions_desc = reversed(many_versions_versions_asc)
class KotlincNotFoundException(Exception):
pass
@@ -40,13 +61,11 @@ def get_single_version(fakeVersionOutput = None):
m = re.match(r'.* kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+-?[a-zA-Z]*) .*', versionOutput)
if m is None:
raise Exception('Cannot detect version of kotlinc (got ' + str(versionOutput) + ')')
current_version = version_string_to_tuple(m.group(1))
current_version = version_string_to_version(m.group(1))
many_versions_tuples.sort(reverse = True)
for version in many_versions_tuples:
if version[0:3] <= current_version[0:3]:
return version_tuple_to_string(version)
for version in many_versions_versions_desc:
if version.lessThanOrEqual(current_version):
return version.toString()
raise Exception(f'No suitable kotlinc version found for {current_version} (got {versionOutput}; know about {str(many_versions)})')