diff --git a/packager.py b/packager.py index 2fa06ab..31cc85f 100644 --- a/packager.py +++ b/packager.py @@ -2,12 +2,11 @@ import subprocess import sys import os import platform -from dataclasses import dataclass - +import hashlib import click -import glob import shutil import toml +from dataclasses import dataclass PACKAGER_TARGET_DIR = "target/packager" @@ -28,7 +27,7 @@ def cli(): def build(skipcargo): """Build espanso distribution""" # Check operating system - TARGET_OS = "macosx" + TARGET_OS = "macos" if platform.system() == "Windows": TARGET_OS = "windows" elif platform.system() == "Linux": @@ -53,12 +52,14 @@ def build(skipcargo): if TARGET_OS == "windows": build_windows(package_info) + elif TARGET_OS == "macos": + build_mac(package_info) def build_windows(package_info): - print("Starting packaging process on windows...") + print("Starting packaging process for Windows...") - # Check javapackager + # Check Inno Setup try: subprocess.run(["iscc"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) except FileNotFoundError: @@ -100,6 +101,51 @@ def build_windows(package_info): print("Compiling installer with Inno setup") subprocess.run(["iscc", os.path.abspath(os.path.join(TARGET_DIR, "setupscript.iss"))]) + +def build_mac(package_info): + print("Starting packaging process for MacOS...") + + print("Clearing target dirs") + + # Clearing previous build directory + if os.path.isdir(PACKAGER_TARGET_DIR): + print("Cleaning packager temp directory...") + shutil.rmtree(PACKAGER_TARGET_DIR) + + TARGET_DIR = os.path.join(PACKAGER_TARGET_DIR, "mac") + os.makedirs(TARGET_DIR, exist_ok=True) + + print("Compressing release to archive...") + target_name = f"espanso-mac-{package_info.version}.tar.gz" + archive_target = os.path.abspath(os.path.join(TARGET_DIR, target_name)) + subprocess.run(["tar", + "-C", os.path.abspath("target/release"), + "-cvf", + archive_target, + "espanso", + ]) + print(f"Created archive: {archive_target}") + + print("Processing Homebrew formula template") + with open("packager/mac/espanso.rb", "r") as formula_template: + content = formula_template.read() + + # Replace variables + content = content.replace("{{{app_desc}}}", package_info.description) + content = content.replace("{{{app_url}}}", package_info.url) + content = content.replace("{{{app_version}}}", package_info.version) + + # Calculate hash + with open(archive_target, "rb") as f: + bytes = f.read() + readable_hash = hashlib.sha256(bytes).hexdigest() + content = content.replace("{{{release_hash}}}", readable_hash) + + with open(os.path.join(TARGET_DIR, "espanso.rb"), "w") as output_script: + output_script.write(content) + + print("Done!") + if __name__ == '__main__': print("[[ espanso packager ]]") diff --git a/packager/mac/espanso.rb b/packager/mac/espanso.rb new file mode 100644 index 0000000..66c584b --- /dev/null +++ b/packager/mac/espanso.rb @@ -0,0 +1,14 @@ +# Documentation: https://docs.brew.sh/Formula-Cookbook +# https://rubydoc.brew.sh/Formula +# PLEASE REMOVE ALL GENERATED COMMENTS BEFORE SUBMITTING YOUR PULL REQUEST! +class Espanso < Formula + desc "{{{app_desc}}}" + homepage "{{{app_url}}}" + url "{{{app_release_url}}}" + sha256 "{{{release_hash}}}" + version "{{{app_version}}}" + + def install + bin.install "espanso" + end +end \ No newline at end of file