|
| 1 | +/** |
| 2 | + * Copyright 2021 Red Hat, Inc. and others. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { extensions, window } from "vscode"; |
| 17 | +import { QuarkusContext } from "../QuarkusContext"; |
| 18 | +import { installExtension, isExtensionInstalled } from "../utils/extensionInstallationUtils"; |
| 19 | + |
| 20 | +const TOOLS_FOR_MICRO_PROFILE_EXT = 'redhat.vscode-microprofile'; |
| 21 | + |
| 22 | +const STARTUP_INSTALL_MEMO = 'mpExtInstallOnStartup.isIgnored'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Prompts the user to install Tools for MicroProfile if they don't have it installed |
| 26 | + * |
| 27 | + * Allows the user to silence this prompt in the future by clicking on a button. |
| 28 | + * Warns the user that only some functionality is available if they choose not to install vscode-microprofile. |
| 29 | + * |
| 30 | + * @returns when the user has installed Tools for MicroProfile, |
| 31 | + * or the user has chosen not to install Tools for MicroProfile, |
| 32 | + * or its detected that they've silenced this popup |
| 33 | + */ |
| 34 | +export async function installMPExtOnStartup(): Promise<void> { |
| 35 | + if (isExtensionInstalled(TOOLS_FOR_MICRO_PROFILE_EXT)) { |
| 36 | + return; |
| 37 | + } |
| 38 | + const installOnStartupIsIgnored = QuarkusContext.getExtensionContext().globalState.get(STARTUP_INSTALL_MEMO, false); |
| 39 | + if (installOnStartupIsIgnored) { |
| 40 | + return; |
| 41 | + } |
| 42 | + const YES = 'Install'; |
| 43 | + const NO = 'Don\'t install'; |
| 44 | + const NOT_AGAIN = 'Don\'t ask me again'; |
| 45 | + const result = await window.showWarningMessage('vscode-quarkus depends on Tools for MicroProfile for many of its features, ' |
| 46 | + + 'but can provide some functionality without it. ' |
| 47 | + + 'Install Tools for MicroProfile now? ' |
| 48 | + + 'You will need to reload the window after the installation.', YES, NO, NOT_AGAIN); |
| 49 | + if (result === YES) { |
| 50 | + try { |
| 51 | + await installExtension(TOOLS_FOR_MICRO_PROFILE_EXT); |
| 52 | + } catch (e) { |
| 53 | + window.showErrorMessage(e); |
| 54 | + } |
| 55 | + } else if (result === NOT_AGAIN) { |
| 56 | + QuarkusContext.getExtensionContext().globalState.update(STARTUP_INSTALL_MEMO, true); |
| 57 | + limitedFunctionalityWarning(); |
| 58 | + } else { |
| 59 | + limitedFunctionalityWarning(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/** |
| 64 | + * Installs Tools for MicroProfile with the user's permission, in order to use a given command |
| 65 | + * |
| 66 | + * @param commandDescription description of the command that requires Tools for MicroProfile in order to be used |
| 67 | + * @returns when the user refuses to install, |
| 68 | + * or when the install succeeds, |
| 69 | + * or when the install fails |
| 70 | + */ |
| 71 | +export async function installMPExtForCommand(commandDescription: string) { |
| 72 | + const YES = 'Install'; |
| 73 | + const NO = `Cancel ${commandDescription}`; |
| 74 | + const result = await window.showWarningMessage(`${commandDescription} requires Tools for MicroProfile. Install it now? ` |
| 75 | + + 'You will need to reload the window after the installation.', |
| 76 | + YES, NO); |
| 77 | + if (result === YES) { |
| 78 | + try { |
| 79 | + await installExtension(TOOLS_FOR_MICRO_PROFILE_EXT); |
| 80 | + } catch (e) { |
| 81 | + window.showErrorMessage(e); |
| 82 | + } |
| 83 | + } else { |
| 84 | + window.showErrorMessage(`${commandDescription} requires Tools for MicroProfile, so it can't be run.`); |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +/** |
| 89 | + * Returns true if Tools for MicroProfile is installed, and false otherwise |
| 90 | + * |
| 91 | + * @returns true if Tools for MicroProfile is installed, and false otherwise |
| 92 | + */ |
| 93 | + export function isToolsForMicroProfileInstalled(): boolean { |
| 94 | + return isExtensionInstalled(TOOLS_FOR_MICRO_PROFILE_EXT); |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Returns when Tools for MicroProfile has started |
| 99 | + * |
| 100 | + * @returns when Tools for MicroProfile has started |
| 101 | + */ |
| 102 | +export async function microProfileToolsStarted(): Promise<void> { |
| 103 | + await extensions.getExtension(TOOLS_FOR_MICRO_PROFILE_EXT).activate(); |
| 104 | +} |
| 105 | + |
| 106 | +async function limitedFunctionalityWarning(): Promise<void> { |
| 107 | + await window.showInformationMessage('vscode-quarkus will run with limited functionality'); |
| 108 | +} |
0 commit comments