Modern Apps git

xtool.git

Public
git clone https://git.maproj.org/xtool.git
appstore-distribution ▾

xtool.git / Sources / XToolSupport / LSPConfig.swift

LSPConfig.swift90 lines · 3.0 KiBRaw
1import Foundation
2import PackLib
3
4enum LSPConfig {
5 static let bsp: (String, String) = (
6 ".bsp/xtool.json",
7 """
8 {
9 "name": "xtool",
10 "version": "1.0",
11 "bspVersion": "2.2.0",
12 "languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
13 "argv": ["/usr/bin/env", "xtool", "dev", "build-server"]
14 }
15 """
16 )
17
18 static let legacyLSP: (String, String) = (
19 ".sourcekit-lsp/config.json",
20 """
21 {
22 "swiftPM": {
23 "swiftSDK": "arm64-apple-ios"
24 }
25 }
26 """
27 )
28
29 nonisolated(nonsending) static func tryEnsure(
30 using schema: PackSchema,
31 quiet: Bool = false,
32 currentDirectory directory: URL = URL(filePath: FileManager.default.currentDirectoryPath)
33 ) async {
34 do {
35 try await ensure(using: schema, quiet: quiet, currentDirectory: directory)
36 } catch {
37 print("warning: failed to validate LSP configuration: \(error)")
38 }
39 }
40
41 nonisolated(nonsending) static func ensure(
42 using schema: PackSchema,
43 quiet: Bool = false,
44 currentDirectory directory: URL = URL(filePath: FileManager.default.currentDirectoryPath)
45 ) async throws {
46 if schema.skipLSP == true {
47 return
48 }
49
50 if try await SwiftVersion.current.supportsSwiftBuild {
51 try await ensure(primary: bsp, secondary: legacyLSP, quiet: quiet, currentDirectory: directory)
52 } else {
53 try await ensure(primary: legacyLSP, secondary: bsp, quiet: quiet, currentDirectory: directory)
54 }
55 }
56
57 // creates the primary LSP-support file if it doesn't exist, and cleans up old files
58 private nonisolated(nonsending) static func ensure(
59 primary: (String, String),
60 secondary: (String, String),
61 quiet: Bool,
62 currentDirectory directory: URL
63 ) async throws {
64 let primaryURL = directory.appendingPathComponent(primary.0)
65 if FileManager.default.fileExists(atPath: primaryURL.path) {
66 return
67 }
68
69 try? FileManager.default.createDirectory(
70 at: primaryURL.deletingLastPathComponent(),
71 withIntermediateDirectories: true
72 )
73 try Data((primary.1 + "\n").utf8).write(to: primaryURL)
74
75 let secondaryURL = directory.appendingPathComponent(secondary.0)
76 if let secondaryData = try? await Data(reading: secondaryURL),
77 let secondaryString = String(data: secondaryData, encoding: .utf8),
78 secondaryString.trimmingCharacters(in: .whitespacesAndNewlines) == secondary.1 {
79 try? FileManager.default.removeItem(at: secondaryURL)
80 }
81
82 if !quiet {
83 print("""
84 [LSP] Created \(primary.0) for enhanced IDE support. You may
85 need to restart your editor for the change to take effect.
86 You can disable this with `skipLSP: true` in xtool.yml.
87 """)
88 }
89 }
90}