|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import ArgumentParser |
| 14 | +import Foundation |
| 15 | +import InProcessClient |
| 16 | +import LanguageServerProtocol |
| 17 | +import SKCore |
| 18 | +import SKSupport |
| 19 | +import SourceKitLSP |
| 20 | + |
| 21 | +import struct TSCBasic.AbsolutePath |
| 22 | +import class TSCBasic.Process |
| 23 | +import var TSCBasic.stderrStream |
| 24 | +import class TSCUtility.PercentProgressAnimation |
| 25 | + |
| 26 | +private actor IndexLogMessageHandler: MessageHandler { |
| 27 | + var hasSeenError: Bool = false |
| 28 | + |
| 29 | + /// Queue to ensure that we don't have two interleaving `print` calls. |
| 30 | + let queue = AsyncQueue<Serial>() |
| 31 | + |
| 32 | + nonisolated func handle(_ notification: some NotificationType) { |
| 33 | + if let notification = notification as? LogMessageNotification { |
| 34 | + queue.async { |
| 35 | + await self.handle(notification) |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + func handle(_ notification: LogMessageNotification) { |
| 41 | + self.hasSeenError = notification.type == .warning |
| 42 | + print(notification.message) |
| 43 | + } |
| 44 | + |
| 45 | + nonisolated func handle<Request: RequestType>( |
| 46 | + _ request: Request, |
| 47 | + id: RequestID, |
| 48 | + reply: @escaping @Sendable (LSPResult<Request.Response>) -> Void |
| 49 | + ) { |
| 50 | + reply(.failure(.methodNotFound(Request.method))) |
| 51 | + } |
| 52 | + |
| 53 | +} |
| 54 | + |
| 55 | +public struct IndexCommand: AsyncParsableCommand { |
| 56 | + public static let configuration: CommandConfiguration = CommandConfiguration( |
| 57 | + commandName: "index", |
| 58 | + abstract: "Index a project and print all the processes executed for it as well as their outputs", |
| 59 | + shouldDisplay: false |
| 60 | + ) |
| 61 | + |
| 62 | + @Option( |
| 63 | + name: .customLong("toolchain"), |
| 64 | + help: """ |
| 65 | + The toolchain used to reduce the sourcekitd issue. \ |
| 66 | + If not specified, the toolchain is found in the same way that sourcekit-lsp finds it |
| 67 | + """ |
| 68 | + ) |
| 69 | + var toolchainOverride: String? |
| 70 | + |
| 71 | + @Option(help: "The path to the project that should be indexed") |
| 72 | + var project: String |
| 73 | + |
| 74 | + public init() {} |
| 75 | + |
| 76 | + public func run() async throws { |
| 77 | + var serverOptions = SourceKitLSPServer.Options() |
| 78 | + serverOptions.indexOptions.enableBackgroundIndexing = true |
| 79 | + |
| 80 | + let installPath = |
| 81 | + if let toolchainOverride, let toolchain = Toolchain(try AbsolutePath(validating: toolchainOverride)) { |
| 82 | + toolchain.path |
| 83 | + } else { |
| 84 | + try AbsolutePath(validating: Bundle.main.bundlePath) |
| 85 | + } |
| 86 | + |
| 87 | + let messageHandler = IndexLogMessageHandler() |
| 88 | + let inProcessClient = try await InProcessSourceKitLSPClient( |
| 89 | + toolchainRegistry: ToolchainRegistry(installPath: installPath), |
| 90 | + serverOptions: serverOptions, |
| 91 | + workspaceFolders: [WorkspaceFolder(uri: DocumentURI(URL(fileURLWithPath: project)))], |
| 92 | + messageHandler: messageHandler |
| 93 | + ) |
| 94 | + let start = ContinuousClock.now |
| 95 | + _ = try await inProcessClient.send(PollIndexRequest()) |
| 96 | + print("Indexing finished in \(start.duration(to: .now))") |
| 97 | + if await messageHandler.hasSeenError { |
| 98 | + throw ExitCode(1) |
| 99 | + } |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +fileprivate extension SourceKitLSPServer { |
| 104 | + func handle<R: RequestType>(_ request: R, requestID: RequestID) async throws -> R.Response { |
| 105 | + return try await withCheckedThrowingContinuation { continuation in |
| 106 | + self.handle(request, id: requestID) { result in |
| 107 | + continuation.resume(with: result) |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments