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 5 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
4 changes: 4 additions & 0 deletions MiniSim.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 */; };
Expand Down Expand Up @@ -100,6 +101,7 @@
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>"; };
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 @@ -265,6 +267,7 @@
76F269832A2A375900424BDA /* CustomErrors */,
7645D4BD2982A1B100019227 /* DeviceService.swift */,
76F04A10298A5AE000BF9CA3 /* ADB.swift */,
551B88292B1385E900B8D325 /* Terminal.swift */,
);
path = Service;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
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 @@ extension UserDefaults {
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 @@ 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) }
}
}
16 changes: 14 additions & 2 deletions 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,15 +155,15 @@ extension SubMenuItems {
NoAudio(),
ToggleA11y(),
Paste(),
DeleteEmulator()
DeleteEmulator(),
LaunchLogCat()
]

static var ios: [SubMenuItem] = [
CopyName(),
CopyUDID(),

Separator(),

Delete()
]
}
12 changes: 12 additions & 0 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(
Expand All @@ -474,6 +485,7 @@ extension DeviceService {
NSAlert.showError(message: error.localizedDescription)
}
}

default:
break
}
Expand Down
48 changes: 48 additions & 0 deletions MiniSim/Service/Terminal.swift
Original file line number Diff line number Diff line change
@@ -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)'")
}
}