2
2
from github import Github
3
3
import hashlib
4
4
import requests
5
+ import fileinput
6
+ import sys
5
7
6
8
GITHUB_REPO = "mozilla/application-services"
7
9
XC_FRAMEWORK_NAME = "MozillaRustComponents.xcframework.zip"
12
14
def get_xcframework_artifact ():
13
15
g = Github (github_access_token )
14
16
repo = g .get_repo (GITHUB_REPO )
15
-
17
+
16
18
release = repo .get_release (as_version )
17
19
for asset in release .get_assets ():
18
20
if asset .name == XC_FRAMEWORK_NAME :
@@ -22,38 +24,18 @@ def compute_checksum(xc_framework_url):
22
24
req = requests .get (xc_framework_url )
23
25
return hashlib .sha256 (req .content ).hexdigest ()
24
26
25
- def update_package_swift (xc_framework_url , checksum ):
26
- old_url = ''
27
- old_checksum = ''
28
- with open (PACKAGE_SWIFT ) as fp :
29
- line = fp .readline ()
30
- while line :
31
- line = fp .readline ()
32
-
33
- # If this is the line that has the URL to
34
- # the xcframework
35
- if XC_FRAMEWORK_NAME in line :
36
- url_start = line .find ('"' ) + 1
37
- url_end = line .find ('"' , url_start )
38
- old_url = line [url_start :url_end ]
39
- # NOTE: We assume that the next line is the
40
- # checksum, there is a note in Package.swift
41
- # to make sure we don't change that
42
- line = fp .readline ()
43
- checksum_start = line .find ('"' ) + 1
44
- checksum_end = line .find ('"' , checksum_start )
45
- old_checksum = line [checksum_start :checksum_end ]
46
- break
47
- file = open (PACKAGE_SWIFT , "r+" )
48
- data = file .read ()
49
- data = data .replace (old_url , xc_framework_url )
50
- data = data .replace (old_checksum , checksum )
51
- file .close ()
52
- file = open (PACKAGE_SWIFT , "wt" )
53
- file .write (data )
54
- file .close ()
55
-
56
-
27
+ def update_package_swift (as_version , checksum ):
28
+ version_line = "let version ="
29
+ checksum_line = "let checksum ="
30
+
31
+ for line in fileinput .input (PACKAGE_SWIFT , inplace = 1 ):
32
+ if line .strip ().startswith (version_line ):
33
+ # Replace the line with the new version included
34
+ line = f"{ version_line } \" { as_version } \" \n "
35
+ elif line .strip ().startswith (checksum_line ):
36
+ # Replace the line with the new computed checksum
37
+ line = f"{ checksum_line } \" { checksum } \" \n "
38
+ sys .stdout .write (line )
57
39
def main ():
58
40
'''
59
41
Updates `Package.swift` with the latest
@@ -63,7 +45,7 @@ def main():
63
45
exit (1 )
64
46
xc_framework_url = get_xcframework_artifact ()
65
47
checksum = compute_checksum (xc_framework_url )
66
- update_package_swift (xc_framework_url , checksum )
48
+ update_package_swift (as_version , checksum )
67
49
68
50
69
51
0 commit comments