|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +import json |
| 4 | +import subprocess |
| 5 | +from pathlib import Path |
| 6 | +from uuid import uuid4 |
| 7 | +from zipfile import ZipFile |
| 8 | + |
| 9 | +import click |
| 10 | + |
| 11 | +FILENAME_LIST = ( |
| 12 | + "info.plist", |
| 13 | + "entry_point.py", |
| 14 | + "core.py", |
| 15 | +) |
| 16 | + |
| 17 | +PATH_LIST = ( |
| 18 | + "arrow", |
| 19 | + "backports", |
| 20 | + "dateutil", |
| 21 | + "ualfred", |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +# def main(): |
| 26 | +# with open("version") as f: |
| 27 | +# version = f.readline().rstrip("\n\r ") |
| 28 | +# |
| 29 | +# zip_filename = f"time-converter.{version}.alfredworkflow" |
| 30 | +# z = ZipFile(zip_filename, mode="w") |
| 31 | +# |
| 32 | +# for filename in FILENAME_LIST: |
| 33 | +# z.write(filename) |
| 34 | +# |
| 35 | +# for pathname in PATH_LIST: |
| 36 | +# for root, dirs, files in walk(pathname): |
| 37 | +# for filename in files: |
| 38 | +# if filename.rfind(".pyc") == -1: |
| 39 | +# z.write(join(root, filename)) |
| 40 | +# |
| 41 | +# z.close() |
| 42 | +# |
| 43 | +# print(f"Create Alfred workflow({zip_filename}) finished.") |
| 44 | + |
| 45 | + |
| 46 | +@click.group() |
| 47 | +def cli(): |
| 48 | + pass |
| 49 | + |
| 50 | + |
| 51 | +@cli.command() |
| 52 | +@click.argument("workflow_path") |
| 53 | +def build(workflow_path: str): |
| 54 | + workflow_path = Path(".").joinpath(workflow_path) |
| 55 | + |
| 56 | + # load package info |
| 57 | + with open(workflow_path.joinpath("package.json")) as f: |
| 58 | + package_info = json.load(f) |
| 59 | + |
| 60 | + # build package |
| 61 | + build_path = Path(".").joinpath("build").joinpath(uuid4().hex) |
| 62 | + build_path.mkdir(parents=True) |
| 63 | + dist_path = Path(".").joinpath("dist") |
| 64 | + dist_path.mkdir(exist_ok=True) |
| 65 | + package_path = dist_path.joinpath( |
| 66 | + f"{package_info['name']}.{package_info['version']}.alfredworkflow" |
| 67 | + ) |
| 68 | + |
| 69 | + # build package - depend |
| 70 | + print("Prepare...") |
| 71 | + requirements: list[str] = list() |
| 72 | + with open(workflow_path.joinpath("requirements.txt")) as f: |
| 73 | + for line in f.readlines(): |
| 74 | + if line.startswith("#") or len(line) == 0: |
| 75 | + continue |
| 76 | + |
| 77 | + requirements.append(line) |
| 78 | + |
| 79 | + for requirement in requirements: |
| 80 | + subprocess.run( |
| 81 | + ["pip", "install", "-U", f"--target={str(build_path)}", requirement], |
| 82 | + capture_output=True, |
| 83 | + ) |
| 84 | + |
| 85 | + # build package - zip |
| 86 | + package_file = ZipFile(package_path, mode="w") |
| 87 | + |
| 88 | + # for filename in FILENAME_LIST: |
| 89 | + # package_file.write(filename) |
| 90 | + |
| 91 | + print("Add 3rd depends files...") |
| 92 | + build_path_str_length = len(str(build_path)) + 1 |
| 93 | + for file in build_path.rglob("*"): |
| 94 | + if file.suffix == ".pyc" or file.name == "__pycache__": |
| 95 | + continue |
| 96 | + |
| 97 | + arc_name = str(file)[build_path_str_length:] |
| 98 | + print(arc_name) |
| 99 | + package_file.write(file, arcname=arc_name) |
| 100 | + |
| 101 | + print("Add workflow files...") |
| 102 | + workflow_files: list[str] = ["info.plist", "main.py", "__init__.py"] |
| 103 | + icon_file = package_info.get("icon") |
| 104 | + if icon_file: |
| 105 | + workflow_files.append(icon_file) |
| 106 | + |
| 107 | + for file in workflow_files: |
| 108 | + package_file.write(workflow_path.joinpath(file), arcname=file) |
| 109 | + |
| 110 | + package_file.write("entry_point.py") |
| 111 | + |
| 112 | + package_file.close() |
| 113 | + print(f"Create Alfred workflow[{package_path}] finished.") |
| 114 | + |
| 115 | + |
| 116 | +@cli.command() |
| 117 | +def test(): |
| 118 | + click.echo("test...todo") |
| 119 | + |
| 120 | + |
| 121 | +if __name__ == "__main__": |
| 122 | + cli() |
0 commit comments