From 969d69e89548c2921809e5eb9becc3e1e33b909d Mon Sep 17 00:00:00 2001 From: Isaac Marovitz Date: Fri, 12 Jan 2024 11:46:58 -0500 Subject: [PATCH] Add ability to run program from WhiskyCmd --- Whisky.xcodeproj/project.pbxproj | 4 --- WhiskyCmd/Main.swift | 25 ++++++++++++++++++- .../Extensions}/Program+Extensions.swift | 7 +++--- 3 files changed, 27 insertions(+), 9 deletions(-) rename {Whisky/Models => WhiskyKit/Sources/WhiskyKit/Extensions}/Program+Extensions.swift (96%) diff --git a/Whisky.xcodeproj/project.pbxproj b/Whisky.xcodeproj/project.pbxproj index b2bb8e82..1072d5ed 100644 --- a/Whisky.xcodeproj/project.pbxproj +++ b/Whisky.xcodeproj/project.pbxproj @@ -20,7 +20,6 @@ 6E355E5829D78249002D83BE /* ConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E355E5729D78249002D83BE /* ConfigView.swift */; }; 6E355E5A29D782B2002D83BE /* ProgramsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E355E5929D782B2002D83BE /* ProgramsView.swift */; }; 6E355E5E29D7D85D002D83BE /* ProgramView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E355E5D29D7D85D002D83BE /* ProgramView.swift */; }; - 6E355E6029D7D8BD002D83BE /* Program+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E355E5F29D7D8BD002D83BE /* Program+Extensions.swift */; }; 6E40495629CCA19C006E3F1B /* WhiskyApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E40495529CCA19C006E3F1B /* WhiskyApp.swift */; }; 6E40495829CCA19C006E3F1B /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6E40495729CCA19C006E3F1B /* ContentView.swift */; }; 6E40495A29CCA19C006E3F1B /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 6E40495929CCA19C006E3F1B /* Assets.xcassets */; }; @@ -118,7 +117,6 @@ 6E355E5729D78249002D83BE /* ConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ConfigView.swift; sourceTree = ""; }; 6E355E5929D782B2002D83BE /* ProgramsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgramsView.swift; sourceTree = ""; }; 6E355E5D29D7D85D002D83BE /* ProgramView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProgramView.swift; sourceTree = ""; }; - 6E355E5F29D7D8BD002D83BE /* Program+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Program+Extensions.swift"; sourceTree = ""; }; 6E40495229CCA19C006E3F1B /* Whisky.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Whisky.app; sourceTree = BUILT_PRODUCTS_DIR; }; 6E40495529CCA19C006E3F1B /* WhiskyApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WhiskyApp.swift; sourceTree = ""; }; 6E40495729CCA19C006E3F1B /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = ""; }; @@ -329,7 +327,6 @@ isa = PBXGroup; children = ( 6E40498229CCA91B006E3F1B /* Bottle+Extensions.swift */, - 6E355E5F29D7D8BD002D83BE /* Program+Extensions.swift */, ); path = Models; sourceTree = ""; @@ -602,7 +599,6 @@ 6E2B25C12B0E20F50084A67A /* PinAddView.swift in Sources */, 6E49E0212AECB7DB00009CAC /* SettingsView.swift in Sources */, 6E7C07C02AAF570100F6E66B /* FileOpenView.swift in Sources */, - 6E355E6029D7D8BD002D83BE /* Program+Extensions.swift in Sources */, 6E6C0CF22A419A6800356232 /* WelcomeView.swift in Sources */, 6E40498129CCA8B0006E3F1B /* BottleView.swift in Sources */, 6E355E5E29D7D85D002D83BE /* ProgramView.swift in Sources */, diff --git a/WhiskyCmd/Main.swift b/WhiskyCmd/Main.swift index 45473795..bdab8450 100644 --- a/WhiskyCmd/Main.swift +++ b/WhiskyCmd/Main.swift @@ -32,7 +32,8 @@ struct Whisky: ParsableCommand { Add.self, // Export.self, Delete.self, - Remove.self + Remove.self, + Run.self /*Install.self, Uninstall.self*/]) } @@ -156,6 +157,28 @@ extension Whisky { } } + struct Run: ParsableCommand { + static var configuration = CommandConfiguration(abstract: "Run a program with Whisky.") + + @Argument var bottleName: String + @Argument var path: String + @Argument var args: [String] = [] + + mutating func run() throws { + var bottlesList = BottleData() + let bottles = bottlesList.loadBottles() + + guard let bottle = bottles.first(where: { $0.settings.name == bottleName }) else { + print("A bottle with that name doesn't exist.") + return + } + + let url = URL(fileURLWithPath: path) + let program = Program(url: url, bottle: bottle) + program.runInTerminal() + } + } + struct Install: ParsableCommand { static var configuration = CommandConfiguration(abstract: "Install Whisky dependencies.") diff --git a/Whisky/Models/Program+Extensions.swift b/WhiskyKit/Sources/WhiskyKit/Extensions/Program+Extensions.swift similarity index 96% rename from Whisky/Models/Program+Extensions.swift rename to WhiskyKit/Sources/WhiskyKit/Extensions/Program+Extensions.swift index a41a7298..055b372f 100644 --- a/Whisky/Models/Program+Extensions.swift +++ b/WhiskyKit/Sources/WhiskyKit/Extensions/Program+Extensions.swift @@ -18,11 +18,10 @@ import Foundation import AppKit -import WhiskyKit import os.log extension Program { - func run() { + public func run() { if NSEvent.modifierFlags.contains(.shift) { self.runInTerminal() } else { @@ -47,11 +46,11 @@ extension Program { } } - func generateTerminalCommand() -> String { + public func generateTerminalCommand() -> String { return Wine.generateRunCommand(bottle: bottle, args: settings.arguments, environment: generateEnvironment()) } - func runInTerminal() { + public func runInTerminal() { let wineCmd = generateTerminalCommand().replacingOccurrences(of: "\\", with: "\\\\") let script = """