|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import plistlib |
| 5 | +import shutil |
| 6 | +import subprocess |
| 7 | +from zipfile import ZIP_STORED, ZipFile |
| 8 | + |
| 9 | +import tomllib |
| 10 | + |
| 11 | + |
| 12 | +def parse_info_plist(): |
| 13 | + """Parse the info.plist file""" |
| 14 | + with open("info.plist", "rb") as f: |
| 15 | + plist = plistlib.load(f) |
| 16 | + return plist |
| 17 | + |
| 18 | + |
| 19 | +def get_workflow_name(): |
| 20 | + """Get the workflow name from parsed plist""" |
| 21 | + plist = parse_info_plist() |
| 22 | + name = plist["name"].replace(" ", "_").lower() |
| 23 | + return name |
| 24 | + |
| 25 | + |
| 26 | +def get_workflow_version(): |
| 27 | + """Get the workflow version from parsed plist""" |
| 28 | + plist = parse_info_plist() |
| 29 | + version = plist["version"].replace(" ", "_").lower() |
| 30 | + return version |
| 31 | + |
| 32 | + |
| 33 | +def get_pyproject_version(): |
| 34 | + """Get the project version from pyproject.toml""" |
| 35 | + with open("pyproject.toml", "rb") as f: |
| 36 | + pyproject = tomllib.load(f) |
| 37 | + version = pyproject["tool"]["poetry"]["version"] |
| 38 | + return version |
| 39 | + |
| 40 | + |
| 41 | +def update_version(version: str, plist_path: str = "info.plist"): |
| 42 | + """Update the version in info.plist""" |
| 43 | + plist = parse_info_plist() |
| 44 | + plist["version"] = version |
| 45 | + with open(plist_path, "wb") as f: |
| 46 | + plistlib.dump(plist, f) |
| 47 | + |
| 48 | + |
| 49 | +def init_venv(): |
| 50 | + """Initialize the venv""" |
| 51 | + |
| 52 | + if os.path.exists(".venv"): |
| 53 | + shutil.rmtree(".venv") |
| 54 | + |
| 55 | + subprocess.run(["poetry", "install", "--only", "main"], check=True) |
| 56 | + |
| 57 | + print("Dependencies installed successfully.") |
| 58 | + |
| 59 | + |
| 60 | +def zip_workflow(filename: str): |
| 61 | + """Zip the workflow""" |
| 62 | + basepath = os.getcwd() |
| 63 | + |
| 64 | + zip_contents = [ |
| 65 | + "icon.png", |
| 66 | + "info.plist", |
| 67 | + "main.py", |
| 68 | + "src", |
| 69 | + ".venv", |
| 70 | + ] |
| 71 | + zip_contents = [os.path.join(basepath, file) for file in zip_contents] |
| 72 | + zip_exlude = ["__pycache__"] |
| 73 | + |
| 74 | + def should_include(path): |
| 75 | + exclude_paths = any(excluded in path for excluded in zip_exlude) |
| 76 | + include_paths = any(included in path for included in zip_contents) |
| 77 | + return not exclude_paths and include_paths |
| 78 | + |
| 79 | + with ZipFile(filename, "w", ZIP_STORED, strict_timestamps=False) as zip: |
| 80 | + for root, _, files in os.walk(basepath): |
| 81 | + for file in files: |
| 82 | + full_path = os.path.join(root, file) |
| 83 | + if should_include(full_path): |
| 84 | + arcname = os.path.relpath(full_path, basepath) |
| 85 | + zip.write(full_path, arcname) |
| 86 | + |
| 87 | + |
| 88 | +def main(): |
| 89 | + workflow_name = get_workflow_name() |
| 90 | + workflow_version = get_workflow_version() |
| 91 | + pyproject_version = get_pyproject_version() |
| 92 | + |
| 93 | + init_venv() |
| 94 | + |
| 95 | + if workflow_version != pyproject_version: |
| 96 | + update_version(pyproject_version) |
| 97 | + else: |
| 98 | + print("Workflow version matches PyProject version. Should this be updated?") |
| 99 | + |
| 100 | + zip_name = f"{workflow_name}-{workflow_version}.alfredworkflow" |
| 101 | + zip_workflow(zip_name) |
| 102 | + |
| 103 | + if os.getenv("GITHUB_ACTIONS"): |
| 104 | + with open(os.environ["GITHUB_OUTPUT"], "a") as f: |
| 105 | + f.write(f"OUTPUT_FILE={zip_name}\n") |
| 106 | + |
| 107 | + with open(os.environ["GITHUB_STEP_SUMMARY"], "a") as f: |
| 108 | + f.write("# Alfred Workflow Build\n") |
| 109 | + f.write(f"* Workflow name: {workflow_name}\n") |
| 110 | + f.write(f"* Workflow version: {workflow_version}\n") |
| 111 | + f.write(f"* Pyproject version: {pyproject_version}\n") |
| 112 | + f.write(f"* ZIP name: {zip_name}\n") |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments