From aceca8b5dfc8edd378ee9ff891bccb0f94c69f31 Mon Sep 17 00:00:00 2001 From: gigagamerchaddhruv-spec Date: Wed, 14 Oct 2026 20:18:20 +0530 Subject: feat: launch Bubomb - native macOS menu bar utility ('Configure the boom') --- Sources/BubombCore/Executors/ScriptExecutor.swift | 77 +++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 Sources/BubombCore/Executors/ScriptExecutor.swift (limited to 'Sources/BubombCore/Executors/ScriptExecutor.swift') diff --git a/Sources/BubombCore/Executors/ScriptExecutor.swift b/Sources/BubombCore/Executors/ScriptExecutor.swift new file mode 100644 index 0000000..55be037 --- /dev/null +++ b/Sources/BubombCore/Executors/ScriptExecutor.swift @@ -0,0 +1,77 @@ +import Foundation + +/// Executes local scripts safely without mutating file permissions or user scripts. +public final class ScriptExecutor: @unchecked Sendable { + private let runner = ProcessRunner() + + public init() {} + + public func execute(config: ScriptConfig) async throws -> ExecutionResult { + let validPath = try PathValidator.validateScript(at: config.scriptPath).get() + + if let workDir = config.workingDirectory { + _ = try PathValidator.validateWorkingDirectory(workDir).get() + } + + let isExecutable = FileManager.default.isExecutableFile(atPath: validPath) + let (interpreterURL, leadingArgs) = resolveInterpreter(forScriptAt: validPath, isExecutable: isExecutable) + + var finalArgs = leadingArgs + finalArgs.append(validPath) + finalArgs.append(contentsOf: config.arguments) + + return try await runner.run( + executableURL: interpreterURL, + arguments: finalArgs, + workingDirectory: config.workingDirectory, + environment: config.environmentVariables, + timeoutSeconds: config.timeoutSeconds + ) + } + + public func cancel() { + runner.cancel() + } + + /// Determines the appropriate interpreter for a script based on its shebang or file extension. + private func resolveInterpreter(forScriptAt path: String, isExecutable: Bool) -> (interpreterURL: URL, leadingArgs: [String]) { + // First check shebang + if let fileHandle = FileHandle(forReadingAtPath: path) { + defer { try? fileHandle.close() } + let headerData = fileHandle.readData(ofLength: 256) + if let headerString = String(data: headerData, encoding: .utf8), headerString.hasPrefix("#!") { + let firstLine = headerString.components(separatedBy: .newlines).first ?? "" + let shebangBody = firstLine.dropFirst(2).trimmingCharacters(in: .whitespaces) + let tokens = shebangBody.components(separatedBy: .whitespaces).filter { !$0.isEmpty } + if let interpreter = tokens.first { + if interpreter == "/usr/bin/env" && tokens.count > 1 { + let actualBin = tokens[1] + return (URL(fileURLWithPath: "/usr/bin/env"), [actualBin]) + } else if FileManager.default.isExecutableFile(atPath: interpreter) { + return (URL(fileURLWithPath: interpreter), Array(tokens.dropFirst())) + } + } + } + } + + // If file is directly marked executable and no shebang, run directly + if isExecutable { + return (URL(fileURLWithPath: path), []) + } + + // Fallback by extension + let ext = (path as NSString).pathExtension.lowercased() + switch ext { + case "sh", "zsh", "bash": + return (URL(fileURLWithPath: "/bin/zsh"), []) + case "py": + return (URL(fileURLWithPath: "/usr/bin/python3"), []) + case "rb": + return (URL(fileURLWithPath: "/usr/bin/ruby"), []) + case "pl": + return (URL(fileURLWithPath: "/usr/bin/perl"), []) + default: + return (URL(fileURLWithPath: "/bin/sh"), []) + } + } +} -- cgit v1.2.3