-
Notifications
You must be signed in to change notification settings - Fork 514
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
TylerLeonhardt
merged 8 commits into
PowerShell:master
from
TylerLeonhardt:update-powershell
Sep 16, 2019
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
220cbed
prompt to update PowerShell version
TylerLeonhardt d7612e9
address all feedback
TylerLeonhardt 3fd99cc
say homebrew is used
TylerLeonhardt ba9f72a
Feedback
TylerLeonhardt 83abb62
Feedback
TylerLeonhardt 108c140
PR feedback
TylerLeonhardt 038d7ad
Merge branch 'update-powershell' of github.com:TylerLeonhardt/vscode-…
TylerLeonhardt e56213a
use correct url from github api
TylerLeonhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { compare, parse, prerelease, SemVer } from "semver"; | ||
import { MessageItem, window } from "vscode"; | ||
import { LanguageClient } from "vscode-languageclient"; | ||
import Settings = require("../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; | ||
public isPreview: boolean = false; | ||
public assets: any[]; | ||
|
||
public constructor(version: string | SemVer, assets: any[] = []) { | ||
this.version = parse(version); | ||
TylerLeonhardt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if (prerelease(this.version)) { | ||
this.isPreview = true; | ||
} | ||
|
||
this.assets = assets; | ||
} | ||
} | ||
|
||
interface IUpdateMessageItem extends MessageItem { | ||
id: number; | ||
} | ||
|
||
export async function InvokePowerShellUpdateCheck( | ||
languageServerClient: LanguageClient, | ||
localVersion: 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 (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].url; | ||
|
||
// Grab MSI and run it. | ||
// tslint:disable-next-line: max-line-length | ||
script = ` | ||
$randomFileName = [System.IO.Path]::GetRandomFileName() | ||
rjmholt marked this conversation as resolved.
Show resolved
Hide resolved
|
||
$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; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.