From 3087ab10577313f493c87c8b60c7f6382baaaefc Mon Sep 17 00:00:00 2001 From: Gokulakrishnan Date: Sun, 26 Nov 2023 18:19:15 +0530 Subject: [PATCH 1/7] added logcat feature --- .../UserDefaults+Configuration.swift | 6 +++ MiniSim/MenuItems/SubMenuItem.swift | 16 ++++++- MiniSim/Service/DeviceService.swift | 12 +++++ MiniSim/Service/Terminal.swift | 48 +++++++++++++++++++ 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 MiniSim/Service/Terminal.swift diff --git a/MiniSim/Extensions/UserDefaults+Configuration.swift b/MiniSim/Extensions/UserDefaults+Configuration.swift index f95cb4e..cdc0086 100644 --- a/MiniSim/Extensions/UserDefaults+Configuration.swift +++ b/MiniSim/Extensions/UserDefaults+Configuration.swift @@ -15,6 +15,7 @@ extension UserDefaults { static let isOnboardingFinished = "isOnboardingFinished" static let enableiOSSimulators = "enableiOSSimulators" static let enableAndroidEmulators = "enableAndroidEmulators" + static let preferedTerminal = "preferedTerminal" } @objc public dynamic var androidHome: String? { @@ -46,4 +47,9 @@ extension UserDefaults { get { bool(forKey: Keys.enableAndroidEmulators) } set { set(newValue, forKey: Keys.enableAndroidEmulators) } } + + @objc public dynamic var preferedTerminal: String? { + get { string(forKey: Keys.preferedTerminal) } + set { set(newValue, forKey: Keys.preferedTerminal) } + } } diff --git a/MiniSim/MenuItems/SubMenuItem.swift b/MiniSim/MenuItems/SubMenuItem.swift index 415846a..3b43024 100644 --- a/MiniSim/MenuItems/SubMenuItem.swift +++ b/MiniSim/MenuItems/SubMenuItem.swift @@ -27,6 +27,7 @@ enum SubMenuItems { case toggleA11y case paste case delete + case logcat case customCommand = 200 } @@ -130,6 +131,17 @@ enum SubMenuItems { accessibilityDescription: "Delete emulator" ) } + + struct LaunchLogCat: SubMenuActionItem { + let title = NSLocalizedString("Launch Logcat", comment: "") + let tag = Tags.logcat.rawValue + let bootsDevice = false + let needBootedDevice = true + let image = NSImage( + systemSymbolName: "trash", + accessibilityDescription: "Launch Logcat" + ) + } } extension SubMenuItems { @@ -143,7 +155,8 @@ extension SubMenuItems { NoAudio(), ToggleA11y(), Paste(), - DeleteEmulator() + DeleteEmulator(), + LaunchLogCat() ] static var ios: [SubMenuItem] = [ @@ -151,7 +164,6 @@ extension SubMenuItems { CopyUDID(), Separator(), - Delete() ] } diff --git a/MiniSim/Service/DeviceService.swift b/MiniSim/Service/DeviceService.swift index ab0424b..4893097 100644 --- a/MiniSim/Service/DeviceService.swift +++ b/MiniSim/Service/DeviceService.swift @@ -425,6 +425,15 @@ extension DeviceService { try shellOut(to: "\(avdPath) delete avd -n \"\(device.name)\"") } + static func launchLogCat(device: Device) throws { + if device.booted { + guard let deviceId = device.identifier else { + throw DeviceError.deviceNotFound + } + let preferedTermial = TerminalType(rawValue: UserDefaults.standard.preferedTerminal ?? "Terminal") + try TerminalService.launchTerminal(type: preferedTermial!, deviceId: deviceId) + } + } static func handleAndroidAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) { do { switch commandTag { @@ -458,6 +467,8 @@ extension DeviceService { if let command = DeviceService.getCustomCommand(platform: .android, commandName: itemName) { try DeviceService.runCustomCommand(device, command: command) } + case .logcat: + try DeviceService.launchLogCat(device: device) case .delete: let result = !NSAlert.showQuestionDialog( @@ -474,6 +485,7 @@ extension DeviceService { NSAlert.showError(message: error.localizedDescription) } } + default: break } diff --git a/MiniSim/Service/Terminal.swift b/MiniSim/Service/Terminal.swift new file mode 100644 index 0000000..9b0bdf4 --- /dev/null +++ b/MiniSim/Service/Terminal.swift @@ -0,0 +1,48 @@ +// +// Terminal.swift +// MiniSim +// +// Created by Gokulakrishnan Subramaniyan on 26/11/23. +// + +import Foundation +import ShellOut + +enum TerminalType: String { + case terminal = "Terminal" + case iterm = "iTerm" +} +protocol TerminalServiceProtocol { + static func getScript(type: TerminalType, deviceId: String) -> String + static func launchTerminal(type: TerminalType, deviceId: String) throws +} + +class TerminalService: TerminalServiceProtocol { + static func getScript(type: TerminalType, deviceId: String) -> String { + let logcatCommand = "adb -s \(deviceId) logcat -v color" + switch type { + case .terminal: + return """ + tell app \"Terminal\" + do script \"\(logcatCommand)\" + end tell + """ + case .iterm: + return """ + tell app \"iTerm\" + set newWindow to (create window with default profile) + tell current session of newWindow + write text \"\(logcatCommand)\" + end tell + end tell + """ + default: + return "" + } + } + + static func launchTerminal(type: TerminalType, deviceId: String) throws { + let script = getScript(type: type, deviceId: deviceId) + try shellOut(to: "osascript -e '\(script)'") + } +} From adc1fa70b4d10f1977e36342c4f721d6b1a7ba3f Mon Sep 17 00:00:00 2001 From: Gokulakrishnan Date: Sun, 26 Nov 2023 18:29:41 +0530 Subject: [PATCH 2/7] icon change --- MiniSim/MenuItems/SubMenuItem.swift | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MiniSim/MenuItems/SubMenuItem.swift b/MiniSim/MenuItems/SubMenuItem.swift index 3b43024..02368c9 100644 --- a/MiniSim/MenuItems/SubMenuItem.swift +++ b/MiniSim/MenuItems/SubMenuItem.swift @@ -133,12 +133,12 @@ enum SubMenuItems { } struct LaunchLogCat: SubMenuActionItem { - let title = NSLocalizedString("Launch Logcat", comment: "") + let title = NSLocalizedString("Launch logcat", comment: "") let tag = Tags.logcat.rawValue let bootsDevice = false let needBootedDevice = true let image = NSImage( - systemSymbolName: "trash", + systemSymbolName: "terminal", accessibilityDescription: "Launch Logcat" ) } From d128198efb62d31e6307c33133dc2a8019569b9b Mon Sep 17 00:00:00 2001 From: Gokulakrishnan Date: Sun, 26 Nov 2023 19:06:44 +0530 Subject: [PATCH 3/7] added comment --- MiniSim/Extensions/UserDefaults+Configuration.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MiniSim/Extensions/UserDefaults+Configuration.swift b/MiniSim/Extensions/UserDefaults+Configuration.swift index cdc0086..3959f73 100644 --- a/MiniSim/Extensions/UserDefaults+Configuration.swift +++ b/MiniSim/Extensions/UserDefaults+Configuration.swift @@ -47,7 +47,7 @@ extension UserDefaults { get { bool(forKey: Keys.enableAndroidEmulators) } set { set(newValue, forKey: Keys.enableAndroidEmulators) } } - +// Need to add ui for changing the terminal preference @objc public dynamic var preferedTerminal: String? { get { string(forKey: Keys.preferedTerminal) } set { set(newValue, forKey: Keys.preferedTerminal) } From 087d219fedf7360cab8d6417d8a7d37104381e3b Mon Sep 17 00:00:00 2001 From: Gokulakrishnan Date: Sun, 26 Nov 2023 19:28:59 +0530 Subject: [PATCH 4/7] added new file Terminal.swift --- MiniSim.xcodeproj/project.pbxproj | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/MiniSim.xcodeproj/project.pbxproj b/MiniSim.xcodeproj/project.pbxproj index 2b61d83..56eb1e4 100644 --- a/MiniSim.xcodeproj/project.pbxproj +++ b/MiniSim.xcodeproj/project.pbxproj @@ -13,6 +13,7 @@ 52B363EA2AEC0D3D006F515C /* ParametersTableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52B363E92AEC0D3D006F515C /* ParametersTableViewModel.swift */; }; 52B363EC2AEC10A3006F515C /* ParametersTableForm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52B363EB2AEC10A3006F515C /* ParametersTableForm.swift */; }; 52B363EE2AEC10B3006F515C /* ParametersTableFormViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52B363ED2AEC10B3006F515C /* ParametersTableFormViewModel.swift */; }; + 551B882A2B1385E900B8D325 /* Terminal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551B88292B1385E900B8D325 /* Terminal.swift */; }; 76059BF52AD4361C0008D38B /* SetupPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF42AD4361C0008D38B /* SetupPreferences.swift */; }; 76059BF72AD449DC0008D38B /* OnboardingHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF62AD449DC0008D38B /* OnboardingHeader.swift */; }; 76059BF92AD558C30008D38B /* SetupItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF82AD558C30008D38B /* SetupItemView.swift */; }; @@ -100,6 +101,7 @@ 52B363E92AEC0D3D006F515C /* ParametersTableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableViewModel.swift; sourceTree = ""; }; 52B363EB2AEC10A3006F515C /* ParametersTableForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableForm.swift; sourceTree = ""; }; 52B363ED2AEC10B3006F515C /* ParametersTableFormViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableFormViewModel.swift; sourceTree = ""; }; + 551B88292B1385E900B8D325 /* Terminal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Terminal.swift; sourceTree = ""; }; 76059BF42AD4361C0008D38B /* SetupPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupPreferences.swift; sourceTree = ""; }; 76059BF62AD449DC0008D38B /* OnboardingHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingHeader.swift; sourceTree = ""; }; 76059BF82AD558C30008D38B /* SetupItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupItemView.swift; sourceTree = ""; }; @@ -265,6 +267,7 @@ 76F269832A2A375900424BDA /* CustomErrors */, 7645D4BD2982A1B100019227 /* DeviceService.swift */, 76F04A10298A5AE000BF9CA3 /* ADB.swift */, + 551B88292B1385E900B8D325 /* Terminal.swift */, ); path = Service; sourceTree = ""; @@ -541,6 +544,7 @@ 7645D4C22982CA9600019227 /* AppDelegate.swift in Sources */, 76F04A11298A5AE000BF9CA3 /* ADB.swift in Sources */, 76730BB9298C1DF80019C680 /* DeviceMenuItem.swift in Sources */, + 551B882A2B1385E900B8D325 /* Terminal.swift in Sources */, 767A9CC929C320ED00554193 /* OnboardingButton.swift in Sources */, 7677999C29C26240009030F8 /* SetupView.swift in Sources */, 76F269852A2A376A00424BDA /* CustomCommandError.swift in Sources */, From 722601ba6060830440b9ab54ac7febbf0e052210 Mon Sep 17 00:00:00 2001 From: Gokulakrishnan Date: Sun, 3 Dec 2023 15:48:36 +0530 Subject: [PATCH 5/7] review changes --- MiniSim.xcodeproj/project.pbxproj | 14 +++++- .../UserDefaults+Configuration.swift | 2 +- MiniSim/MenuItems/SubMenuItem.swift | 1 + MiniSim/MiniSim.swift | 3 +- MiniSim/Service/DeviceService.swift | 12 ++--- MiniSim/Service/Terminal.swift | 48 ------------------- MiniSim/Service/Terminal/Terminal.swift | 35 ++++++++++++++ MiniSim/Service/Terminal/TerminalApps.swift | 40 ++++++++++++++++ MiniSimTests/UserDefaultsTests.swift | 6 +++ 9 files changed, 104 insertions(+), 57 deletions(-) delete mode 100644 MiniSim/Service/Terminal.swift create mode 100644 MiniSim/Service/Terminal/Terminal.swift create mode 100644 MiniSim/Service/Terminal/TerminalApps.swift diff --git a/MiniSim.xcodeproj/project.pbxproj b/MiniSim.xcodeproj/project.pbxproj index 56eb1e4..e4cb97e 100644 --- a/MiniSim.xcodeproj/project.pbxproj +++ b/MiniSim.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 52B363EC2AEC10A3006F515C /* ParametersTableForm.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52B363EB2AEC10A3006F515C /* ParametersTableForm.swift */; }; 52B363EE2AEC10B3006F515C /* ParametersTableFormViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 52B363ED2AEC10B3006F515C /* ParametersTableFormViewModel.swift */; }; 551B882A2B1385E900B8D325 /* Terminal.swift in Sources */ = {isa = PBXBuildFile; fileRef = 551B88292B1385E900B8D325 /* Terminal.swift */; }; + 55CDB0782B1B6D24002418D7 /* TerminalApps.swift in Sources */ = {isa = PBXBuildFile; fileRef = 55CDB0772B1B6D24002418D7 /* TerminalApps.swift */; }; 76059BF52AD4361C0008D38B /* SetupPreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF42AD4361C0008D38B /* SetupPreferences.swift */; }; 76059BF72AD449DC0008D38B /* OnboardingHeader.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF62AD449DC0008D38B /* OnboardingHeader.swift */; }; 76059BF92AD558C30008D38B /* SetupItemView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 76059BF82AD558C30008D38B /* SetupItemView.swift */; }; @@ -102,6 +103,7 @@ 52B363EB2AEC10A3006F515C /* ParametersTableForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableForm.swift; sourceTree = ""; }; 52B363ED2AEC10B3006F515C /* ParametersTableFormViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableFormViewModel.swift; sourceTree = ""; }; 551B88292B1385E900B8D325 /* Terminal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Terminal.swift; sourceTree = ""; }; + 55CDB0772B1B6D24002418D7 /* TerminalApps.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalApps.swift; sourceTree = ""; }; 76059BF42AD4361C0008D38B /* SetupPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupPreferences.swift; sourceTree = ""; }; 76059BF62AD449DC0008D38B /* OnboardingHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingHeader.swift; sourceTree = ""; }; 76059BF82AD558C30008D38B /* SetupItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupItemView.swift; sourceTree = ""; }; @@ -205,6 +207,15 @@ path = ParametersTable; sourceTree = ""; }; + 55CDB0762B1B6D06002418D7 /* Terminal */ = { + isa = PBXGroup; + children = ( + 551B88292B1385E900B8D325 /* Terminal.swift */, + 55CDB0772B1B6D24002418D7 /* TerminalApps.swift */, + ); + path = Terminal; + sourceTree = ""; + }; 762CF1E12981DDD400099999 /* Extensions */ = { isa = PBXGroup; children = ( @@ -264,10 +275,10 @@ 7645D4BC2982800D00019227 /* Service */ = { isa = PBXGroup; children = ( + 55CDB0762B1B6D06002418D7 /* Terminal */, 76F269832A2A375900424BDA /* CustomErrors */, 7645D4BD2982A1B100019227 /* DeviceService.swift */, 76F04A10298A5AE000BF9CA3 /* ADB.swift */, - 551B88292B1385E900B8D325 /* Terminal.swift */, ); path = Service; sourceTree = ""; @@ -599,6 +610,7 @@ 7630B26D2986B4FD00D8B57D /* KeyboardShortcuts.swift in Sources */, 76059BF52AD4361C0008D38B /* SetupPreferences.swift in Sources */, 7684FAAF29D202F500230BB0 /* AndroidHomeError.swift in Sources */, + 55CDB0782B1B6D24002418D7 /* TerminalApps.swift in Sources */, 7645D4BE2982A1B100019227 /* DeviceService.swift in Sources */, 765ABF382A8BECD900A063CB /* ExecuteCommand.swift in Sources */, ); diff --git a/MiniSim/Extensions/UserDefaults+Configuration.swift b/MiniSim/Extensions/UserDefaults+Configuration.swift index 3959f73..4e20321 100644 --- a/MiniSim/Extensions/UserDefaults+Configuration.swift +++ b/MiniSim/Extensions/UserDefaults+Configuration.swift @@ -47,7 +47,7 @@ extension UserDefaults { get { bool(forKey: Keys.enableAndroidEmulators) } set { set(newValue, forKey: Keys.enableAndroidEmulators) } } -// Need to add ui for changing the terminal preference +// TODO: #112 Need to add ui for changing the terminal preference @objc public dynamic var preferedTerminal: String? { get { string(forKey: Keys.preferedTerminal) } set { set(newValue, forKey: Keys.preferedTerminal) } diff --git a/MiniSim/MenuItems/SubMenuItem.swift b/MiniSim/MenuItems/SubMenuItem.swift index 02368c9..2f15795 100644 --- a/MiniSim/MenuItems/SubMenuItem.swift +++ b/MiniSim/MenuItems/SubMenuItem.swift @@ -164,6 +164,7 @@ extension SubMenuItems { CopyUDID(), Separator(), + Delete() ] } diff --git a/MiniSim/MiniSim.swift b/MiniSim/MiniSim.swift index ed925f0..9092023 100644 --- a/MiniSim/MiniSim.swift +++ b/MiniSim/MiniSim.swift @@ -116,7 +116,8 @@ class MiniSim: NSObject { private func setDefaultValues() { UserDefaults.standard.register(defaults: [ UserDefaults.Keys.enableAndroidEmulators: true, - UserDefaults.Keys.enableiOSSimulators: true + UserDefaults.Keys.enableiOSSimulators: true, + UserDefaults.Keys.preferedTerminal: "Terminal" ]) } diff --git a/MiniSim/Service/DeviceService.swift b/MiniSim/Service/DeviceService.swift index 4893097..156e745 100644 --- a/MiniSim/Service/DeviceService.swift +++ b/MiniSim/Service/DeviceService.swift @@ -426,13 +426,13 @@ extension DeviceService { } static func launchLogCat(device: Device) throws { - if device.booted { - guard let deviceId = device.identifier else { - throw DeviceError.deviceNotFound - } - let preferedTermial = TerminalType(rawValue: UserDefaults.standard.preferedTerminal ?? "Terminal") - try TerminalService.launchTerminal(type: preferedTermial!, deviceId: deviceId) + guard let deviceId = device.identifier else { + throw DeviceError.deviceNotFound } + guard let preferedTerminal = TerminalType(rawValue: UserDefaults.standard.preferedTerminal ?? "Terminal") + else { return } + let terminal = TerminalService.getTerminal(type: preferedTerminal) + try TerminalService.launchTerminal(terminal: terminal, deviceId: deviceId) } static func handleAndroidAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) { do { diff --git a/MiniSim/Service/Terminal.swift b/MiniSim/Service/Terminal.swift deleted file mode 100644 index 9b0bdf4..0000000 --- a/MiniSim/Service/Terminal.swift +++ /dev/null @@ -1,48 +0,0 @@ -// -// Terminal.swift -// MiniSim -// -// Created by Gokulakrishnan Subramaniyan on 26/11/23. -// - -import Foundation -import ShellOut - -enum TerminalType: String { - case terminal = "Terminal" - case iterm = "iTerm" -} -protocol TerminalServiceProtocol { - static func getScript(type: TerminalType, deviceId: String) -> String - static func launchTerminal(type: TerminalType, deviceId: String) throws -} - -class TerminalService: TerminalServiceProtocol { - static func getScript(type: TerminalType, deviceId: String) -> String { - let logcatCommand = "adb -s \(deviceId) logcat -v color" - switch type { - case .terminal: - return """ - tell app \"Terminal\" - do script \"\(logcatCommand)\" - end tell - """ - case .iterm: - return """ - tell app \"iTerm\" - set newWindow to (create window with default profile) - tell current session of newWindow - write text \"\(logcatCommand)\" - end tell - end tell - """ - default: - return "" - } - } - - static func launchTerminal(type: TerminalType, deviceId: String) throws { - let script = getScript(type: type, deviceId: deviceId) - try shellOut(to: "osascript -e '\(script)'") - } -} diff --git a/MiniSim/Service/Terminal/Terminal.swift b/MiniSim/Service/Terminal/Terminal.swift new file mode 100644 index 0000000..1a9aa96 --- /dev/null +++ b/MiniSim/Service/Terminal/Terminal.swift @@ -0,0 +1,35 @@ +// +// Terminal.swift +// MiniSim +// +// Created by Gokulakrishnan Subramaniyan on 26/11/23. +// + +import Foundation +import ShellOut + +enum TerminalType: String { + case terminal = "Terminal" + case iterm = "iTerm" +} +protocol TerminalServiceProtocol { + static func getTerminal(type: TerminalType) -> TerminalApp + static func launchTerminal(terminal: TerminalApp, deviceId: String) throws +} + +class TerminalService: TerminalServiceProtocol { + static func getTerminal(type: TerminalType) -> TerminalApp { + switch type { + case .terminal: + return AppleTerminal() + case .iterm: + return ITermTerminal() + } + } + + static func launchTerminal(terminal: TerminalApp, deviceId: String) throws { + let logcatCommand = "adb -s \(deviceId) logcat -v color" + let terminalScript = terminal.getLaunchScript(deviceId: deviceId, logcatCommand: logcatCommand) + try shellOut(to: "osascript -e '\(terminalScript)'") + } +} diff --git a/MiniSim/Service/Terminal/TerminalApps.swift b/MiniSim/Service/Terminal/TerminalApps.swift new file mode 100644 index 0000000..e4a7a02 --- /dev/null +++ b/MiniSim/Service/Terminal/TerminalApps.swift @@ -0,0 +1,40 @@ +// +// TerminalApps.swift +// MiniSim +// +// Created by Gokulakrishnan Subramaniyan on 02/12/23. +// + +import Foundation + +protocol TerminalApp { + var name: String { get } + func getLaunchScript(deviceId: String, logcatCommand: String) -> String +} + +struct AppleTerminal: TerminalApp { + var name: String = "Terminal" + func getLaunchScript(deviceId: String, logcatCommand: String) -> String { + """ + tell app \"Terminal\" + activate + do script \"\(logcatCommand)\" + end tell + """ + } +} + +struct ITermTerminal: TerminalApp { + var name: String = "iTerm" + + func getLaunchScript(deviceId: String, logcatCommand: String) -> String { + """ + tell app \"iTerm\" + set newWindow to (create window with default profile) + tell current session of newWindow + write text \"\(logcatCommand)\" + end tell + end tell + """ + } +} diff --git a/MiniSimTests/UserDefaultsTests.swift b/MiniSimTests/UserDefaultsTests.swift index 33a4fcc..3cfebc4 100644 --- a/MiniSimTests/UserDefaultsTests.swift +++ b/MiniSimTests/UserDefaultsTests.swift @@ -9,6 +9,7 @@ final class UserDefaultsTests: XCTestCase { let savedIsOnboardingFinished = UserDefaults.standard.isOnboardingFinished let savedEnableiOSSimulators = UserDefaults.standard.enableiOSSimulators let savedEnableAndroidEmulators = UserDefaults.standard.enableAndroidEmulators + let savedPrefferedTerminal = UserDefaults.standard.preferedTerminal override func setUp() { UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.parameters) @@ -17,6 +18,7 @@ final class UserDefaultsTests: XCTestCase { UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.isOnboardingFinished) UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.enableiOSSimulators) UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.enableAndroidEmulators) + UserDefaults.standard.removeObject(forKey: UserDefaults.Keys.preferedTerminal) } override func tearDown() { @@ -26,6 +28,7 @@ final class UserDefaultsTests: XCTestCase { UserDefaults.standard.isOnboardingFinished = savedIsOnboardingFinished UserDefaults.standard.enableiOSSimulators = savedEnableiOSSimulators UserDefaults.standard.enableAndroidEmulators = savedEnableAndroidEmulators + UserDefaults.standard.preferedTerminal = savedPrefferedTerminal } func testDefaultValues() { @@ -35,6 +38,7 @@ final class UserDefaultsTests: XCTestCase { XCTAssertEqual(UserDefaults.standard.isOnboardingFinished, false) XCTAssertEqual(UserDefaults.standard.enableiOSSimulators, true) XCTAssertEqual(UserDefaults.standard.enableAndroidEmulators, true) + XCTAssertEqual(UserDefaults.standard.preferedTerminal, "Terminal") } func testChangingParameters() { @@ -53,10 +57,12 @@ final class UserDefaultsTests: XCTestCase { UserDefaults.standard.enableiOSSimulators = false UserDefaults.standard.enableAndroidEmulators = false UserDefaults.standard.androidHome = "test" + UserDefaults.standard.preferedTerminal = "test" XCTAssertEqual(UserDefaults.standard.isOnboardingFinished, true) XCTAssertEqual(UserDefaults.standard.enableiOSSimulators, false) XCTAssertEqual(UserDefaults.standard.enableAndroidEmulators, false) XCTAssertEqual(UserDefaults.standard.androidHome, "test") + XCTAssertEqual(UserDefaults.standard.preferedTerminal, "test") } } From 4661eb61a772f1fadd685c6cd3aade46211e62b9 Mon Sep 17 00:00:00 2001 From: Gokulakrishnan Date: Sun, 10 Dec 2023 10:54:13 +0530 Subject: [PATCH 6/7] review changes --- MiniSim/Service/DeviceService.swift | 2 +- MiniSim/Service/Terminal/Terminal.swift | 8 ++------ MiniSim/Service/Terminal/TerminalApps.swift | 5 +++++ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/MiniSim/Service/DeviceService.swift b/MiniSim/Service/DeviceService.swift index 156e745..74bde60 100644 --- a/MiniSim/Service/DeviceService.swift +++ b/MiniSim/Service/DeviceService.swift @@ -429,7 +429,7 @@ extension DeviceService { guard let deviceId = device.identifier else { throw DeviceError.deviceNotFound } - guard let preferedTerminal = TerminalType(rawValue: UserDefaults.standard.preferedTerminal ?? "Terminal") + guard let preferedTerminal = Terminal(rawValue: UserDefaults.standard.preferedTerminal ?? Terminal.terminal.rawValue ) else { return } let terminal = TerminalService.getTerminal(type: preferedTerminal) try TerminalService.launchTerminal(terminal: terminal, deviceId: deviceId) diff --git a/MiniSim/Service/Terminal/Terminal.swift b/MiniSim/Service/Terminal/Terminal.swift index 1a9aa96..d464dd6 100644 --- a/MiniSim/Service/Terminal/Terminal.swift +++ b/MiniSim/Service/Terminal/Terminal.swift @@ -8,17 +8,13 @@ import Foundation import ShellOut -enum TerminalType: String { - case terminal = "Terminal" - case iterm = "iTerm" -} protocol TerminalServiceProtocol { - static func getTerminal(type: TerminalType) -> TerminalApp + static func getTerminal(type: Terminal) -> TerminalApp static func launchTerminal(terminal: TerminalApp, deviceId: String) throws } class TerminalService: TerminalServiceProtocol { - static func getTerminal(type: TerminalType) -> TerminalApp { + static func getTerminal(type: Terminal) -> TerminalApp { switch type { case .terminal: return AppleTerminal() diff --git a/MiniSim/Service/Terminal/TerminalApps.swift b/MiniSim/Service/Terminal/TerminalApps.swift index e4a7a02..6321a1e 100644 --- a/MiniSim/Service/Terminal/TerminalApps.swift +++ b/MiniSim/Service/Terminal/TerminalApps.swift @@ -12,6 +12,11 @@ protocol TerminalApp { func getLaunchScript(deviceId: String, logcatCommand: String) -> String } +enum Terminal: String { + case terminal = "Terminal" + case iterm = "iTerm" +} + struct AppleTerminal: TerminalApp { var name: String = "Terminal" func getLaunchScript(deviceId: String, logcatCommand: String) -> String { From 32c2e243a03206d30111bb9f4ea619be0fdd597c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oskar=20Kwas=CC=81niewski?= Date: Tue, 12 Dec 2023 20:18:19 +0100 Subject: [PATCH 7/7] fix: format code --- MiniSim/Service/DeviceService.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/MiniSim/Service/DeviceService.swift b/MiniSim/Service/DeviceService.swift index 74bde60..50873b5 100644 --- a/MiniSim/Service/DeviceService.swift +++ b/MiniSim/Service/DeviceService.swift @@ -429,11 +429,14 @@ extension DeviceService { guard let deviceId = device.identifier else { throw DeviceError.deviceNotFound } - guard let preferedTerminal = Terminal(rawValue: UserDefaults.standard.preferedTerminal ?? Terminal.terminal.rawValue ) + guard let preferedTerminal = Terminal( + rawValue: UserDefaults.standard.preferedTerminal ?? Terminal.terminal.rawValue + ) else { return } let terminal = TerminalService.getTerminal(type: preferedTerminal) try TerminalService.launchTerminal(terminal: terminal, deviceId: deviceId) } + static func handleAndroidAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) { do { switch commandTag {