Skip to content

Commit

Permalink
feat: deno upgrade prompt (#988)
Browse files Browse the repository at this point in the history
  • Loading branch information
nayeemrmn authored Nov 15, 2023
1 parent 5c13d5f commit 62b1f34
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 4 deletions.
23 changes: 21 additions & 2 deletions client/src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ import {
} from "./lsp_extensions";
import * as tasks from "./tasks";
import { DenoTestController, TestingFeature } from "./testing";
import type { DenoExtensionContext, TestCommandOptions } from "./types";
import type {
DenoExtensionContext,
DidUpgradeCheckParams,
TestCommandOptions,
} from "./types";
import { WelcomePanel } from "./welcome";
import {
assert,
Expand All @@ -38,6 +42,7 @@ import type {
Position,
} from "vscode-languageclient/node";
import { getWorkspacesEnabledInfo } from "./enable";
import { denoUpgradePromptAndExecute } from "./upgrade";

// deno-lint-ignore no-explicit-any
export type Callback = (...args: any[]) => unknown;
Expand Down Expand Up @@ -210,6 +215,16 @@ export function startLanguageServer(
);
extensionContext.serverCapabilities = client.initializeResult?.capabilities;
extensionContext.statusBar.refresh(extensionContext);
extensionContext.client.onNotification(
"deno/didUpgradeCheck",
(params: DidUpgradeCheckParams) => {
if (extensionContext.serverInfo) {
extensionContext.serverInfo.upgradeAvailable =
params.upgradeAvailable;
extensionContext.statusBar.refresh(extensionContext);
}
},
);

if (testingFeature.enabled) {
context.subscriptions.push(new DenoTestController(extensionContext));
Expand Down Expand Up @@ -399,11 +414,15 @@ export function welcome(
};
}

export function openOutput(
export function statusBarClicked(
_context: vscode.ExtensionContext,
extensionContext: DenoExtensionContext,
) {
return () => {
extensionContext.outputChannel.show(true);
if (extensionContext.serverInfo?.upgradeAvailable) {
// Async dispatch on purpose.
denoUpgradePromptAndExecute(extensionContext.serverInfo.upgradeAvailable);
}
};
}
2 changes: 1 addition & 1 deletion client/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ export async function activate(
registerCommand("deno.client.restart", commands.startLanguageServer);
registerCommand("deno.client.status", commands.status);
registerCommand("deno.client.welcome", commands.welcome);
registerCommand("deno.client.openOutput", commands.openOutput);
registerCommand("deno.client.statusBarClicked", commands.statusBarClicked);
}

export function deactivate(): Thenable<void> | undefined {
Expand Down
3 changes: 3 additions & 0 deletions client/src/server_info.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { InitializeResult } from "vscode-languageclient";
import { UpgradeAvailable } from "./types";

export class DenoServerInfo {
readonly #fullVersion: string;
upgradeAvailable: UpgradeAvailable | null;

constructor(serverInfo: InitializeResult["serverInfo"]) {
this.#fullVersion = serverInfo?.version ?? "";
this.upgradeAvailable = null;
}

/** Gets the version with configuration and architecture. Ex: x.x.x (release, x86_64-etc) */
Expand Down
5 changes: 4 additions & 1 deletion client/src/status_bar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export class DenoStatusBar {
vscode.StatusBarAlignment.Right,
0,
);
this.#inner.command = "deno.client.openOutput";
this.#inner.command = "deno.client.statusBarClicked";
}

dispose() {
Expand All @@ -22,6 +22,9 @@ export class DenoStatusBar {
refresh(extensionContext: DenoExtensionContext) {
if (extensionContext.serverInfo) {
this.#inner.text = `Deno ${extensionContext.serverInfo.version}`;
if (extensionContext.serverInfo.upgradeAvailable) {
this.#inner.text += " (Upgrade available)";
}
this.#inner.tooltip = extensionContext.serverInfo.versionWithBuildInfo;
}

Expand Down
1 change: 1 addition & 0 deletions client/src/tasks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class DenoTaskProvider implements vscode.TaskProvider {
group: vscode.TaskGroup.Test,
problemMatchers: ["$deno-test"],
},
{ command: "upgrade", group: undefined, problemMatchers: ["$deno"] },
];

const tasks: vscode.Task[] = [];
Expand Down
9 changes: 9 additions & 0 deletions client/src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,12 @@ export interface DenoExtensionContext {
export interface TestCommandOptions {
inspect: boolean;
}

export interface UpgradeAvailable {
latestVersion: string;
isCanary: boolean;
}

export interface DidUpgradeCheckParams {
upgradeAvailable: UpgradeAvailable | null;
}
68 changes: 68 additions & 0 deletions client/src/upgrade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.

import { EXTENSION_NS } from "./constants";
import * as tasks from "./tasks";
import { UpgradeAvailable } from "./types";
import { assert, getDenoCommandName } from "./util";
import * as vscode from "vscode";

export async function denoUpgradePromptAndExecute(
{ latestVersion, isCanary }: UpgradeAvailable,
) {
const config = vscode.workspace.getConfiguration(EXTENSION_NS);
let prompt = isCanary
? `A new canary release of Deno is available: ${latestVersion.slice(0, 7)}.`
: `A new release of Deno is available: ${latestVersion}.`;
prompt += " Would you like to upgrade?";
const selection = await vscode.window.showInformationMessage(
prompt,
"Upgrade",
"Dismiss",
);
if (selection !== "Upgrade") {
return;
}
const args = ["upgrade"];
if (config.get("unstable")) {
args.push("--unstable");
}
if (isCanary) {
args.push("--canary");
}
args.push("--version");
args.push(latestVersion);
const env = {} as Record<string, string>;
const cacheDir: string | undefined | null = config.get("cache");
if (cacheDir?.trim()) {
env["DENO_DIR"] = cacheDir.trim();
}
const definition: tasks.DenoTaskDefinition = {
type: tasks.TASK_TYPE,
command: "upgrade",
args,
env,
};
assert(vscode.workspace.workspaceFolders);
const target = vscode.workspace.workspaceFolders[0];
const denoCommand = await getDenoCommandName();
const task = tasks.buildDenoTask(
target,
denoCommand,
definition,
"upgrade",
args,
["$deno"],
);
task.presentationOptions = {
reveal: vscode.TaskRevealKind.Always,
panel: vscode.TaskPanelKind.Dedicated,
clear: true,
};
const execution = await vscode.tasks.executeTask(task);
const disposable = vscode.tasks.onDidEndTask((event) => {
if (event.execution == execution) {
disposable.dispose();
vscode.commands.executeCommand("deno.client.restart");
}
});
}

0 comments on commit 62b1f34

Please sign in to comment.