|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 the VS Code Swift project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of VS Code Swift project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +import { join } from "path"; |
| 16 | +import * as vscode from "vscode"; |
| 17 | +import { FolderContext } from "../FolderContext"; |
| 18 | +import { selectFolder } from "../ui/SelectFolderQuickPick"; |
| 19 | +import { WorkspaceContext } from "../WorkspaceContext"; |
| 20 | +import configuration from "../configuration"; |
| 21 | + |
| 22 | +export async function generateSourcekitConfiguration(ctx: WorkspaceContext): Promise<boolean> { |
| 23 | + if (ctx.folders.length === 0) { |
| 24 | + return false; |
| 25 | + } |
| 26 | + |
| 27 | + if (ctx.folders.length === 1) { |
| 28 | + const folder = ctx.folders[0]; |
| 29 | + const success = await createSourcekitConfiguration(ctx, folder); |
| 30 | + void vscode.window.showTextDocument(vscode.Uri.file(sourcekitConfigFilePath(folder))); |
| 31 | + return success; |
| 32 | + } |
| 33 | + |
| 34 | + const foldersToGenerate: FolderContext[] = await selectFolder( |
| 35 | + ctx, |
| 36 | + "Select a folder to generate a SourceKit-LSP configuration for" |
| 37 | + ); |
| 38 | + if (!foldersToGenerate.length) { |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + return ( |
| 43 | + await Promise.all( |
| 44 | + foldersToGenerate.map(folder => createSourcekitConfiguration(ctx, folder)) |
| 45 | + ) |
| 46 | + ).reduceRight((prev, curr) => prev || curr); |
| 47 | +} |
| 48 | + |
| 49 | +export const sourcekitFolderPath = (f: FolderContext) => join(f.folder.fsPath, ".sourcekit-lsp"); |
| 50 | +export const sourcekitConfigFilePath = (f: FolderContext) => |
| 51 | + join(sourcekitFolderPath(f), "config.json"); |
| 52 | + |
| 53 | +async function createSourcekitConfiguration( |
| 54 | + workspaceContext: WorkspaceContext, |
| 55 | + folderContext: FolderContext |
| 56 | +): Promise<boolean> { |
| 57 | + const sourcekitFolder = vscode.Uri.file(sourcekitFolderPath(folderContext)); |
| 58 | + const sourcekitConfigFile = vscode.Uri.file(sourcekitConfigFilePath(folderContext)); |
| 59 | + |
| 60 | + try { |
| 61 | + await vscode.workspace.fs.stat(sourcekitConfigFile); |
| 62 | + return true; |
| 63 | + } catch (error) { |
| 64 | + if ((error as NodeJS.ErrnoException).code !== "ENOENT") { |
| 65 | + workspaceContext.outputChannel.appendLine( |
| 66 | + `Failed to read file at ${sourcekitConfigFile.fsPath}: ${error}` |
| 67 | + ); |
| 68 | + } |
| 69 | + // Ignore, don't care if the file doesn't exist yet |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + const stats = await vscode.workspace.fs.stat(sourcekitFolder); |
| 74 | + if (stats.type !== vscode.FileType.Directory) { |
| 75 | + void vscode.window.showErrorMessage( |
| 76 | + `File ${sourcekitFolder.fsPath} already exists but is not a directory` |
| 77 | + ); |
| 78 | + return false; |
| 79 | + } |
| 80 | + } catch (error) { |
| 81 | + if ((error as NodeJS.ErrnoException).code !== "ENOENT") { |
| 82 | + workspaceContext.outputChannel.appendLine( |
| 83 | + `Failed to read folder at ${sourcekitFolder.fsPath}: ${error}` |
| 84 | + ); |
| 85 | + } |
| 86 | + await vscode.workspace.fs.createDirectory(sourcekitFolder); |
| 87 | + } |
| 88 | + const version = folderContext.toolchain.swiftVersion; |
| 89 | + const versionString = `${version.major}.${version.minor}`; |
| 90 | + let branch = |
| 91 | + configuration.lsp.configurationBranch || |
| 92 | + (version.dev ? "main" : `release/${versionString}`); |
| 93 | + if (!(await checkURLExists(schemaURL(branch)))) { |
| 94 | + branch = "main"; |
| 95 | + } |
| 96 | + await vscode.workspace.fs.writeFile( |
| 97 | + sourcekitConfigFile, |
| 98 | + Buffer.from( |
| 99 | + JSON.stringify( |
| 100 | + { |
| 101 | + $schema: schemaURL(branch), |
| 102 | + }, |
| 103 | + undefined, |
| 104 | + 2 |
| 105 | + ) |
| 106 | + ) |
| 107 | + ); |
| 108 | + return true; |
| 109 | +} |
| 110 | + |
| 111 | +const schemaURL = (branch: string) => |
| 112 | + `https://raw.githubusercontent.com/swiftlang/sourcekit-lsp/refs/heads/${branch}/config.schema.json`; |
| 113 | + |
| 114 | +async function checkURLExists(url: string): Promise<boolean> { |
| 115 | + try { |
| 116 | + const response = await fetch(url, { method: "HEAD" }); |
| 117 | + return response.ok; |
| 118 | + } catch { |
| 119 | + return false; |
| 120 | + } |
| 121 | +} |
0 commit comments