Skip to content

Prompt to update PowerShell version #2105

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
merged 8 commits into from
Sep 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
112 changes: 112 additions & 0 deletions src/features/UpdatePowerShell.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import fetch from "node-fetch";
import { compare, parse, prerelease, SemVer } from "semver";
import { MessageItem, window } from "vscode";
import Settings = require("../settings");
import { EvaluateRequestType } from "./Console";

const PowerShellGitHubReleasesUrl =
"https://api.github.com/repos/PowerShell/PowerShell/releases/latest";
const PowerShellGitHubRPrereleasesUrl =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PowerShellGitHubRPrereleasesUrl --> 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(PowerShellGitHubRPrereleasesUrl)
.then((res) => res.json()))[0];
} else {
releaseJson = await fetch(PowerShellGitHubReleasesUrl)
.then((res) => res.json());
}

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

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

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

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

this.assets = assets;
}
}

interface IUpdateMessageItem extends MessageItem {
id: number;
}

export async function InvokePowerShellUpdateCheck(
localVersion: SemVer, arch: string, release: GitHubReleaseInformation) {
const options: IUpdateMessageItem[] = [
{
id: 0,
title: "Yes!",
},
{
id: 1,
title: "Not now.",
},
{
id: 2,
title: "Never...",
},
];

if (compare(release.version, localVersion) > 0) {
const result = await window.showInformationMessage(
`You have an old version of PowerShell (${
localVersion.raw
}). The current latest release is ${
release.version.raw
}. Would you like to update the version?`, ...options);

// Yes choice.
if (result.id === 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].url;

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

} else if (process.platform === "darwin") {
script = "brew cask upgrade powershell";
if (release.isPreview) {
script = "brew cask upgrade powershell-preview";
}
} else if (process.platform === "linux") {
window.showWarningMessage(
"Update through VS Code not supported on Linux.");
return;
}
await this.languageServerClient.sendRequest(EvaluateRequestType, {
expression: script,
});
// Never choice.
} else if (result.id === 2) {
await Settings.change("promptToUpdatePowerShell", false, true);
}
}
}
Loading