summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Executors/ScriptExecutor.swift
blob: 55be037b4a88a7b7f81084d9d78615f1a9ff6fbe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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"), [])
        }
    }
}