33import argparse
44import os
55import re
6+ import subprocess
67
78VERSION_PATTERN = '\d+\.\d+\.\d+'
89
910parser = argparse .ArgumentParser (
1011 description = "Update files containing hard-coded aws-crt-java version numbers." )
11- parser .add_argument ('version' , help = 'aws-crt-java version. i.e. "0.1.2"' )
12-
12+ parser .add_argument ('version' , nargs = '?' ,
13+ help = 'version to use (i.e. "0.1.2"). default: automatically detect latest version' )
1314args = parser .parse_args ()
14- if re .fullmatch (VERSION_PATTERN , args .version ) is None :
15- exit (f'Invalid version: "{ args .version } ". Version must look like "0.1.2"' )
1615
17- os .chdir (os .path .dirname (__file__ ))
1816
17+ def main ():
18+ if args .version is None :
19+ args .version = get_latest_crt_version ()
20+ print (f'Latest version: { args .version } ' )
21+
22+ if re .fullmatch (VERSION_PATTERN , args .version ) is None :
23+ exit (f'Invalid version: "{ args .version } ". Must look like "0.1.2"' )
24+
25+ os .chdir (os .path .dirname (__file__ ))
26+
27+ update (filepath = 'sdk/pom.xml' ,
28+ preceded_by = r'<artifactId>aws-crt</artifactId>\s*<version>' ,
29+ followed_by = r'</version>' )
30+
31+ update (filepath = 'README.md' ,
32+ preceded_by = r'--branch v' ,
33+ followed_by = r' .*aws-crt-java.git' )
1934
20- def update (filepath , * , preceded_by , followed_by ):
35+ update (filepath = 'README.md' ,
36+ preceded_by = r"implementation 'software.amazon.awssdk.crt:android:" ,
37+ followed_by = r"'" )
38+
39+ update (filepath = 'android/iotdevicesdk/build.gradle' ,
40+ preceded_by = r"api 'software.amazon.awssdk.crt:aws-crt-android:" ,
41+ followed_by = r"'" )
42+
43+
44+ def update (* , filepath , preceded_by , followed_by ):
2145 """
2246 Args:
2347 filepath: File containing hard-coded CRT version numbers.
@@ -40,18 +64,30 @@ def update(filepath, *, preceded_by, followed_by):
4064 f .truncate ()
4165
4266
43- update (filepath = 'sdk/pom.xml' ,
44- preceded_by = r'<artifactId>aws-crt</artifactId>\s*<version>' ,
45- followed_by = r'</version>' )
67+ def get_latest_crt_version ():
68+ repo = 'https://github.com/awslabs/aws-crt-java.git'
69+ cmd = ['git' , 'ls-remote' , '--tags' , repo ]
70+ results = subprocess .run (cmd , check = True , capture_output = True , text = True )
71+
72+ latest_str = None
73+ latest_nums = None
74+ for line in results .stdout .splitlines ():
75+ # line looks like: "e18f041a0c8d17189f2eae2a32f16e0a7a3f0f1c refs/tags/v0.5.18"
76+ pattern = r'[a-f0-9]+\s+refs/tags/v((\d+)\.(\d+)\.(\d+))'
77+ match = re .fullmatch (pattern , line )
78+ if not match :
79+ continue
80+
81+ # we only want the latest version
82+ # convert to tuple of numbers so we can compare
83+ version_str = match .group (1 )
84+ version_nums = [int (match .group (x )) for x in range (3 , 5 )]
85+ if latest_nums is None or (version_nums > latest_nums ):
86+ latest_nums = version_nums
87+ latest_str = version_str
4688
47- update (filepath = 'README.md' ,
48- preceded_by = r'--branch v' ,
49- followed_by = r' .*aws-crt-java.git' )
89+ return latest_str
5090
51- update (filepath = 'README.md' ,
52- preceded_by = r"implementation 'software.amazon.awssdk.crt:android:" ,
53- followed_by = r"'" )
5491
55- update (filepath = 'android/iotdevicesdk/build.gradle' ,
56- preceded_by = r"api 'software.amazon.awssdk.crt:aws-crt-android:" ,
57- followed_by = r"'" )
92+ if __name__ == '__main__' :
93+ main ()
0 commit comments