Skip to content

armgit / xtool

Public Imported from GitHub
0

Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, macOS.

1.4 KB
Raw Blame
import XUtils
import XCLIUtils
import Foundation
import XKit
import ArgumentParser

struct ConnectionOptions: ParsableArguments {
    struct WithoutSearchMode: ParsableArguments {
        @Option(name: .shortAndLong) var udid: String?
    }

    @OptionGroup var base: WithoutSearchMode
    @Flag var search: ClientDevice.SearchMode = .all

    func client() async throws -> ClientDevice {
        try await base.client(searchMode: search)
    }
}

extension ConnectionOptions.WithoutSearchMode {
    func client(searchMode search: ClientDevice.SearchMode) async throws -> ClientDevice {
        d.out.print("Waiting for device to be connected...")

        let stream = try await ClientDevice.search(mode: search)
        for await devices in stream {
            guard !devices.isEmpty else { continue }
            if let udid {
                guard let device = devices.first(where: { $0.udid == udid }) else {
                    continue
                }
                return device
            } else {
                return try await Console.choose(
                    from: devices,
                    onNoElement: { throw Console.Error("Device not found") },
                    multiPrompt: "Choose device",
                    formatter: { "\($0.deviceName) (\($0.connectionType), udid: \($0.udid))" }
                )
            }
        }

        throw CancellationError()
    }
}