Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added logcat feature #111

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions MiniSim.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
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 */; };
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 */; };
Expand Down Expand Up @@ -100,6 +102,8 @@
52B363E92AEC0D3D006F515C /* ParametersTableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableViewModel.swift; sourceTree = "<group>"; };
52B363EB2AEC10A3006F515C /* ParametersTableForm.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableForm.swift; sourceTree = "<group>"; };
52B363ED2AEC10B3006F515C /* ParametersTableFormViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ParametersTableFormViewModel.swift; sourceTree = "<group>"; };
551B88292B1385E900B8D325 /* Terminal.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Terminal.swift; sourceTree = "<group>"; };
55CDB0772B1B6D24002418D7 /* TerminalApps.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TerminalApps.swift; sourceTree = "<group>"; };
76059BF42AD4361C0008D38B /* SetupPreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupPreferences.swift; sourceTree = "<group>"; };
76059BF62AD449DC0008D38B /* OnboardingHeader.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingHeader.swift; sourceTree = "<group>"; };
76059BF82AD558C30008D38B /* SetupItemView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetupItemView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -203,6 +207,15 @@
path = ParametersTable;
sourceTree = "<group>";
};
55CDB0762B1B6D06002418D7 /* Terminal */ = {
isa = PBXGroup;
children = (
551B88292B1385E900B8D325 /* Terminal.swift */,
55CDB0772B1B6D24002418D7 /* TerminalApps.swift */,
);
path = Terminal;
sourceTree = "<group>";
};
762CF1E12981DDD400099999 /* Extensions */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -262,6 +275,7 @@
7645D4BC2982800D00019227 /* Service */ = {
isa = PBXGroup;
children = (
55CDB0762B1B6D06002418D7 /* Terminal */,
76F269832A2A375900424BDA /* CustomErrors */,
7645D4BD2982A1B100019227 /* DeviceService.swift */,
76F04A10298A5AE000BF9CA3 /* ADB.swift */,
Expand Down Expand Up @@ -541,6 +555,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 */,
Expand Down Expand Up @@ -595,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 */,
);
Expand Down
6 changes: 6 additions & 0 deletions MiniSim/Extensions/UserDefaults+Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
static let isOnboardingFinished = "isOnboardingFinished"
static let enableiOSSimulators = "enableiOSSimulators"
static let enableAndroidEmulators = "enableAndroidEmulators"
static let preferedTerminal = "preferedTerminal"
}

@objc public dynamic var androidHome: String? {
Expand Down Expand Up @@ -46,4 +47,9 @@
get { bool(forKey: Keys.enableAndroidEmulators) }
set { set(newValue, forKey: Keys.enableAndroidEmulators) }
}
// TODO: #112 Need to add ui for changing the terminal preference

Check warning on line 50 in MiniSim/Extensions/UserDefaults+Configuration.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Todo Violation: TODOs should be resolved (#112 Need to add ui for changi...) (todo)
@objc public dynamic var preferedTerminal: String? {
get { string(forKey: Keys.preferedTerminal) }
okwasniewski marked this conversation as resolved.
Show resolved Hide resolved
set { set(newValue, forKey: Keys.preferedTerminal) }
}
}
15 changes: 14 additions & 1 deletion MiniSim/MenuItems/SubMenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ enum SubMenuItems {
case toggleA11y
case paste
case delete
case logcat
case customCommand = 200
}

Expand Down Expand Up @@ -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: "terminal",
accessibilityDescription: "Launch Logcat"
)
}
}

extension SubMenuItems {
Expand All @@ -143,7 +155,8 @@ extension SubMenuItems {
NoAudio(),
ToggleA11y(),
Paste(),
DeleteEmulator()
DeleteEmulator(),
LaunchLogCat()
]

static var ios: [SubMenuItem] = [
Expand Down
3 changes: 2 additions & 1 deletion MiniSim/MiniSim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
])
}

Expand Down
12 changes: 12 additions & 0 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@
try shellOut(to: ProcessPaths.xcrun.rawValue, arguments: ["simctl", "delete", uuid])
}

static func handleiOSAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) {

Check warning on line 305 in MiniSim/Service/DeviceService.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 11 (cyclomatic_complexity)
switch commandTag {
case .copyName:
NSPasteboard.general.copyToPasteboard(text: device.name)
Expand Down Expand Up @@ -425,7 +425,16 @@
try shellOut(to: "\(avdPath) delete avd -n \"\(device.name)\"")
}

static func launchLogCat(device: Device) throws {
guard let deviceId = device.identifier else {
throw DeviceError.deviceNotFound
}
guard let preferedTerminal = TerminalType(rawValue: UserDefaults.standard.preferedTerminal ?? "Terminal")
okwasniewski marked this conversation as resolved.
Show resolved Hide resolved
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) {

Check warning on line 437 in MiniSim/Service/DeviceService.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Cyclomatic Complexity Violation: Function should have complexity 10 or less; currently complexity is 16 (cyclomatic_complexity)
do {
switch commandTag {
case .coldBoot:
Expand Down Expand Up @@ -458,6 +467,8 @@
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(
Expand All @@ -474,6 +485,7 @@
NSAlert.showError(message: error.localizedDescription)
}
}

default:
break
}
Expand All @@ -481,4 +493,4 @@
NSAlert.showError(message: error.localizedDescription)
}
}
}

Check warning on line 496 in MiniSim/Service/DeviceService.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

File Length Violation: File should contain 400 lines or less: currently contains 496 (file_length)
35 changes: 35 additions & 0 deletions MiniSim/Service/Terminal/Terminal.swift
Original file line number Diff line number Diff line change
@@ -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"
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's name this just Terminaland move it to TerminalApps.swift so its in one place

protocol TerminalServiceProtocol {
static func getTerminal(type: TerminalType) -> TerminalApp
static func launchTerminal(terminal: TerminalApp, deviceId: String) throws
}

class TerminalService: TerminalServiceProtocol {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's leave tests for this module for now - I will add them later

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)'")
}
}
40 changes: 40 additions & 0 deletions MiniSim/Service/Terminal/TerminalApps.swift
Original file line number Diff line number Diff line change
@@ -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
"""
}
}
6 changes: 6 additions & 0 deletions MiniSimTests/UserDefaultsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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")
}
}
Loading