@@ -11,29 +11,35 @@ def is_windows():
1111 return True
1212 return False
1313
14+ def version_tuple_to_string (version ):
15+ return f'{ version [0 ]} .{ version [1 ]} .{ version [2 ]} '
16+
17+ def version_string_to_tuple (version ):
18+ m = re .match (r'([0-9]+)\.([0-9]+)\.([0-9]+)' , version )
19+ return tuple ([int (m .group (i )) for i in range (1 , 4 )])
20+
1421many_versions = [ '1.4.32' , '1.5.31' , '1.6.10' , '1.6.20' ]
1522
16- def get_single_version ():
23+ many_versions_tuples = [version_string_to_tuple (v ) for v in many_versions ]
24+
25+ def get_single_version (fakeVersionOutput = None ):
1726 # TODO: `shell=True` is a workaround to get CI working on Windows. It breaks the build on Linux.
18- versionOutput = subprocess .run (['kotlinc' , '-version' ], capture_output = True , text = True , shell = is_windows ())
19- m = re .match (r'.* kotlinc-jvm ([0-9]+)\.( [0-9]+)\.( [0-9]+) .*' , versionOutput . stderr )
27+ versionOutput = subprocess .run (['kotlinc' , '-version' ], capture_output = True , text = True , shell = is_windows ()). stderr if fakeVersionOutput is None else fakeVersionOutput
28+ m = re .match (r'.* kotlinc-jvm ([0-9]+\. [0-9]+\. [0-9]+) .*' , versionOutput )
2029 if m is None :
2130 raise Exception ('Cannot detect version of kotlinc (got ' + str (versionOutput ) + ')' )
22- major = m .group (1 )
23- minor = m .group (2 )
24- patch = m .group (3 )
25- current_version = f'{ major } .{ minor } .{ patch } '
26- matching_minor_versions = [ version for version in many_versions if version .startswith (f'{ major } .{ minor } ' ) ]
31+ current_version = version_string_to_tuple (m .group (1 ))
32+ matching_minor_versions = [ version for version in many_versions_tuples if version [0 :2 ] == current_version [0 :2 ] ]
2733 if len (matching_minor_versions ) == 0 :
2834 raise Exception (f'Cannot find a matching minor version for kotlinc version { current_version } (got { versionOutput } ; know about { str (many_versions )} )' )
2935
3036 matching_minor_versions .sort ()
3137
3238 for version in matching_minor_versions :
33- if current_version >= version :
34- return version
39+ if version >= current_version :
40+ return version_tuple_to_string ( version )
3541
36- return matching_minor_versions [- 1 ]
42+ return version_tuple_to_string ( matching_minor_versions [- 1 ])
3743
3844 raise Exception (f'No suitable kotlinc version found for { current_version } (got { versionOutput } ; know about { str (many_versions )} )' )
3945
@@ -44,11 +50,13 @@ def get_latest_url():
4450
4551if __name__ == "__main__" :
4652 args = sys .argv
47- if len (args ) != 2 :
53+ if len (args ) < 2 :
4854 raise Exception ("Bad arguments" )
4955 command = args [1 ]
5056 if command == 'latest-url' :
5157 print (get_latest_url ())
58+ elif command == 'single-version' :
59+ print (get_single_version (* args [2 :]))
5260 else :
5361 raise Exception ("Unknown command: " + command )
5462
0 commit comments