-
-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: move custom commands to separate class
- Loading branch information
1 parent
0121c91
commit a318096
Showing
11 changed files
with
260 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import Foundation | ||
|
||
class CustomCommandService { | ||
static var shell: ShellProtocol = Shell() | ||
static var adb: ADBProtocol.Type = ADB.self | ||
|
||
static func getCustomCommands(platform: Platform, userDefaults: UserDefaults = UserDefaults.standard) -> [Command] { | ||
guard let commandsData = userDefaults.commands else { return [] } | ||
guard let commands = try? JSONDecoder().decode([Command].self, from: commandsData) else { | ||
return [] | ||
} | ||
|
||
return commands.filter { $0.platform == platform } | ||
} | ||
|
||
static func getCustomCommand( | ||
platform: Platform, | ||
commandName: String, | ||
userDefaults: UserDefaults = UserDefaults.standard | ||
) -> Command? { | ||
let commands = getCustomCommands(platform: platform, userDefaults: userDefaults) | ||
return commands.first { $0.name == commandName } | ||
} | ||
|
||
static func runCustomCommand(_ device: Device, command: Command) throws { | ||
// Thread.assertBackgroundThread() | ||
var commandToExecute = command.command | ||
.replacingOccurrences(of: Variables.deviceName.rawValue, with: device.name) | ||
|
||
let deviceID = device.identifier ?? "" | ||
|
||
if command.platform == .android { | ||
commandToExecute = try commandToExecute | ||
.replacingOccurrences(of: Variables.adbPath.rawValue, with: adb.getAdbPath()) | ||
.replacingOccurrences(of: Variables.adbId.rawValue, with: deviceID) | ||
.replacingOccurrences(of: Variables.androidHomePath.rawValue, with: adb.getAndroidHome()) | ||
} else { | ||
commandToExecute = commandToExecute | ||
.replacingOccurrences(of: Variables.uuid.rawValue, with: deviceID) | ||
.replacingOccurrences(of: Variables.xcrunPath.rawValue, with: DeviceConstants.ProcessPaths.xcrun.rawValue) | ||
} | ||
|
||
do { | ||
try shell.execute(command: commandToExecute) | ||
if command.bootsDevice ?? false && command.platform == .ios { | ||
try? DeviceService.launchSimulatorApp(uuid: deviceID) | ||
} | ||
NotificationCenter.default.post(name: .commandDidSucceed, object: nil) | ||
} catch { | ||
throw CustomCommandError.commandError(errorMessage: error.localizedDescription) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import Foundation | ||
|
||
enum DeviceConstants { | ||
static let deviceBootedError = "Unable to boot device in current state: Booted" | ||
static let derivedDataLocation = "~/Library/Developer/Xcode/DerivedData" | ||
|
||
enum ProcessPaths: String { | ||
case xcrun = "/usr/bin/xcrun" | ||
case xcodeSelect = "/usr/bin/xcode-select" | ||
} | ||
|
||
enum BundleURL: String { | ||
case emulator = "qemu-system-aarch64" | ||
case simulator = "Simulator.app" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.