|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the VS Code Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2024 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 * as vscode from "vscode"; |
| 16 | +import * as fs from "fs/promises"; |
| 17 | +import * as path from "path"; |
| 18 | +import { DocumentationPreviewEditor } from "./DocumentationPreviewEditor"; |
| 19 | +import { WorkspaceContext } from "../WorkspaceContext"; |
| 20 | +import { FolderContext } from "../FolderContext"; |
| 21 | +import { createSwiftTask } from "../tasks/SwiftTaskProvider"; |
| 22 | +import { executeTaskWithUI, updateAfterError } from "../commands/utilities"; |
| 23 | + |
| 24 | +export class DocumentationManager { |
| 25 | + private previewEditor?: DocumentationPreviewEditor; |
| 26 | + |
| 27 | + constructor( |
| 28 | + private readonly extension: vscode.ExtensionContext, |
| 29 | + private readonly context: WorkspaceContext |
| 30 | + ) {} |
| 31 | + |
| 32 | + async launchDocumentationPreview(): Promise<void> { |
| 33 | + if (!this.previewEditor) { |
| 34 | + const folderContext = this.context.folders.at(0); |
| 35 | + if (!folderContext) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + const archive = await this.buildDocumentation(folderContext); |
| 40 | + if (!archive) { |
| 41 | + return; |
| 42 | + } |
| 43 | + |
| 44 | + this.previewEditor = new DocumentationPreviewEditor( |
| 45 | + archive, |
| 46 | + this.extension, |
| 47 | + this.context |
| 48 | + ); |
| 49 | + const subscriptions: vscode.Disposable[] = [ |
| 50 | + this.previewEditor.onDidDispose(() => { |
| 51 | + subscriptions.forEach(d => d.dispose()); |
| 52 | + this.previewEditor = undefined; |
| 53 | + }), |
| 54 | + ]; |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + async buildDocumentation(folderContext: FolderContext): Promise<string | undefined> { |
| 59 | + const buildPath = path.join(folderContext.folder.fsPath, ".build", "vscode-swift"); |
| 60 | + const outputPath = path.join(buildPath, "documentation-preview"); |
| 61 | + await fs.rm(outputPath, { recursive: true, force: true }); |
| 62 | + await fs.mkdir(outputPath, { recursive: true }); |
| 63 | + const task = createSwiftTask( |
| 64 | + [ |
| 65 | + "package", |
| 66 | + "--disable-sandbox", |
| 67 | + "generate-documentation", |
| 68 | + "--enable-experimental-combined-documentation", |
| 69 | + "--output-path", |
| 70 | + outputPath, |
| 71 | + ], |
| 72 | + "Build Documentation", |
| 73 | + { |
| 74 | + cwd: folderContext.folder, |
| 75 | + env: { |
| 76 | + DOCC_HTML_DIR: this.extension.asAbsolutePath("assets/swift-docc-render"), |
| 77 | + }, |
| 78 | + scope: folderContext.workspaceFolder, |
| 79 | + prefix: folderContext.name, |
| 80 | + presentationOptions: { reveal: vscode.TaskRevealKind.Silent }, |
| 81 | + }, |
| 82 | + folderContext.workspaceContext.toolchain |
| 83 | + ); |
| 84 | + const succeeded = await executeTaskWithUI(task, "Building Documentation", folderContext); |
| 85 | + updateAfterError(succeeded, folderContext); |
| 86 | + if (!succeeded) { |
| 87 | + vscode.window.showErrorMessage("Failed to build documentation via SwiftDocC"); |
| 88 | + return; |
| 89 | + } |
| 90 | + return outputPath; |
| 91 | + } |
| 92 | +} |
0 commit comments