import AppKit /// Mollusc apps: directories holding a mollusc.json under the dev roots, run with `mollusc dev`. enum MolluscApps { static let roots = [NSHomeDirectory() + "/Code"] static let maxDepth = 4 /// Directory names never worth descending into. static let pruned: Set = ["node_modules", ".git", "dist", "out", "build", "examples"] /// Keeps the Process objects alive while their dev sessions run. private static var running: [String: Process] = [:] static func scan() -> [AppEntry] { var found: [AppEntry] = [] for root in roots { walk(URL(fileURLWithPath: root), depth: 0, into: &found) } return found } private static func walk(_ dir: URL, depth: Int, into found: inout [AppEntry]) { let fm = FileManager.default let manifest = dir.appendingPathComponent("mollusc.json") if let data = try? Data(contentsOf: manifest), let json = try? JSONSerialization.jsonObject(with: data) as? [String: Any] { let name = (json["name"] as? String) ?? dir.lastPathComponent var iconFile: String? if let icon = json["icon"] as? String, fm.fileExists(atPath: dir.appendingPathComponent(icon).path) { iconFile = dir.appendingPathComponent(icon).path } let home = NSHomeDirectory() let shown = dir.path.hasPrefix(home) ? "~" + dir.path.dropFirst(home.count) : dir.path found.append(AppEntry(name: name, url: dir, kind: .mollusc, detail: "dev ยท \(shown)", iconFile: iconFile)) return // an app's own tree holds no further apps } guard depth < maxDepth, let items = try? fm.contentsOfDirectory(at: dir, includingPropertiesForKeys: [.isDirectoryKey], options: [.skipsHiddenFiles]) else { return } for item in items where !pruned.contains(item.lastPathComponent) { // isDirectory is false for symlinks, which also keeps us out of link loops. if (try? item.resourceValues(forKeys: [.isDirectoryKey]).isDirectory) == true { walk(item, depth: depth + 1, into: &found) } } } /// Runs `mollusc dev ` through the user's login shell, so PATH is the one their terminal has. /// Mollusc itself holds a single-instance lock, so launching a running app just brings it forward. static func launch(_ entry: AppEntry) { let process = Process() process.executableURL = URL(fileURLWithPath: loginShell()) process.arguments = ["-lc", "exec mollusc dev \"$0\"", entry.url.path] process.currentDirectoryURL = entry.url process.standardInput = FileHandle.nullDevice let log = logFile(for: entry) process.standardOutput = log process.standardError = log let key = entry.url.path process.terminationHandler = { _ in DispatchQueue.main.async { running[key] = nil } } do { try process.run() running[key] = process } catch { NSLog("FreeLaunch: could not start mollusc dev for \(key): \(error)") } } private static func loginShell() -> String { if let entry = getpwuid(getuid()), let shell = entry.pointee.pw_shell { return String(cString: shell) } return "/bin/zsh" } /// ~/Library/Logs/FreeLaunch/-.log, truncated on each launch. private static func logFile(for entry: AppEntry) -> FileHandle { let fm = FileManager.default let dir = fm.urls(for: .libraryDirectory, in: .userDomainMask)[0] .appendingPathComponent("Logs/FreeLaunch", isDirectory: true) try? fm.createDirectory(at: dir, withIntermediateDirectories: true) let stem = entry.url.pathComponents.suffix(2).joined(separator: "-") let file = dir.appendingPathComponent(stem + ".log") fm.createFile(atPath: file.path, contents: nil) return (try? FileHandle(forWritingTo: file)) ?? FileHandle.nullDevice } }