Files
xtool/Sources/XToolSupport/LSPConfig.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

92 lines
3.0 KiB
Swift

import XUtils
import Foundation
import PackLib
enum LSPConfig {
static let bsp: (String, String) = (
".bsp/xtool.json",
"""
{
"name": "xtool",
"version": "1.0",
"bspVersion": "2.2.0",
"languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
"argv": ["/usr/bin/env", "xtool", "dev", "build-server"]
}
"""
)
static let legacyLSP: (String, String) = (
".sourcekit-lsp/config.json",
"""
{
"swiftPM": {
"swiftSDK": "arm64-apple-ios"
}
}
"""
)
nonisolated(nonsending) static func tryEnsure(
using schema: PackSchema,
quiet: Bool = false,
currentDirectory directory: URL = URL(filePath: FileManager.default.currentDirectoryPath)
) async {
do {
try await ensure(using: schema, quiet: quiet, currentDirectory: directory)
} catch {
d.out.print("warning: failed to validate LSP configuration: \(error)")
}
}
nonisolated(nonsending) static func ensure(
using schema: PackSchema,
quiet: Bool = false,
currentDirectory directory: URL = URL(filePath: FileManager.default.currentDirectoryPath)
) async throws {
if schema.skipLSP == true {
return
}
if try await SwiftVersion.current.supportsSwiftBuild {
try await ensure(primary: bsp, secondary: legacyLSP, quiet: quiet, currentDirectory: directory)
} else {
try await ensure(primary: legacyLSP, secondary: bsp, quiet: quiet, currentDirectory: directory)
}
}
// creates the primary LSP-support file if it doesn't exist, and cleans up old files
private nonisolated(nonsending) static func ensure(
primary: (String, String),
secondary: (String, String),
quiet: Bool,
currentDirectory directory: URL
) async throws {
let primaryURL = directory.appendingPathComponent(primary.0)
if FileManager.default.fileExists(atPath: primaryURL.path) {
return
}
try? FileManager.default.createDirectory(
at: primaryURL.deletingLastPathComponent(),
withIntermediateDirectories: true
)
try Data((primary.1 + "\n").utf8).write(to: primaryURL)
let secondaryURL = directory.appendingPathComponent(secondary.0)
if let secondaryData = try? await Data(reading: secondaryURL),
let secondaryString = String(data: secondaryData, encoding: .utf8),
secondaryString.trimmingCharacters(in: .whitespacesAndNewlines) == secondary.1 {
try? FileManager.default.removeItem(at: secondaryURL)
}
if !quiet {
d.out.print("""
[LSP] Created \(primary.0) for enhanced IDE support. You may
need to restart your editor for the change to take effect.
You can disable this with `skipLSP: true` in xtool.yml.
""")
}
}
}