From 73972594980356571e460be3300d7053acf584bd Mon Sep 17 00:00:00 2001 From: Maxime Dufour Date: Thu, 25 Apr 2024 13:08:55 +0000 Subject: [PATCH 1/2] Install to the app with the version in the name Signed-off-by: Maxime Dufour --- src/components/osc_cost.ts | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/src/components/osc_cost.ts b/src/components/osc_cost.ts index ea5b1ea..952c1f6 100644 --- a/src/components/osc_cost.ts +++ b/src/components/osc_cost.ts @@ -352,12 +352,12 @@ export async function showErrorMessageWithInstallPrompt() { // eslint-disable-next-line @typescript-eslint/no-unused-vars async (p, _) => { p.report({ message: vscode.l10n.t("Installing the latest stable version of {0}", tool) }); - await installOscCost(p).catch((reason: string) => { + const path = await installOscCost(p).catch((reason: string) => { vscode.window.showErrorMessage(vscode.l10n.t("Error while installing {0}: {1}", tool, reason)); throw vscode.l10n.t("Error while installing {0}: {1}", tool, reason); }); p.report({ message: vscode.l10n.t("Adding the path to the user config") }); - await addInstalledPathToExtension(); + await addInstalledPathToExtension(path); }); break; } @@ -397,14 +397,14 @@ function defaultInstalledPath(): string { return path.join(os.homedir(), `.vs-osc_viewer`); } -function defaultBinName(): string { +function defaultBinName(version: string): string { const tool = 'osc-cost'; const extension = (shell.isUnix()) ? '' : '.exe'; - return `${tool}${extension}`; + return `${tool}-${version}${extension}`; } -async function installOscCost(p?: vscode.Progress<{ message?: string; increment?: number }>): Promise { +async function installOscCost(p?: vscode.Progress<{ message?: string; increment?: number }>): Promise { const tool = 'osc-cost'; const extension = (shell.isUnix()) ? '' : '.exe'; const platform = shell.platform(); @@ -426,7 +426,7 @@ async function installOscCost(p?: vscode.Progress<{ message?: string; increment? p?.report({ message: vscode.l10n.t("Latest stable version found is {0}", version) }); const targetDir = defaultInstalledPath(); - const binName = defaultBinName(); + const binName = defaultBinName(version); if (!pathExists(targetDir)) { fs.mkdirSync(targetDir); @@ -453,14 +453,10 @@ async function installOscCost(p?: vscode.Progress<{ message?: string; increment? fs.chmodSync(downloadFile, '0750'); } - return null; + return downloadFile; } -async function addInstalledPathToExtension() { +async function addInstalledPathToExtension(path: string) { + await updateConfigurationParameter(OSC_COST_PARAMETER + ".oscCostPath", path); - const targetDir = defaultInstalledPath(); - const binName = defaultBinName(); - - await updateConfigurationParameter(OSC_COST_PARAMETER + ".oscCostPath", path.join(targetDir, binName)); - -} \ No newline at end of file +} From 964ce281e32f458feb2cb24c9de2f798f4ae3483 Mon Sep 17 00:00:00 2001 From: Maxime Dufour Date: Thu, 25 Apr 2024 13:14:08 +0000 Subject: [PATCH 2/2] Ask to install to the latest version Signed-off-by: Maxime Dufour --- src/components/osc_cost.ts | 51 ++++++++++++++++++++++++++++++----- src/configuration/listener.ts | 5 ++-- src/extension.ts | 15 ++++++++--- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/src/components/osc_cost.ts b/src/components/osc_cost.ts index 952c1f6..12d1439 100644 --- a/src/components/osc_cost.ts +++ b/src/components/osc_cost.ts @@ -12,7 +12,7 @@ import { VOLUME_FOLDER_NAME } from "../flat/folders/specific/node.folder.volume" import { Profile, ResourceNodeType } from "../flat/node"; import { OutputChannel } from "../logs/output_channel"; import { Platform, platformArch, shell } from "./shell"; -import { satisfies } from 'compare-versions'; +import { compareVersions, satisfies } from 'compare-versions'; import * as fs from 'fs'; import * as path from 'path'; @@ -268,8 +268,8 @@ export async function isOscCostWorking(): Promise { const oscCostPath = getOscCostPath(); if (typeof oscCostPath === 'undefined') { - vscode.window.showErrorMessage(vscode.l10n.t("{0} is not found", "osc-cost")); - showErrorMessageWithInstallPrompt(); + const message = vscode.l10n.t("{0} is not found. Do you want to install it ?", "osc-cost"); + showMessageWithInstallPrompt(vscode.LogLevel.Error, message); return false; } @@ -328,13 +328,24 @@ function getDefaultOptions(version: string): string | undefined { return options[0][1]; } -export async function showErrorMessageWithInstallPrompt() { +export async function showMessageWithInstallPrompt(level: vscode.LogLevel.Error | vscode.LogLevel.Warning | vscode.LogLevel.Info, message: string) { const tool = "osc-cost"; - const message = vscode.l10n.t("{0} is not found. Do you want to install it ?", tool); const yes = vscode.l10n.t('Yes'); const noManually = vscode.l10n.t('No, open the documentation'); const no = vscode.l10n.t('No'); - const choice = await vscode.window.showErrorMessage(message, yes, noManually, no); + + let choice: string | undefined; + switch (level) { + case vscode.LogLevel.Info: + choice = await vscode.window.showInformationMessage(message, yes, noManually, no); + break; + case vscode.LogLevel.Warning: + choice = await vscode.window.showWarningMessage(message, yes, noManually, no); + break; + case vscode.LogLevel.Error: + choice = await vscode.window.showErrorMessage(message, yes, noManually, no); + break; + } switch (choice) { case no: return; @@ -460,3 +471,31 @@ async function addInstalledPathToExtension(path: string) { await updateConfigurationParameter(OSC_COST_PARAMETER + ".oscCostPath", path); } + +export async function updateToLatestVersionInstalled(): Promise { + const oscCostPath = getOscCostPath(); + + if (typeof oscCostPath === 'undefined') { + vscode.window.showErrorMessage(vscode.l10n.t("{0} is not found", "osc-cost")); + return Promise.reject(); + } + + const localOscVersion = await getOscCostVersion(oscCostPath); + if (typeof localOscVersion === 'undefined') { + vscode.window.showErrorMessage(vscode.l10n.t("Error while retrieving the version of {0}", "osc-cost")); + return Promise.reject(); + } + + const latestVersionAvailable = await getStableVersion(); + if (typeof latestVersionAvailable === 'undefined') { + vscode.window.showErrorMessage(vscode.l10n.t("Error while retrieving the latest available version of {0}", "osc-cost")); + return Promise.reject(); + } + + if (compareVersions(localOscVersion, latestVersionAvailable) < 0) { + const message = vscode.l10n.t("A new version of {0} is available, do you want to update it? ({1} -> {2})", "osc-cost", `v${localOscVersion}`, latestVersionAvailable); + await showMessageWithInstallPrompt(vscode.LogLevel.Warning, message); + } + + return null; +} \ No newline at end of file diff --git a/src/configuration/listener.ts b/src/configuration/listener.ts index 1d27e99..76da888 100644 --- a/src/configuration/listener.ts +++ b/src/configuration/listener.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; import { CONFIGURATION_NAME, OSC_COST_PARAMETER } from "../configuration/utils"; -import { isOscCostEnabled, isOscCostFound, showErrorMessageWithInstallPrompt } from '../components/osc_cost'; +import { isOscCostEnabled, isOscCostFound, showMessageWithInstallPrompt } from '../components/osc_cost'; export function handleOscViewerUpdateConf() { @@ -8,7 +8,8 @@ export function handleOscViewerUpdateConf() { // Osc-Cost enabled if (e.affectsConfiguration(`${CONFIGURATION_NAME}.${OSC_COST_PARAMETER}.enabled`)) { if (isOscCostEnabled() && !(isOscCostFound())) { - showErrorMessageWithInstallPrompt(); + const message = vscode.l10n.t("{0} is not found. Do you want to install it ?", "osc-cost"); + showMessageWithInstallPrompt(vscode.LogLevel.Error, message); } } }); diff --git a/src/extension.ts b/src/extension.ts index 335328b..502903f 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -19,7 +19,7 @@ import { SubResourceNode } from './flat/resources/types/node.resources.subresour import { NetResourceNode } from './flat/resources/node.resources.nets'; import { init } from './network/networkview'; import { OscLinkProvider } from './virtual_filesystem/osclinkprovider'; -import { isOscCostEnabled, isOscCostFound, showErrorMessageWithInstallPrompt } from './components/osc_cost'; +import { updateToLatestVersionInstalled, isOscCostEnabled, isOscCostFound, showMessageWithInstallPrompt } from './components/osc_cost'; import { handleOscViewerUpdateConf } from './configuration/listener'; function getMultipleSelection(mainSelectedItem: T, allSelectedItems?: any[]): T[] { @@ -295,13 +295,20 @@ export function activate(context: vscode.ExtensionContext) { init(arg.profile, arg.resourceId, context); }); - if (isOscCostEnabled() && !isOscCostFound()) { - showErrorMessageWithInstallPrompt(); - } // Watch Conf Update handleOscViewerUpdateConf(); + // Check latest version of osc-cost if enabled + if (isOscCostEnabled()) { + if (!isOscCostFound()) { + const message = vscode.l10n.t("{0} is not found. Do you want to install it ?", "osc-cost"); + showMessageWithInstallPrompt(vscode.LogLevel.Error, message); + } else { + updateToLatestVersionInstalled(); + } + } + } // this method is called when your extension is deactivated export function deactivate() {