Skip to content

Commit

Permalink
feat: allow custom commands to boot devices
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Jul 12, 2023
1 parent 8db3798 commit de0eddf
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 10 deletions.
16 changes: 12 additions & 4 deletions MiniSim/Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,12 @@ class Menu: NSMenu {
case .customCommand:
let iosCommands = getAdditionalCommands(platform: .ios)
guard let command = iosCommands.first(where: {$0.name == sender.title}) else { return }
do {
try deviceService.runCustomCommand(device, command: command)
} catch {
NSAlert.showError(message: error.localizedDescription)
DispatchQueue.global().async { [self] in
do {
try deviceService.runCustomCommand(device, command: command)
} catch {
NSAlert.showError(message: error.localizedDescription)
}
}
default:
break
Expand Down Expand Up @@ -305,6 +307,9 @@ class Menu: NSMenu {
if item.needBootedDevice && !booted {
continue
}
if item.bootsDevice ?? false && booted {
continue
}
menuItem.image = NSImage(systemSymbolName: item.icon, accessibilityDescription: item.name)
menuItem.title = item.name
subMenu.addItem(menuItem)
Expand All @@ -331,6 +336,9 @@ class Menu: NSMenu {
if item.needBootedDevice && !booted {
continue
}
if item.bootsDevice ?? false && booted {
continue
}
menuItem.image = NSImage(systemSymbolName: item.icon, accessibilityDescription: item.name)
menuItem.title = item.name
subMenu.addItem(menuItem)
Expand Down
2 changes: 1 addition & 1 deletion MiniSim/MiniSim.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class MiniSim: NSObject {
settingsController.window?.delegate = self

appendMenu()
self.menu.getDevices()
menu.getDevices()
initObservers()
}

Expand Down
27 changes: 26 additions & 1 deletion MiniSim/Model/Command.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,34 @@ import Foundation

struct Command: Identifiable, Codable, Hashable {
var id = UUID()
/**
Name of the command shown in the menubar.
*/
let name: String

/**
Actual command to execute for eg: `adb devices`.
*/
let command: String

/**
SFSymbol name - shown in the menu bar.
*/
let icon: String

/**
Platform on which command will be executed.
*/
let platform: Platform
let needBootedDevice: Bool

/**
Determines if command will need a booted device to execute.
*/
var needBootedDevice: Bool

/**
Determines if command boots device.
Needs to be optional to preserve backwards compatibility with data stored in UserDefaults.
*/
var bootsDevice: Bool?
}
3 changes: 3 additions & 0 deletions MiniSim/Service/DeviceService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ class DeviceService: DeviceServiceProtocol {

do {
try shellOut(to: commandToExecute)
if (command.bootsDevice ?? false && command.platform == .ios) {
try? launchSimulatorApp(uuid: deviceID)
}
NotificationCenter.default.post(name: .commandDidSucceed, object: nil)
} catch {
throw CustomCommandError.commandError(errorMessage: error.localizedDescription)
Expand Down
32 changes: 29 additions & 3 deletions MiniSim/Views/CustomCommands/CustomCommandForm.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,18 @@ struct CustomCommandForm: View {
}

Toggle(isOn: $viewModel.needsBootedDevice, label: {
Text("Booted device")
Text("Needs booted device")
})
.help("Determines if command will need a booted device to execute.")
.toggleStyle(.switch)
.disabled(viewModel.bootsDevice)

Toggle(isOn: $viewModel.bootsDevice, label: {
Text("Boots device")
})
.help("Determines if executed command boots device. This command will be hidden on booted devices.")
.toggleStyle(.switch)
.disabled(viewModel.needsBootedDevice)

HStack {
HStack {
Expand All @@ -87,7 +96,17 @@ struct CustomCommandForm: View {
}
Spacer()
Button(command != nil ? "Update" : "Add") {
onSubmit(Command(name: viewModel.commandName, command: viewModel.command, icon: viewModel.icon, platform: viewModel.platform, needBootedDevice: viewModel.needsBootedDevice), command)
onSubmit(
Command(
name: viewModel.commandName,
command: viewModel.command,
icon: viewModel.icon,
platform: viewModel.platform,
needBootedDevice: viewModel.needsBootedDevice,
bootsDevice: viewModel.bootsDevice
),
command
)
viewModel.clearForm()
}
.buttonStyle(.borderedProminent)
Expand All @@ -103,7 +122,14 @@ struct CustomCommandForm: View {
}
.onAppear {
if let command {
viewModel.onAppear(commandName: command.name, command: command.command, needsBootedDevice: command.needBootedDevice, platform: command.platform, icon: command.icon)
viewModel.onAppear(
commandName: command.name,
command: command.command,
needsBootedDevice: command.needBootedDevice,
bootsDevice: command.bootsDevice ?? false,
platform: command.platform,
icon: command.icon
)
}
viewModel.onAppear(allCommands: allCommands, isUpdating: command != nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extension CustomCommandForm {
@Published var commandName = ""
@Published var command = ""
@Published var needsBootedDevice = false
@Published var bootsDevice = false
@Published var platform: Platform = Platform.android
@Published var icon = "info"

Expand All @@ -21,10 +22,11 @@ extension CustomCommandForm {
var allCommands: [Command] = []
var isUpdating: Bool = false

func onAppear(commandName: String = "", command: String = "", needsBootedDevice: Bool = false, platform: Platform = Platform.android, icon: String = "info", iconPickerPresented: Bool = false) {
func onAppear(commandName: String = "", command: String = "", needsBootedDevice: Bool = false, bootsDevice: Bool = false, platform: Platform = Platform.android, icon: String = "info", iconPickerPresented: Bool = false) {
self.commandName = commandName
self.command = command
self.needsBootedDevice = needsBootedDevice
self.bootsDevice = bootsDevice
self.platform = platform
self.icon = icon
self.iconPickerPresented = iconPickerPresented
Expand Down

0 comments on commit de0eddf

Please sign in to comment.