2022-05-24 14:40:16 +03:00
|
|
|
#!/usr/bin/python
|
|
|
|
|
2022-03-15 15:21:25 +03:00
|
|
|
import platform
|
2021-12-01 16:00:19 +03:00
|
|
|
import re
|
2022-05-20 14:44:55 +03:00
|
|
|
import shutil
|
2021-12-01 16:00:19 +03:00
|
|
|
import subprocess
|
2022-04-08 15:58:28 +03:00
|
|
|
import sys
|
2021-12-01 16:00:19 +03:00
|
|
|
|
2022-03-15 15:21:25 +03:00
|
|
|
def is_windows():
|
|
|
|
'''Whether we appear to be running on Windows'''
|
|
|
|
if platform.system() == 'Windows':
|
|
|
|
return True
|
|
|
|
if platform.system().startswith('CYGWIN'):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2023-11-16 16:34:14 +03:00
|
|
|
class Version:
|
|
|
|
def __init__(self, major, minor, patch, tag):
|
|
|
|
self.major = major
|
|
|
|
self.minor = minor
|
|
|
|
self.patch = patch
|
|
|
|
self.tag = tag
|
2022-04-13 00:25:51 +03:00
|
|
|
|
2023-11-16 16:34:14 +03:00
|
|
|
def toTupleWithTag(self):
|
|
|
|
return [self.major, self.minor, self.patch, self.tag]
|
|
|
|
|
|
|
|
def toTupleNoTag(self):
|
|
|
|
return [self.major, self.minor, self.patch]
|
|
|
|
|
2023-11-17 13:48:33 +03:00
|
|
|
def lessThan(self, other):
|
|
|
|
return self.toTupleNoTag() < other.toTupleNoTag()
|
|
|
|
|
2023-11-16 16:34:14 +03:00
|
|
|
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):
|
2022-06-16 15:25:59 +03:00
|
|
|
m = re.match(r'([0-9]+)\.([0-9]+)\.([0-9]+)(.*)', version)
|
2023-11-16 16:34:14 +03:00
|
|
|
return Version(int(m.group(1)), int(m.group(2)), int(m.group(3)), m.group(4))
|
2022-04-13 00:25:51 +03:00
|
|
|
|
2023-02-03 19:19:29 +03:00
|
|
|
# Version number used by CI.
|
2023-07-11 16:10:48 +03:00
|
|
|
ci_version = '1.9.0'
|
2022-08-12 15:27:06 +03:00
|
|
|
|
2024-04-10 19:57:42 +03:00
|
|
|
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', '2.0.0-RC1' ]
|
2021-12-01 16:00:19 +03:00
|
|
|
|
2023-11-16 16:34:14 +03:00
|
|
|
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)
|
2022-04-13 00:25:51 +03:00
|
|
|
|
2022-05-19 23:54:18 +03:00
|
|
|
class KotlincNotFoundException(Exception):
|
|
|
|
pass
|
|
|
|
|
2022-04-13 00:25:51 +03:00
|
|
|
def get_single_version(fakeVersionOutput = None):
|
2022-05-20 14:44:55 +03:00
|
|
|
# kotlinc might be kotlinc.bat or kotlinc.cmd on Windows, so we use `which` to find out what it is
|
|
|
|
kotlinc = shutil.which('kotlinc')
|
|
|
|
if kotlinc is None:
|
|
|
|
raise KotlincNotFoundException()
|
2022-05-20 14:59:04 +03:00
|
|
|
versionOutput = subprocess.run([kotlinc, '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True).stderr if fakeVersionOutput is None else fakeVersionOutput
|
2023-11-17 13:41:56 +03:00
|
|
|
m = re.match(r'.* kotlinc-jvm ([0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z][a-zA-Z0-9]*)?) .*', versionOutput)
|
2021-12-01 16:00:19 +03:00
|
|
|
if m is None:
|
2022-03-15 15:21:25 +03:00
|
|
|
raise Exception('Cannot detect version of kotlinc (got ' + str(versionOutput) + ')')
|
2023-11-16 16:34:14 +03:00
|
|
|
current_version = version_string_to_version(m.group(1))
|
2022-04-07 17:20:47 +03:00
|
|
|
|
2023-11-16 16:34:14 +03:00
|
|
|
for version in many_versions_versions_desc:
|
|
|
|
if version.lessThanOrEqual(current_version):
|
|
|
|
return version.toString()
|
2022-04-07 17:20:47 +03:00
|
|
|
|
|
|
|
raise Exception(f'No suitable kotlinc version found for {current_version} (got {versionOutput}; know about {str(many_versions)})')
|
2022-04-08 15:58:28 +03:00
|
|
|
|
|
|
|
def get_latest_url():
|
2022-08-12 15:27:06 +03:00
|
|
|
url = 'https://github.com/JetBrains/kotlin/releases/download/v' + ci_version + '/kotlin-compiler-' + ci_version + '.zip'
|
2022-04-08 15:58:28 +03:00
|
|
|
return url
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
args = sys.argv
|
2022-04-13 00:25:51 +03:00
|
|
|
if len(args) < 2:
|
2022-04-08 15:58:28 +03:00
|
|
|
raise Exception("Bad arguments")
|
|
|
|
command = args[1]
|
|
|
|
if command == 'latest-url':
|
|
|
|
print(get_latest_url())
|
2022-04-13 00:25:51 +03:00
|
|
|
elif command == 'single-version':
|
|
|
|
print(get_single_version(*args[2:]))
|
2022-04-08 15:58:28 +03:00
|
|
|
else:
|
|
|
|
raise Exception("Unknown command: " + command)
|
|
|
|
|