Skip to content

Commit

Permalink
feature: delete android emulator (#104)
Browse files Browse the repository at this point in the history
* added delete option for emulator

* fix deleting booted devices

* lint issue fixes
  • Loading branch information
gokul1099 authored Nov 20, 2023
1 parent 2a991ce commit 8075092
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
14 changes: 13 additions & 1 deletion MiniSim/MenuItems/SubMenuItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ enum SubMenuItems {
accessibilityDescription: "Delete simulator"
)
}

struct DeleteEmulator: SubMenuActionItem {
let title = NSLocalizedString("Delete emulator", comment: "")
let tag = Tags.delete.rawValue
let bootsDevice = false
let needBootedDevice = false
let image = NSImage(
systemSymbolName: "trash",
accessibilityDescription: "Delete emulator"
)
}
}

extension SubMenuItems {
Expand All @@ -131,7 +142,8 @@ extension SubMenuItems {
ColdBoot(),
NoAudio(),
ToggleA11y(),
Paste()
Paste(),
DeleteEmulator()
]

static var ios: [SubMenuItem] = [
Expand Down
5 changes: 5 additions & 0 deletions MiniSim/Service/Adb.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ final class ADB: ADBProtocol {
case home = "/Android/sdk"
case emulator = "/emulator/emulator"
case adb = "/platform-tools/adb"
case avd = "/cmdline-tools/latest/bin/avdmanager"
}

/**
Expand All @@ -47,6 +48,10 @@ final class ADB: ADBProtocol {
try getAndroidHome() + Paths.adb.rawValue
}

static func getAvdPath() throws -> String {
try getAndroidHome() + Paths.avd.rawValue
}

/**
Checks if passed path exists and points to `ANDROID_HOME`.
*/
Expand Down
30 changes: 28 additions & 2 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,8 +413,19 @@ extension DeviceService {
try shellOut(to: "\(adbPath) -s \(deviceId) shell input text \"\(formattedText)\"")
}

static func deleteEmulator(device: Device) throws {
let avdPath = try ADB.getAvdPath()
let adbPath = try ADB.getAdbPath()
if device.booted {
guard let deviceId = device.identifier else {
throw DeviceError.deviceNotFound
}
try shellOut(to: "\(adbPath) -s \(deviceId) emu kill")
}
try shellOut(to: "\(avdPath) delete avd -n \"\(device.name)\"")
}

static func handleAndroidAction(device: Device, commandTag: SubMenuItems.Tags, itemName: String) {
queue.async {
do {
switch commandTag {
case .coldBoot:
Expand Down Expand Up @@ -447,12 +458,27 @@ extension DeviceService {
if let command = DeviceService.getCustomCommand(platform: .android, commandName: itemName) {
try DeviceService.runCustomCommand(device, command: command)
}

case .delete:
let result = !NSAlert.showQuestionDialog(
title: "Are you sure?",
message: "Are you sure you want to delete this Emulator?"
)
if result { return }
queue.async {
do {
try DeviceService.deleteEmulator(device: device)
DeviceService.showSuccessMessage(title: "Emulator deleted!", message: device.name)
NotificationCenter.default.post(name: .deviceDeleted, object: nil)
} catch {
NSAlert.showError(message: error.localizedDescription)
}
}
default:
break
}
} catch {
NSAlert.showError(message: error.localizedDescription)
}
}
}
}

0 comments on commit 8075092

Please sign in to comment.