11// Copyright (c) Microsoft Corporation.
22// Licensed under the MIT License.
33
4- import { spawn } from "child_process" ;
5- import * as fs from "fs" ; // TODO: Remove, but it's for a stream.
64import fetch from "node-fetch" ;
7- import * as os from "os" ;
8- import * as path from "path" ;
95import { SemVer } from "semver" ;
10- import * as stream from "stream" ;
11- import * as util from "util" ;
126import vscode = require( "vscode" ) ;
137
148import { ILogger } from "../logging" ;
15- import { IPowerShellVersionDetails , SessionManager } from "../session" ;
9+ import { IPowerShellVersionDetails } from "../session" ;
1610import { changeSetting , Settings } from "../settings" ;
17- import { isWindows } from "../utils" ;
18-
19- const streamPipeline = util . promisify ( stream . pipeline ) ;
2011
2112interface IUpdateMessageItem extends vscode . MessageItem {
2213 id : number ;
@@ -29,7 +20,6 @@ export class UpdatePowerShell {
2920 private static LTSBuildInfoURL = "https://aka.ms/pwsh-buildinfo-lts" ;
3021 private static StableBuildInfoURL = "https://aka.ms/pwsh-buildinfo-stable" ;
3122 private static PreviewBuildInfoURL = "https://aka.ms/pwsh-buildinfo-preview" ;
32- private static GitHubAPIReleaseURL = "https://api.github.com/repos/PowerShell/PowerShell/releases/tags/" ;
3323 private static GitHubWebReleaseURL = "https://github.com/PowerShell/PowerShell/releases/tag/" ;
3424 private static promptOptions : IUpdateMessageItem [ ] = [
3525 {
@@ -46,10 +36,8 @@ export class UpdatePowerShell {
4636 } ,
4737 ] ;
4838 private localVersion : SemVer ;
49- private architecture : string ;
5039
5140 constructor (
52- private sessionManager : SessionManager ,
5341 private sessionSettings : Settings ,
5442 private logger : ILogger ,
5543 versionDetails : IPowerShellVersionDetails ) {
@@ -58,7 +46,6 @@ export class UpdatePowerShell {
5846 // to SemVer. The version handler in PSES handles Windows PowerShell and
5947 // just returns the first three fields like '5.1.22621'.
6048 this . localVersion = new SemVer ( versionDetails . commit ) ;
61- this . architecture = versionDetails . architecture . toLowerCase ( ) ;
6249 }
6350
6451 private shouldCheckForUpdate ( ) : boolean {
@@ -173,74 +160,13 @@ export class UpdatePowerShell {
173160 await vscode . env . openExternal ( url ) ;
174161 }
175162
176- private async updateWindows ( tag : string ) : Promise < void > {
177- let msiMatcher : string ;
178- if ( this . architecture === "x64" ) {
179- msiMatcher = "win-x64.msi" ;
180- } else if ( this . architecture === "x86" ) {
181- msiMatcher = "win-x86.msi" ;
182- } else {
183- // We shouldn't get here, but do something sane anyway.
184- return this . openReleaseInBrowser ( tag ) ;
185- }
186-
187- let response = await fetch ( UpdatePowerShell . GitHubAPIReleaseURL + tag ) ;
188- if ( ! response . ok ) {
189- throw new Error ( "Failed to fetch GitHub release info!" ) ;
190- }
191- const release = await response . json ( ) ;
192-
193- // eslint-disable-next-line @typescript-eslint/no-explicit-any
194- const asset = release . assets . filter ( ( a : any ) => a . name . indexOf ( msiMatcher ) >= 0 ) [ 0 ] ;
195- const msiDownloadPath = path . join ( os . tmpdir ( ) , asset . name ) ;
196-
197- response = await fetch ( asset . browser_download_url ) ;
198- if ( ! response . ok ) {
199- throw new Error ( "Failed to fetch MSI!" ) ;
200- }
201-
202- const progressOptions = {
203- title : "Downloading PowerShell Installer..." ,
204- location : vscode . ProgressLocation . Notification ,
205- cancellable : false ,
206- } ;
207- // Streams the body of the request to a file.
208- await vscode . window . withProgress ( progressOptions ,
209- async ( ) => { await streamPipeline ( response . body , fs . createWriteStream ( msiDownloadPath ) ) ; } ) ;
210-
211- // Stop the session because Windows likes to hold on to files.
212- this . logger . writeDiagnostic ( "MSI downloaded, stopping session and closing terminals!" ) ;
213- await this . sessionManager . stop ( ) ;
214-
215- // Close all terminals with the name "pwsh" in the current VS Code session.
216- // This will encourage folks to not close the instance of VS Code that spawned
217- // the MSI process.
218- for ( const terminal of vscode . window . terminals ) {
219- if ( terminal . name === "pwsh" ) {
220- terminal . dispose ( ) ;
221- }
222- }
223-
224- // Invoke the MSI via cmd.
225- this . logger . writeDiagnostic ( `Running '${ msiDownloadPath } ' to update PowerShell...` ) ;
226- const msi = spawn ( "msiexec" , [ "/i" , msiDownloadPath ] ) ;
227-
228- msi . on ( "close" , ( ) => {
229- // Now that the MSI is finished, restart the session.
230- this . logger . writeDiagnostic ( "MSI installation finished, restarting session." ) ;
231- void this . sessionManager . start ( ) ;
232- fs . unlinkSync ( msiDownloadPath ) ;
233- } ) ;
234- }
235-
236163 private async installUpdate ( tag : string ) : Promise < void > {
237164 const releaseVersion = new SemVer ( tag ) ;
238165 const result = await vscode . window . showInformationMessage (
239- `You have an old version of PowerShell (${ this . localVersion . version } ). The current latest release is ${ releaseVersion . version } .
240- Would you like to update the version? ${ isWindows
241- ? "This will close ALL pwsh terminals running in this VS Code session!"
242- : "We can't update you automatically, but we can open the latest release in your browser!"
243- } `, ...UpdatePowerShell . promptOptions ) ;
166+ `You have an old version of PowerShell (${ this . localVersion . version } ).
167+ The current latest release is ${ releaseVersion . version } .
168+ Would you like to open the GitHub release in your browser?` ,
169+ ...UpdatePowerShell . promptOptions ) ;
244170
245171 // If the user cancels the notification.
246172 if ( ! result ) {
@@ -253,11 +179,7 @@ export class UpdatePowerShell {
253179 switch ( result . id ) {
254180 // Yes
255181 case 0 :
256- if ( isWindows && ( this . architecture === "x64" || this . architecture === "x86" ) ) {
257- await this . updateWindows ( tag ) ;
258- } else {
259- await this . openReleaseInBrowser ( tag ) ;
260- }
182+ await this . openReleaseInBrowser ( tag ) ;
261183 break ;
262184 // Not Now
263185 case 1 :
0 commit comments