Skip to content

Commit

Permalink
Ask to install to the latest version
Browse files Browse the repository at this point in the history
Signed-off-by: Maxime Dufour <[email protected]>
  • Loading branch information
outscale-mdr committed Apr 30, 2024
1 parent c3d722e commit a0f8f11
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
51 changes: 45 additions & 6 deletions src/components/osc_cost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -268,8 +268,8 @@ export async function isOscCostWorking(): Promise<boolean> {
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;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -460,3 +471,31 @@ async function addInstalledPathToExtension(path: string) {
await updateConfigurationParameter(OSC_COST_PARAMETER + ".oscCostPath", path);

}

export async function updateToLatestVersionInstalled(): Promise<null> {
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;
}
5 changes: 3 additions & 2 deletions src/configuration/listener.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
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() {
vscode.workspace.onDidChangeConfiguration((e) => {
// 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);
}
}
});
Expand Down
15 changes: 11 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(mainSelectedItem: T, allSelectedItems?: any[]): T[] {
Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit a0f8f11

Please sign in to comment.