summaryrefslogtreecommitdiff
path: root/Sources/BubombCore/Executors/ApplicationLauncher.swift
diff options
context:
space:
mode:
Diffstat (limited to 'Sources/BubombCore/Executors/ApplicationLauncher.swift')
-rw-r--r--Sources/BubombCore/Executors/ApplicationLauncher.swift78
1 files changed, 78 insertions, 0 deletions
diff --git a/Sources/BubombCore/Executors/ApplicationLauncher.swift b/Sources/BubombCore/Executors/ApplicationLauncher.swift
new file mode 100644
index 0000000..27bfd1b
--- /dev/null
+++ b/Sources/BubombCore/Executors/ApplicationLauncher.swift
@@ -0,0 +1,78 @@
+import Foundation
+#if canImport(AppKit)
+import AppKit
+#endif
+
+/// Launches macOS applications safely using native APIs.
+public final class ApplicationLauncher: Sendable {
+ public init() {}
+
+ public func launch(appPath: String, arguments: [String] = []) async throws -> ExecutionResult {
+ let startTime = Date()
+ let validatedPath = try PathValidator.validateApplication(at: appPath).get()
+ let appURL = URL(fileURLWithPath: validatedPath)
+
+ #if canImport(AppKit)
+ let config = NSWorkspace.OpenConfiguration()
+ if !arguments.isEmpty {
+ config.arguments = arguments
+ }
+
+ do {
+ _ = try await withCheckedThrowingContinuation { (continuation: CheckedContinuation<NSRunningApplication, Error>) in
+ NSWorkspace.shared.openApplication(at: appURL, configuration: config) { app, error in
+ if let error = error {
+ continuation.resume(throwing: error)
+ } else if let app = app {
+ continuation.resume(returning: app)
+ } else {
+ continuation.resume(throwing: ExecutionError.applicationLaunchFailed("Application launch failed without error info."))
+ }
+ }
+ }
+ let duration = Date().timeIntervalSince(startTime)
+ return ExecutionResult(
+ exitCode: 0,
+ stdout: "Successfully launched application: \(appURL.lastPathComponent)",
+ stderr: "",
+ duration: duration
+ )
+ } catch {
+ // If native NSWorkspace openApplication threw, try /usr/bin/open as fallback
+ let runner = ProcessRunner()
+ var args = ["-a", validatedPath]
+ if !arguments.isEmpty {
+ args.append("--args")
+ args.append(contentsOf: arguments)
+ }
+ let res = try await runner.run(
+ executableURL: URL(fileURLWithPath: "/usr/bin/open"),
+ arguments: args
+ )
+ return ExecutionResult(
+ exitCode: res.exitCode,
+ stdout: "Launched via open tool: \(appURL.lastPathComponent)",
+ stderr: res.stderr,
+ duration: Date().timeIntervalSince(startTime)
+ )
+ }
+ #else
+ let runner = ProcessRunner()
+ var args = ["-a", validatedPath]
+ if !arguments.isEmpty {
+ args.append("--args")
+ args.append(contentsOf: arguments)
+ }
+ let res = try await runner.run(
+ executableURL: URL(fileURLWithPath: "/usr/bin/open"),
+ arguments: args
+ )
+ return ExecutionResult(
+ exitCode: res.exitCode,
+ stdout: "Launched via open tool: \(appURL.lastPathComponent)",
+ stderr: res.stderr,
+ duration: Date().timeIntervalSince(startTime)
+ )
+ #endif
+ }
+}