diff --git a/Sources/ExFig/Subcommands/GenerateConfigFile.swift b/Sources/ExFig/Subcommands/GenerateConfigFile.swift index f73039a5..7eb2047f 100644 --- a/Sources/ExFig/Subcommands/GenerateConfigFile.swift +++ b/Sources/ExFig/Subcommands/GenerateConfigFile.swift @@ -22,13 +22,23 @@ extension ExFigCommand { var globalOptions: GlobalOptions @Option(name: .shortAndLong, help: "Platform: ios or android.") - var platform: Platform + var platform: Platform? func run() async throws { ExFigCommand.initializeTerminalUI(verbose: globalOptions.verbose, quiet: globalOptions.quiet) let ui = ExFigCommand.terminalUI! - let fileContents: String = switch platform { + let selectedPlatform: Platform + if let platform { + selectedPlatform = platform + } else { + if !TTYDetector.isTTY { + throw ValidationError("Missing required argument: --platform ") + } + selectedPlatform = try promptForPlatform(ui: ui) + } + + let fileContents: String = switch selectedPlatform { case .android: androidConfigFileContents case .ios: @@ -119,5 +129,28 @@ extension ExFigCommand { throw ExFigError.custom(errorString: "Unable to create config file at: \(destination)") } } + + private func promptForPlatform(ui: TerminalUI) throws -> Platform { + ui.info("Select a platform to generate config for:") + let platforms = Platform.allCases + for (index, platform) in platforms.enumerated() { + ui.info("\(index + 1). \(platform.rawValue)") + } + + while true { + TerminalOutputManager.shared.writeDirect("Enter number [1-\(platforms.count)]: ") + ANSICodes.flushStdout() + + guard let input = readLine(), + let index = Int(input.trimmingCharacters(in: .whitespacesAndNewlines)), + index > 0, index <= platforms.count + else { + ui.error("Invalid selection. Please enter a number between 1 and \(platforms.count).") + continue + } + + return platforms[index - 1] + } + } } } diff --git a/Sources/ExFigCore/Platform.swift b/Sources/ExFigCore/Platform.swift index ebe298ba..f3af2d39 100644 --- a/Sources/ExFigCore/Platform.swift +++ b/Sources/ExFigCore/Platform.swift @@ -4,7 +4,7 @@ import Foundation /// /// ExFig supports exporting to both iOS (Xcode) and Android (Android Studio) projects. /// The platform affects naming conventions, output formats, and generated code. -public enum Platform: String, Sendable { +public enum Platform: String, Sendable, CaseIterable { /// iOS/iPadOS/macOS platform (Xcode projects). /// Generates xcassets, Swift extensions for UIKit and SwiftUI. case ios