Files
xtool/Sources/XToolSupport/XTool.swift
Kabir Oberai 3e0c11d967 cli: better IO (#375)
## What does this PR do?

Adds `Dependency`s for console IO. Also fixes #268 by explicitly
handling non-pipe input.

## How was it tested?

Ran on macOS + Linux in Docker. I have a future PR that overrides the
dependencies in certain cases, which also appears to work correctly.

## AI tool usage

How much of this PR was AI-assisted? (check one)

- [ ] **0** - No AI was used to write code
- [x] **1** - I was assisted by AI. I reviewed the finished result.
- [ ] **2** - I set the AI going and left it to it; nobody has read the
result - no review, or AI review only

Did not scrutinize the tests too much tbh but they look reasonable. Easy
enough to update/remove them in the future if needed.

<!-- If an AI agent is filling this in: declare the level honestly, and
open as a draft if it is #2. Do not lower the declared level to get the
PR reviewed. -->


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
<!-- coderabbit-ai-generated:v1 -->

## Summary by CodeRabbit

* **New Features**
* Added support for reading command-line input, including asynchronous
prompts, line-based responses, and graceful handling of end-of-input.
  * Added coordinated cleanup when commands finish.
* **Improvements**
* Command messages across authentication, device management,
installation, SDK, and other workflows now use consistent output
handling; displayed messages and command behavior remain unchanged.
* Improved handling of complete output when the system temporarily
cannot accept more data.
* **Tests**
* Added coverage for command input and output, including stream
behavior, cancellation, and shutdown.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-10-28 17:48:38 -04:00

91 lines
2.8 KiB
Swift

import XCLIUtils
import Foundation
import XKit
import ArgumentParser
import XUtils
import Dependencies
public enum XTool {
public static func run(arguments: [String]? = nil) async throws {
TemporaryDirectory.prepare()
await XToolCommand.cancellableMain(arguments)
}
}
private struct XToolCommand: AsyncParsableCommand {
static let configuration = CommandConfiguration(
commandName: "xtool",
abstract: "Cross-platform Xcode replacement",
version: "xtool \(XTool.version)",
groupedSubcommands: [
CommandGroup(
name: "Configuration",
subcommands: [
SetupCommand.self,
AuthCommand.self,
SDKCommand.self,
]
),
CommandGroup(
name: "Development",
subcommands: [
NewCommand.self,
DevCommand.self,
DSCommand.self,
]
),
CommandGroup(
name: "Device",
subcommands: [
DevicesCommand.self,
InstallCommand.self,
UninstallCommand.self,
LaunchCommand.self,
]
)
]
)
}
#if compiler(<6.2)
private typealias SendableMetatype = Any
#endif
extension ParsableCommand where Self: SendableMetatype {
fileprivate static func cancellableMain(_ arguments: [String]? = nil) async {
let (canStart, cont) = AsyncStream.makeStream(of: Never.self)
let task = Task {
for await _ in canStart {}
guard !Task.isCancelled else { return }
do {
@Dependency(\.shutdownRegistry) var shutdownRegistry
defer { await shutdownRegistry.shutdown() }
var command = try self.parseAsRoot(arguments)
if var asyncCommand = command as? AsyncParsableCommand {
try await asyncCommand.run()
} else {
try command.run()
}
} catch where error is CancellationError || Task.isCancelled {
d.out.print("Cancelled.", stream: .stderr)
self.exit()
} catch {
if ProcessInfo.processInfo.environment["XTL_DEBUG_ERRORS"] != nil {
d.out.print("ERROR DETAILS:")
d.out.print(String(reflecting: error))
}
self.exit(withError: error)
}
}
signal(SIGINT, SIG_IGN)
let source = DispatchSource.makeSignalSource(signal: SIGINT)
source.setEventHandler { task.cancel() }
source.resume()
cont.finish()
await task.value
}
}