Skip to content

Commit

Permalink
draft implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Igor Litvinenko committed Aug 31, 2023
1 parent efcbe06 commit bb2026d
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 26 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ jobs:
with:
python-version: '3.10'
- run: pip install -r .scripts/requirements.txt
- run: python .scripts/release.py ${{ secrets.TOKEN }}
- run: python .scripts/release.py ${{ secrets.TOKEN }} ${{ github.ref_name }}
76 changes: 52 additions & 24 deletions .scripts/release.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,25 @@ def is_new_version(version:str, current_verions_path:str):

return semver.compare(version, current_version) > 0

def get_latest_release(github: Github, name: str, file_name: str):
def get_latest_release(github: Github, name: str):
repo = github.get_repo(name)
release = repo.get_latest_release()
version = release.tag_name
return (version, release)

def get_release_asset_url(gh_release, asset_name):
url = None
for asset in release.get_assets():
if asset.content_type == "application/zip" and asset.browser_download_url.endswith(file_name):
for asset in gh_release.get_assets():
if asset.content_type == "application/zip" and asset.browser_download_url.endswith(asset_name):
url = asset.browser_download_url
break

return (version, url)
return url

def download_url_and_extract(url: str, destination: str):
directory = "Airship"

print('downloading airship.zip release asset')
print(f'downloading {destination} release asset')
with urllib.request.urlopen(url) as dl_file:
with open(destination, 'wb') as out_file:
out_file.write(dl_file.read())
Expand All @@ -64,6 +67,9 @@ def download_url_and_extract(url: str, destination: str):
shutil.unpack_archive(destination, directory)

os.remove(destination)

# TODO: remove
directory = os.path.join(directory, directory)

return directory

Expand All @@ -72,19 +78,22 @@ def copy_misc_files(source_dir:str, traget_dir:str, misc_files:list):
for file in misc_files:
shutil.copy(os.path.join(source_dir, file), traget_dir)

def zip_all_frameworks(source_dir: str, destination_dir:str):
def zip_all_frameworks(source_dir: str, destination_dir:str, suffix: str):
print('repacking airship components')
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)

def zip_dir(dir:str, zip_path:str):
print(f'zipping {dir}')
print(f'zipping {dir} to {zip_path}')
shutil.make_archive(zip_path, 'zip', base_dir=dir)

with os.scandir(source_dir) as it:
for entry in it:
if entry.name.endswith(".xcframework") and entry.is_dir():
zip_dir(entry.path, os.path.join(destination_dir, entry.name))
components = os.path.splitext(entry.name)
result_filename = ''.join([components[0], suffix, components[1]])

zip_dir(entry.path, os.path.join(destination_dir, result_filename))

def compute_checksum_for_frameworks(frameworks_dir:str, project_path:str):
print('computing frameworks checksums')
Expand All @@ -97,23 +106,28 @@ def compute_checksum_for_frameworks(frameworks_dir:str, project_path:str):

return result

def release_files_in_dir(github:Github, repo_name:str, dir:str, version:str):
def release_files_in_dir(github:Github, repo_name:str, dir:str, version:str, create_new:bool):
print(f'Creating a github release with version {version}')
repo = github.get_repo(repo_name)
release = repo.create_git_release(version, version, f'Prebuilt Ariship SDK v{version}')
if create_new:
release = repo.create_git_release(version, version, f'Prebuilt Ariship SDK v{version}')
else:
existing_version, release = get_latest_release(github, repo_name)
if existing_version != version:
raise Exception(f'Version mismatch on trying to update the existing release. Current version {version}, release version {existing_version}')

with os.scandir(dir) as it:
for entry in it:
if not entry.name.endswith('.zip'): continue
print(f'uploading {entry.name}')
release.upload_asset(entry.path)

def generate_package_swift(target_dir:str, template:str, release_url:str, checksums:dict):
def generate_package_swift(target_dir:str, template:str, release_url:str, checksums:dict, suffix: str):
basement_target_name = "AirshipBasement"
core_target_name = "AirshipCore"

def generate_product_entry(name):
product_name = name.split('.')[0]
product_name = name.split('.')[0].removesuffix(suffix)
additional_targets = ""
if product_name == basement_target_name: return None
if product_name == core_target_name: additional_targets = f', "{basement_target_name}"'
Expand All @@ -126,7 +140,7 @@ def generate_product_entry(name):

def generate_binary_entry(entry):
name, checksum = entry
product_name = name.split('.')[0]
product_name = name.split('.')[0].removesuffix(suffix)
return f'''
.binaryTarget(
name: "{product_name}",
Expand Down Expand Up @@ -167,44 +181,58 @@ def commit_changes_with_tag(parent_dir:str, version:str, version_file:str, misc_

airship_repo = "urbanairship/ios-library"
prebuilt_repo = "urbanairship/ios-library-prebuilt"
airship_frameworks_asset_name = "Airship.zip"
airship_frameworks_asset_name_xcode_14 = "Airship.zip"
airship_frameworks_asset_name_xcode_15 = "Airship-Xcode15.zip"
package_template = ".scripts/package.swift.template"
release_dir = "release-tmp"
distribution_repo_url = "https://github.com/urbanairship/ios-library-prebuilt"
current_version_file = 'VERSION'
misc_files_to_copy = ["BUILD_INFO", "CHANGELOG.md", "LICENSE", "README.md"]

def main(github_token):
print('configuring github')
def main(github_token, branch):
print(f'configuring github on branch {branch}')
github = Github(login_or_token=github_token)

if branch == 'main':
asset_name = airship_frameworks_asset_name_xcode_14
should_create_new_release = True
suffix = ''
elif branch == 'xcode-15':
asset_name = airship_frameworks_asset_name_xcode_15
should_create_new_release = False
suffix = '-Xcode15'
else:
raise Exception(f'Not allowed running release from branch {branch}')

print('getting the latest airship release')
version, link = get_latest_release(github, airship_repo, airship_frameworks_asset_name)
version, gh_release = get_latest_release(github, airship_repo)

if not is_new_version(version, os.path.join('.', current_version_file)):
print('found the same or older vesrion. ignoring')
return

print('got a new airship release. continue')

filepath = download_url_and_extract(link, airship_frameworks_asset_name)
link = get_release_asset_url(gh_release, asset_name)
filepath = download_url_and_extract(link, asset_name)

if not os.path.exists(filepath):
raise Exception(f'Failed to open {filepath}')

copy_misc_files(filepath, './', misc_files_to_copy)

zip_all_frameworks(filepath, release_dir)
zip_all_frameworks(filepath, release_dir, suffix)
framework_checksums = compute_checksum_for_frameworks(release_dir, './')

generate_package_swift("./", package_template, f'{distribution_repo_url}/releases/download/{version}/', framework_checksums)
generate_package_swift("./", package_template, f'{distribution_repo_url}/releases/download/{version}/', framework_checksums, suffix)

commit_changes_with_tag('./', version, current_version_file, misc_files_to_copy)
release_files_in_dir(github, prebuilt_repo, release_dir, version)
release_files_in_dir(github, prebuilt_repo, release_dir, version, should_create_new_release)

parser = argparse.ArgumentParser(description='Airship prebuil library action')
parser.add_argument('token', type=str, help='Github access token')
parser.add_argument('branch', type=str, help='Current branch name')

main(parser.parse_args().token)
parsed_args = parser.parse_args()
main(parsed_args.token, parsed_args.branch)


1 change: 0 additions & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +0,0 @@
17.2.1

0 comments on commit bb2026d

Please sign in to comment.