summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Executors/ScriptExecutor.swift
diff options
context:
space:
mode:
authorgigagamerchaddhruv-spec <gigagamerchaddhruv-spec@users.noreply.github.com>2026-10-14 20:18:20 +0530
committergigagamerchaddhruv-spec <gigagamerchaddhruv-spec@users.noreply.github.com>2026-10-14 20:18:20 +0530
commitaceca8b5dfc8edd378ee9ff891bccb0f94c69f31 (patch)
tree08401ce8d050afbbe77514f479601a8e9535f4ec /Sources/BubombCore/Executors/ScriptExecutor.swift
feat: launch Bubomb - native macOS menu bar utility ('Configure the boom')HEADmain
Diffstat (limited to 'Sources/BubombCore/Executors/ScriptExecutor.swift')
-rw-r--r--Sources/BubombCore/Executors/ScriptExecutor.swift77
1 files changed, 77 insertions, 0 deletions
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"), [])
+ }
+ }
+}