Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alert user to install osc-cost to latest version #118

Merged
merged 2 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 52 additions & 17 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 All @@ -352,12 +363,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;
}
Expand Down Expand Up @@ -397,14 +408,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<null> {
async function installOscCost(p?: vscode.Progress<{ message?: string; increment?: number }>): Promise<string> {
const tool = 'osc-cost';
const extension = (shell.isUnix()) ? '' : '.exe';
const platform = shell.platform();
Expand All @@ -426,7 +437,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);
Expand All @@ -453,14 +464,38 @@ 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));
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
Loading