|
| 1 | +/* |
| 2 | + * api.ts |
| 3 | + * |
| 4 | + * Copyright (C) 2026 by Posit Software, PBC |
| 5 | + * |
| 6 | + * Unless you have received this program directly from Posit Software pursuant |
| 7 | + * to the terms of a commercial license agreement with Posit Software, then |
| 8 | + * this program is licensed to you under the terms of version 3 of the |
| 9 | + * GNU Affero General Public License. This program is distributed WITHOUT |
| 10 | + * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, |
| 11 | + * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the |
| 12 | + * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. |
| 13 | + * |
| 14 | + */ |
| 15 | + |
| 16 | +import { QuartoContext } from "quarto-core"; |
| 17 | + |
| 18 | +/** |
| 19 | + * Public API for the Quarto extension. |
| 20 | + * |
| 21 | + * Other extensions can access this API to get information about the Quarto CLI |
| 22 | + * that the Quarto extension is using. This is useful when you need to know |
| 23 | + * the exact Quarto binary path, including when Quarto is bundled in Positron |
| 24 | + * or installed in a Python virtual environment. |
| 25 | + * |
| 26 | + * ## Usage from another extension |
| 27 | + * |
| 28 | + * Since your extension cannot import types from the Quarto extension directly, |
| 29 | + * copy this interface definition into your own codebase: |
| 30 | + * |
| 31 | + * ```typescript |
| 32 | + * // Copy this interface into your extension |
| 33 | + * interface QuartoExtensionApi { |
| 34 | + * getQuartoPath(): string | undefined; |
| 35 | + * getQuartoVersion(): string | undefined; |
| 36 | + * isQuartoAvailable(): boolean; |
| 37 | + * } |
| 38 | + * |
| 39 | + * // Then use it like this: |
| 40 | + * async function getQuartoPathFromExtension(): Promise<string | undefined> { |
| 41 | + * const quartoExt = vscode.extensions.getExtension('quarto.quarto'); |
| 42 | + * if (!quartoExt) { |
| 43 | + * return undefined; |
| 44 | + * } |
| 45 | + * if (!quartoExt.isActive) { |
| 46 | + * await quartoExt.activate(); |
| 47 | + * } |
| 48 | + * const api = quartoExt.exports as QuartoExtensionApi; |
| 49 | + * return api.getQuartoPath(); |
| 50 | + * } |
| 51 | + * ``` |
| 52 | + */ |
| 53 | +export interface QuartoExtensionApi { |
| 54 | + /** |
| 55 | + * Get the path to the Quarto CLI binary that the extension is using. |
| 56 | + * Returns undefined if Quarto is not available. |
| 57 | + */ |
| 58 | + getQuartoPath(): string | undefined; |
| 59 | + |
| 60 | + /** |
| 61 | + * Get the version of Quarto that the extension is using. |
| 62 | + * Returns undefined if Quarto is not available. |
| 63 | + */ |
| 64 | + getQuartoVersion(): string | undefined; |
| 65 | + |
| 66 | + /** |
| 67 | + * Check if Quarto is available. |
| 68 | + */ |
| 69 | + isQuartoAvailable(): boolean; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Create the public API for the Quarto extension. |
| 74 | + */ |
| 75 | +export function createQuartoExtensionApi(quartoContext: QuartoContext): QuartoExtensionApi { |
| 76 | + return { |
| 77 | + getQuartoPath(): string | undefined { |
| 78 | + if (!quartoContext.available) { |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + return quartoContext.binPath; |
| 82 | + }, |
| 83 | + |
| 84 | + getQuartoVersion(): string | undefined { |
| 85 | + if (!quartoContext.available) { |
| 86 | + return undefined; |
| 87 | + } |
| 88 | + return quartoContext.version; |
| 89 | + }, |
| 90 | + |
| 91 | + isQuartoAvailable(): boolean { |
| 92 | + return quartoContext.available; |
| 93 | + }, |
| 94 | + }; |
| 95 | +} |
0 commit comments