|
| 1 | +/*--------------------------------------------------------- |
| 2 | + * Copyright (C) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------*/ |
| 4 | + |
| 5 | +import fetch from "node-fetch"; |
| 6 | +import * as semver from "semver"; |
| 7 | +import { MessageItem, window } from "vscode"; |
| 8 | +import { LanguageClient } from "vscode-languageclient"; |
| 9 | +import * as Settings from "../settings"; |
| 10 | +import { EvaluateRequestType } from "./Console"; |
| 11 | + |
| 12 | +const PowerShellGitHubReleasesUrl = |
| 13 | + "https://api.github.com/repos/PowerShell/PowerShell/releases/latest"; |
| 14 | +const PowerShellGitHubPrereleasesUrl = |
| 15 | + "https://api.github.com/repos/PowerShell/PowerShell/releases"; |
| 16 | + |
| 17 | +export class GitHubReleaseInformation { |
| 18 | + public static async FetchLatestRelease(preview: boolean): Promise<GitHubReleaseInformation> { |
| 19 | + // Fetch the latest PowerShell releases from GitHub. |
| 20 | + let releaseJson: any; |
| 21 | + if (preview) { |
| 22 | + // This gets all releases and the first one is the latest prerelease if |
| 23 | + // there is a prerelease version. |
| 24 | + releaseJson = (await fetch(PowerShellGitHubPrereleasesUrl) |
| 25 | + .then((res) => res.json())).find((release: any) => release.prerelease); |
| 26 | + } else { |
| 27 | + releaseJson = await fetch(PowerShellGitHubReleasesUrl) |
| 28 | + .then((res) => res.json()); |
| 29 | + } |
| 30 | + |
| 31 | + return new GitHubReleaseInformation( |
| 32 | + releaseJson.tag_name, releaseJson.assets); |
| 33 | + } |
| 34 | + |
| 35 | + public version: semver.SemVer; |
| 36 | + public isPreview: boolean = false; |
| 37 | + public assets: any[]; |
| 38 | + |
| 39 | + public constructor(version: string | semver.SemVer, assets: any[] = []) { |
| 40 | + this.version = semver.parse(version); |
| 41 | + |
| 42 | + if (semver.prerelease(this.version)) { |
| 43 | + this.isPreview = true; |
| 44 | + } |
| 45 | + |
| 46 | + this.assets = assets; |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +interface IUpdateMessageItem extends MessageItem { |
| 51 | + id: number; |
| 52 | +} |
| 53 | + |
| 54 | +export async function InvokePowerShellUpdateCheck( |
| 55 | + languageServerClient: LanguageClient, |
| 56 | + localVersion: semver.SemVer, |
| 57 | + arch: string, |
| 58 | + release: GitHubReleaseInformation) { |
| 59 | + const options: IUpdateMessageItem[] = [ |
| 60 | + { |
| 61 | + id: 0, |
| 62 | + title: "Yes", |
| 63 | + }, |
| 64 | + { |
| 65 | + id: 1, |
| 66 | + title: "Not now", |
| 67 | + }, |
| 68 | + { |
| 69 | + id: 2, |
| 70 | + title: "Do not show this notification again", |
| 71 | + }, |
| 72 | + ]; |
| 73 | + |
| 74 | + // If our local version is up-to-date, we can return early. |
| 75 | + if (semver.compare(localVersion, release.version) >= 0) { |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + const commonText: string = `You have an old version of PowerShell (${ |
| 80 | + localVersion.raw |
| 81 | + }). The current latest release is ${ |
| 82 | + release.version.raw |
| 83 | + }.`; |
| 84 | + |
| 85 | + if (process.platform === "linux") { |
| 86 | + await window.showInformationMessage( |
| 87 | + `${commonText} We recommend updating to the latest version.`); |
| 88 | + return; |
| 89 | + } |
| 90 | + |
| 91 | + const isMacOS: boolean = process.platform === "darwin"; |
| 92 | + const result = await window.showInformationMessage( |
| 93 | + `${commonText} Would you like to update the version? ${ |
| 94 | + isMacOS ? "(Homebrew is required on macOS)" : "" |
| 95 | + }`, ...options); |
| 96 | + |
| 97 | + // If the user cancels the notification. |
| 98 | + if (!result) { return; } |
| 99 | + |
| 100 | + // Yes choice. |
| 101 | + switch (result.id) { |
| 102 | + // Yes choice. |
| 103 | + case 0: |
| 104 | + let script: string; |
| 105 | + if (process.platform === "win32") { |
| 106 | + const msiMatcher = arch === "x86" ? |
| 107 | + "win-x86.msi" : "win-x64.msi"; |
| 108 | + |
| 109 | + const assetUrl = release.assets.filter((asset: any) => |
| 110 | + asset.name.indexOf(msiMatcher) >= 0)[0].browser_download_url; |
| 111 | + |
| 112 | + // Grab MSI and run it. |
| 113 | + // tslint:disable-next-line: max-line-length |
| 114 | + script = ` |
| 115 | +$randomFileName = [System.IO.Path]::GetRandomFileName() |
| 116 | +$tmpMsiPath = Microsoft.PowerShell.Management\\Join-Path ([System.IO.Path]::GetTempPath()) "$randomFileName.msi" |
| 117 | +Microsoft.PowerShell.Utility\\Invoke-RestMethod -Uri ${assetUrl} -OutFile $tmpMsiPath |
| 118 | +try |
| 119 | +{ |
| 120 | + Microsoft.PowerShell.Management\\Start-Process -Wait -Path $tmpMsiPath |
| 121 | +} |
| 122 | +finally |
| 123 | +{ |
| 124 | + Microsoft.PowerShell.Management\\Remove-Item $tmpMsiPath |
| 125 | +}`; |
| 126 | + |
| 127 | + } else if (isMacOS) { |
| 128 | + script = "brew cask upgrade powershell"; |
| 129 | + if (release.isPreview) { |
| 130 | + script = "brew cask upgrade powershell-preview"; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + await languageServerClient.sendRequest(EvaluateRequestType, { |
| 135 | + expression: script, |
| 136 | + }); |
| 137 | + break; |
| 138 | + |
| 139 | + // Never choice. |
| 140 | + case 2: |
| 141 | + await Settings.change("promptToUpdatePowerShell", false, true); |
| 142 | + break; |
| 143 | + default: |
| 144 | + break; |
| 145 | + } |
| 146 | +} |
0 commit comments