|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Build script for creating amacOS (.app) bundle |
| 4 | +Run this *on macOS* - PyInstaller cannot cross-compile |
| 5 | +""" |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import os |
| 9 | +import platform |
| 10 | +from pathlib import Path |
| 11 | +import argparse |
| 12 | +import shutil |
| 13 | +import tempfile |
| 14 | + |
| 15 | +def build_mac(codesign=False, identity=None, entitlements=None, notarize=False, team_id=None, apple_id=None, asc_password=None): |
| 16 | + project_root = Path(__file__).parent.parent |
| 17 | + os.chdir(project_root) |
| 18 | + |
| 19 | + dist_dir = project_root / "dist" / "mac" |
| 20 | + build_dir = project_root / "build" / "mac" |
| 21 | + |
| 22 | + shutil.rmtree(dist_dir, ignore_errors=True) |
| 23 | + shutil.rmtree(build_dir, ignore_errors=True) |
| 24 | + |
| 25 | + cmd = [ |
| 26 | + "python", "-m", "PyInstaller", |
| 27 | + "--name=PlantUML2DrawIO", |
| 28 | + "--windowed", |
| 29 | + "--onedir", |
| 30 | + "--clean", |
| 31 | + "--noconfirm", |
| 32 | + "--paths=src", |
| 33 | + "--add-data=src/plantuml2drawio:plantuml2drawio", |
| 34 | + "--hidden-import=plantuml2drawio", |
| 35 | + "--icon=resources/icons/p2d_icon.icns", |
| 36 | + f"--distpath={dist_dir}", |
| 37 | + f"--workpath={build_dir}", |
| 38 | + "src/plantuml2drawio/app.py" |
| 39 | + ] |
| 40 | + |
| 41 | + print("Building macOS application bundle…") |
| 42 | + print("Command:", " ".join(cmd)) |
| 43 | + |
| 44 | + try: |
| 45 | + subprocess.run(cmd, check=True) |
| 46 | + app_path = dist_dir / "PlantUML2DrawIO.app" |
| 47 | + print(f"Built: {app_path}") |
| 48 | + |
| 49 | + if codesign: |
| 50 | + sign_app(app_path, identity, entitlements) |
| 51 | + if notarize: |
| 52 | + notarize_app(app_path, team_id, apple_id, asc_password) |
| 53 | + |
| 54 | + except subprocess.CalledProcessError as e: |
| 55 | + print("Build failed") |
| 56 | + print(e.stdout or "", e.stderr or "") |
| 57 | + sys.exit(1) |
| 58 | + |
| 59 | +def sign_app(app_path, identity, entitlements=None): |
| 60 | + """Code-sign the .app so Gatekeeper lets it run on other Macs.""" |
| 61 | + if not identity: |
| 62 | + print("--codesign supplied but no --identity given; skipping codesign.") |
| 63 | + return |
| 64 | + cmd = [ |
| 65 | + "codesign", "--deep", "--force", |
| 66 | + "--options", "runtime", |
| 67 | + "--sign", identity, |
| 68 | + str(app_path) |
| 69 | + ] |
| 70 | + if entitlements: |
| 71 | + cmd.extend(["--entitlements", str(entitlements)]) |
| 72 | + |
| 73 | + print("Signing…") |
| 74 | + subprocess.run(cmd, check=True) |
| 75 | + print("Signed") |
| 76 | + |
| 77 | +def notarize_app(app_path, team_id, apple_id, asc_password): |
| 78 | + """Submit the .app (zipped) to Apple notarization and staple the ticket.""" |
| 79 | + if not (team_id and apple_id and asc_password): |
| 80 | + print("⚠️ --notarize needs --team-id, --apple-id and --asc-password.") |
| 81 | + return |
| 82 | + |
| 83 | + zip_path = Path(tempfile.gettempdir()) / (app_path.stem + ".zip") |
| 84 | + shutil.make_archive(zip_path.with_suffix(""), 'zip', root_dir=app_path) |
| 85 | + print("Uploading to Apple notarization service…") |
| 86 | + |
| 87 | + submit = [ |
| 88 | + "xcrun", "notarytool", |
| 89 | + "submit", str(zip_path), |
| 90 | + "--team-id", team_id, |
| 91 | + "--apple-id", apple_id, |
| 92 | + "--password", asc_password, |
| 93 | + "--wait" # waits until approval / failure |
| 94 | + ] |
| 95 | + subprocess.run(submit, check=True) |
| 96 | + |
| 97 | + print("Stapling ticket…") |
| 98 | + subprocess.run(["xcrun", "stapler", "staple", str(app_path)], check=True) |
| 99 | + print("Notarized & stapled") |
| 100 | + |
| 101 | +if __name__ == "__main__": |
| 102 | + if platform.system() != "Darwin": |
| 103 | + sys.exit("⚠️ This script must be run on macOS (Darwin).") |
| 104 | + |
| 105 | + p = argparse.ArgumentParser(description="Build macOS .app bundle") |
| 106 | + p.add_argument("--codesign", action="store_true", |
| 107 | + help="Code-sign the resulting .app") |
| 108 | + p.add_argument("--identity", help="Developer ID Application identity") |
| 109 | + p.add_argument("--entitlements", help="Path to entitlements.plist") |
| 110 | + p.add_argument("--notarize", action="store_true", |
| 111 | + help="After signing, notarize the app with Apple") |
| 112 | + p.add_argument("--team-id", help="Apple Team ID (10 chars)") |
| 113 | + p.add_argument("--apple-id", help="Apple ID used for notarization") |
| 114 | + p.add_argument("--asc-password",help="App-specific password or keychain item") |
| 115 | + |
| 116 | + args = p.parse_args() |
| 117 | + build_mac(args.codesign, args.identity, args.entitlements, |
| 118 | + args.notarize, args.team_id, args.apple_id, args.asc_password) |
0 commit comments