Skip to content

BACKPORT: Prompt to update PowerShell version (#2105) #2177

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

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
92 changes: 89 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@
"onView:PowerShellCommands"
],
"dependencies": {
"node-fetch": "^2.6.0",
"semver": "^6.3.0",
"vscode-extension-telemetry": "~0.1.2",
"vscode-languageclient": "~5.2.1"
},
"devDependencies": {
"@types/mocha": "~5.2.7",
"@types/node": "~10.11.0",
"@types/node-fetch": "^2.5.0",
"@types/rewire": "^2.5.28",
"@types/semver": "^6.0.1",
"mocha": "~5.2.0",
"mocha-junit-reporter": "~1.23.1",
"mocha-multi-reporters": "~1.1.7",
Expand Down Expand Up @@ -575,6 +579,11 @@
"type": "string",
"description": "Specifies the PowerShell version name, as displayed by the 'PowerShell: Show Session Menu' command, used when the extension loads e.g \"Windows PowerShell (x86)\" or \"PowerShell Core 6 (x64)\"."
},
"powershell.promptToUpdatePowerShell": {
"type": "boolean",
"description": "Specifies whether you should be prompted to update your version of PowerShell.",
"default": true
},
"powershell.startAutomatically": {
"type": "boolean",
"default": true,
Expand Down
146 changes: 146 additions & 0 deletions src/features/UpdatePowerShell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import fetch from "node-fetch";
import * as semver from "semver";
import { MessageItem, window } from "vscode";
import { LanguageClient } from "vscode-languageclient";
import * as Settings from "../settings";
import { EvaluateRequestType } from "./Console";

const PowerShellGitHubReleasesUrl =
"https://api.github.com/repos/PowerShell/PowerShell/releases/latest";
const PowerShellGitHubPrereleasesUrl =
"https://api.github.com/repos/PowerShell/PowerShell/releases";

export class GitHubReleaseInformation {
public static async FetchLatestRelease(preview: boolean): Promise<GitHubReleaseInformation> {
// Fetch the latest PowerShell releases from GitHub.
let releaseJson: any;
if (preview) {
// This gets all releases and the first one is the latest prerelease if
// there is a prerelease version.
releaseJson = (await fetch(PowerShellGitHubPrereleasesUrl)
.then((res) => res.json())).find((release: any) => release.prerelease);
} else {
releaseJson = await fetch(PowerShellGitHubReleasesUrl)
.then((res) => res.json());
}

return new GitHubReleaseInformation(
releaseJson.tag_name, releaseJson.assets);
}

public version: semver.SemVer;
public isPreview: boolean = false;
public assets: any[];

public constructor(version: string | semver.SemVer, assets: any[] = []) {
this.version = semver.parse(version);

if (semver.prerelease(this.version)) {
this.isPreview = true;
}

this.assets = assets;
}
}

interface IUpdateMessageItem extends MessageItem {
id: number;
}

export async function InvokePowerShellUpdateCheck(
languageServerClient: LanguageClient,
localVersion: semver.SemVer,
arch: string,
release: GitHubReleaseInformation) {
const options: IUpdateMessageItem[] = [
{
id: 0,
title: "Yes",
},
{
id: 1,
title: "Not now",
},
{
id: 2,
title: "Do not show this notification again",
},
];

// If our local version is up-to-date, we can return early.
if (semver.compare(localVersion, release.version) >= 0) {
return;
}

const commonText: string = `You have an old version of PowerShell (${
localVersion.raw
}). The current latest release is ${
release.version.raw
}.`;

if (process.platform === "linux") {
await window.showInformationMessage(
`${commonText} We recommend updating to the latest version.`);
return;
}

const isMacOS: boolean = process.platform === "darwin";
const result = await window.showInformationMessage(
`${commonText} Would you like to update the version? ${
isMacOS ? "(Homebrew is required on macOS)" : ""
}`, ...options);

// If the user cancels the notification.
if (!result) { return; }

// Yes choice.
switch (result.id) {
// Yes choice.
case 0:
let script: string;
if (process.platform === "win32") {
const msiMatcher = arch === "x86" ?
"win-x86.msi" : "win-x64.msi";

const assetUrl = release.assets.filter((asset: any) =>
asset.name.indexOf(msiMatcher) >= 0)[0].browser_download_url;

// Grab MSI and run it.
// tslint:disable-next-line: max-line-length
script = `
$randomFileName = [System.IO.Path]::GetRandomFileName()
$tmpMsiPath = Microsoft.PowerShell.Management\\Join-Path ([System.IO.Path]::GetTempPath()) "$randomFileName.msi"
Microsoft.PowerShell.Utility\\Invoke-RestMethod -Uri ${assetUrl} -OutFile $tmpMsiPath
try
{
Microsoft.PowerShell.Management\\Start-Process -Wait -Path $tmpMsiPath
}
finally
{
Microsoft.PowerShell.Management\\Remove-Item $tmpMsiPath
}`;

} else if (isMacOS) {
script = "brew cask upgrade powershell";
if (release.isPreview) {
script = "brew cask upgrade powershell-preview";
}
}

await languageServerClient.sendRequest(EvaluateRequestType, {
expression: script,
});
break;

// Never choice.
case 2:
await Settings.change("promptToUpdatePowerShell", false, true);
break;
default:
break;
}
}
Loading