Last active
July 12, 2021 17:23
-
-
Save daniellivingston/626bd9ee9e71a098209f08696fd92bf2 to your computer and use it in GitHub Desktop.
Simple bootstrap script for prototyping SwiftUI apps without an Xcode project.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Simple bootstrap script for quickly prototyping | |
Swift-based macOS applications. | |
usage: swift-run.py [-h] [-c SWIFT_COMPILER] [-a ARGS] [-m MAIN] [-s SAVE] | |
file [file ...] | |
positional arguments: | |
file list of files to run | |
optional arguments: | |
-h, --help show this help message and exit | |
-c SWIFT_COMPILER, --swift-compiler SWIFT_COMPILER | |
swift compiler path (default: swift) | |
-a ARGS, --args ARGS swift compiler args (default: ) | |
-m MAIN, --main MAIN main SwiftUI view (default: | |
ContentView().frame(maxWidth: .infinity, maxHeight: | |
.infinity)) | |
-s SAVE, --save SAVE saves the joined swift files + boilerplate to this | |
file (default: ) | |
""" | |
import argparse | |
import os | |
import subprocess | |
import tempfile | |
from typing import List | |
DEFAULTS = { | |
"main_view": "ContentView().frame(maxWidth: .infinity, maxHeight: .infinity)", | |
"swift_args": "", | |
"swift_exec": "swift", | |
"window": { | |
"width": 480, | |
"height": 480, | |
"style_titled": True, | |
"style_closable": True, | |
"style_miniaturizable": True, | |
"style_resizable": True, | |
"style_fullSizeContentView": True, | |
}, | |
} | |
def join_files(files: List[str], boilerplate: str = ""): | |
all_text = "" | |
for file in files: | |
with open(file, "r") as f: | |
all_text += f.read() + "\n\n" | |
all_text += boilerplate | |
return all_text | |
def exec_text(text: str, swift_exec: str = "", swift_args: str = "", script_out: str = None): | |
with tempfile.TemporaryDirectory() as tmp_dir: | |
if script_out is None or script_out.strip() == "": | |
script_out = os.path.join(tmp_dir, "main.swift") | |
with open(script_out, "w") as f: | |
f.write(text) | |
cmd = f"{swift_exec} {swift_args} {script_out}" | |
print(cmd) | |
subprocess.run( | |
cmd.split(), | |
check=True, | |
capture_output=True, | |
) | |
def get_boilerplate(main_view: str = "ContentView()"): | |
return ( | |
""" | |
// Run any SwiftUI view as a Mac app. | |
import Cocoa | |
import SwiftUI | |
NSApplication.shared.run { | |
""" | |
+ main_view | |
+ """ | |
} | |
extension NSApplication { | |
public func run<V: View>(@ViewBuilder view: () -> V) { | |
let appDelegate = AppDelegate(view()) | |
NSApp.setActivationPolicy(.regular) | |
mainMenu = customMenu | |
delegate = appDelegate | |
run() | |
} | |
} | |
// Inspired by https://www.cocoawithlove.com/2010/09/minimalist-cocoa-programming.html | |
extension NSApplication { | |
var customMenu: NSMenu { | |
let appMenu = NSMenuItem() | |
appMenu.submenu = NSMenu() | |
let appName = ProcessInfo.processInfo.processName | |
appMenu.submenu?.addItem(NSMenuItem(title: "About \(appName)", action: #selector(NSApplication.orderFrontStandardAboutPanel(_:)), keyEquivalent: "")) | |
appMenu.submenu?.addItem(NSMenuItem.separator()) | |
let services = NSMenuItem(title: "Services", action: nil, keyEquivalent: "") | |
self.servicesMenu = NSMenu() | |
services.submenu = self.servicesMenu | |
appMenu.submenu?.addItem(services) | |
appMenu.submenu?.addItem(NSMenuItem.separator()) | |
appMenu.submenu?.addItem(NSMenuItem(title: "Hide \(appName)", action: #selector(NSApplication.hide(_:)), keyEquivalent: "h")) | |
let hideOthers = NSMenuItem(title: "Hide Others", action: #selector(NSApplication.hideOtherApplications(_:)), keyEquivalent: "h") | |
hideOthers.keyEquivalentModifierMask = [.command, .option] | |
appMenu.submenu?.addItem(hideOthers) | |
appMenu.submenu?.addItem(NSMenuItem(title: "Show All", action: #selector(NSApplication.unhideAllApplications(_:)), keyEquivalent: "")) | |
appMenu.submenu?.addItem(NSMenuItem.separator()) | |
appMenu.submenu?.addItem(NSMenuItem(title: "Quit \(appName)", action: #selector(NSApplication.terminate(_:)), keyEquivalent: "q")) | |
let windowMenu = NSMenuItem() | |
windowMenu.submenu = NSMenu(title: "Window") | |
windowMenu.submenu?.addItem(NSMenuItem(title: "Minmize", action: #selector(NSWindow.miniaturize(_:)), keyEquivalent: "m")) | |
windowMenu.submenu?.addItem(NSMenuItem(title: "Zoom", action: #selector(NSWindow.performZoom(_:)), keyEquivalent: "")) | |
windowMenu.submenu?.addItem(NSMenuItem.separator()) | |
windowMenu.submenu?.addItem(NSMenuItem(title: "Show All", action: #selector(NSApplication.arrangeInFront(_:)), keyEquivalent: "m")) | |
let mainMenu = NSMenu(title: "Main Menu") | |
mainMenu.addItem(appMenu) | |
mainMenu.addItem(windowMenu) | |
return mainMenu | |
} | |
} | |
class AppDelegate<V: View>: NSObject, NSApplicationDelegate, NSWindowDelegate { | |
init(_ contentView: V) { | |
self.contentView = contentView | |
} | |
var window: NSWindow! | |
var hostingView: NSView? | |
var contentView: V | |
func applicationDidFinishLaunching(_ notification: Notification) { | |
window = NSWindow( | |
contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), | |
styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], | |
backing: .buffered, defer: false) | |
window.center() | |
window.setFrameAutosaveName("Main Window") | |
hostingView = NSHostingView(rootView: contentView) | |
window.contentView = hostingView | |
window.makeKeyAndOrderFront(nil) | |
window.delegate = self | |
NSApp.activate(ignoringOtherApps: true) | |
} | |
} | |
""" | |
) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument( | |
"files", metavar="file", type=str, nargs="+", help="list of files to run" | |
) | |
parser.add_argument( | |
"-c", | |
"--swift-compiler", | |
help="swift compiler path", | |
type=str, | |
default=DEFAULTS["swift_exec"], | |
) | |
parser.add_argument( | |
"-a", | |
"--args", | |
help="swift compiler args", | |
type=str, | |
default=DEFAULTS["swift_args"], | |
) | |
parser.add_argument( | |
"-m", | |
"--main", | |
help="main SwiftUI view", | |
type=str, | |
default=DEFAULTS["main_view"], | |
) | |
parser.add_argument( | |
"-s", | |
"--save", | |
help="saves the joined swift files + boilerplate to this file", | |
type=str, | |
default="", | |
) | |
args = parser.parse_args() | |
print(args) | |
exec_text( | |
join_files(args.files, boilerplate=get_boilerplate(args.main)), | |
swift_exec=args.swift_compiler, | |
swift_args=args.args, | |
script_out=args.save | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment