1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
package import BuildServerProtocol
package import BuildSystemIntegration
package import Foundation
import LanguageServerProtocol
import SKLogging
package extension BuildSystemManager {
/// Retrieves the name of the Swift module for a given target.
///
/// **Note:** prefer using ``module(for:in:)`` over ths function. This function
/// only exists for cases where you want the Swift module name of a target where
/// you don't know one of its Swift document URIs in advance. E.g. when handling
/// requests for Markdown/Tutorial files in DocC since they don't have compile
/// commands that could be used to find the module name.
///
/// - Parameter target: The build target identifier
/// - Returns: The name of the Swift module or nil if it could not be determined
func moduleName(for target: BuildTargetIdentifier) async -> String? {
let sourceFiles =
await orLog(
"Failed to retreive source files from target \(target.uri)",
{ try await self.sourceFiles(in: [target]).flatMap(\.sources) }
) ?? []
for sourceFile in sourceFiles {
let language = await defaultLanguage(for: sourceFile.uri, in: target)
guard language == .swift else {
continue
}
if let moduleName = await moduleName(for: sourceFile.uri, in: target) {
return moduleName
}
}
return nil
}
/// Finds the SwiftDocC documentation catalog associated with a target, if any.
///
/// - Parameter target: The build target identifier
/// - Returns: The URL of the documentation catalog or nil if one could not be found
func doccCatalog(for target: BuildTargetIdentifier) async -> URL? {
let sourceFiles =
await orLog(
"Failed to retrieve source files from target \(target.uri)",
{ try await self.sourceFiles(in: [target]).flatMap(\.sources) }
) ?? []
let catalogURLs = sourceFiles.compactMap { sourceItem -> URL? in
guard sourceItem.dataKind == .sourceKit,
let data = SourceKitSourceItemData(fromLSPAny: sourceItem.data),
data.kind == .doccCatalog
else {
return nil
}
return sourceItem.uri.fileURL
}.sorted(by: { $0.absoluteString < $1.absoluteString })
if catalogURLs.count > 1 {
logger.error("Multiple SwiftDocC catalogs found in build target \(target.uri)")
}
return catalogURLs.first
}
}
|