Skip to content

Fix update powershell feature on windows #2325

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
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
77 changes: 49 additions & 28 deletions src/features/UpdatePowerShell.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/

import { spawn } from "child_process";
import * as fs from "fs";
import fetch, { RequestInit } from "node-fetch";
import * as os from "os";
import * as path from "path";
import * as semver from "semver";
import { MessageItem, window } from "vscode";
import * as stream from "stream";
import * as util from "util";
import { MessageItem, ProgressLocation, window } from "vscode";
import { LanguageClient } from "vscode-languageclient";
import { SessionManager } from "../session";
import * as Settings from "../settings";
import { isMacOS, isWindows } from "../utils";
import { EvaluateRequestType } from "./Console";

const streamPipeline = util.promisify(stream.pipeline);

const PowerShellGitHubReleasesUrl =
"https://api.github.com/repos/PowerShell/PowerShell/releases/latest";
const PowerShellGitHubPrereleasesUrl =
Expand Down Expand Up @@ -67,6 +77,7 @@ interface IUpdateMessageItem extends MessageItem {
}

export async function InvokePowerShellUpdateCheck(
sessionManager: SessionManager,
languageServerClient: LanguageClient,
localVersion: semver.SemVer,
arch: string,
Expand Down Expand Up @@ -103,7 +114,6 @@ export async function InvokePowerShellUpdateCheck(
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)" : ""
Expand All @@ -116,39 +126,50 @@ export async function InvokePowerShellUpdateCheck(
switch (result.id) {
// Yes choice.
case 0:
let script: string;
if (process.platform === "win32") {
if (isWindows) {
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
}`;
const asset = release.assets.filter((a: any) => a.name.indexOf(msiMatcher) >= 0)[0];
const msiDownloadPath = path.join(os.tmpdir(), asset.name);

} else if (isMacOS) {
script = "brew cask upgrade powershell";
if (release.isPreview) {
script = "brew cask upgrade powershell-preview";
const res = await fetch(asset.browser_download_url);
if (!res.ok) {
throw new Error("unable to fetch MSI");
}

await window.withProgress({
title: "Downloading PowerShell Installer...",
location: ProgressLocation.Notification,
cancellable: false,
},
async () => {
// Streams the body of the request to a file.
await streamPipeline(res.body, fs.createWriteStream(msiDownloadPath));
});

// Stop the Integrated Console session because Windows likes to hold on to files.
sessionManager.stop();

// Invoke the MSI via cmd.
const msi = spawn("msiexec", ["/i", msiDownloadPath]);

msi.on("close", (code) => {
// Now that the MSI is finished, start the Integrated Console session.
sessionManager.start();
fs.unlinkSync(msiDownloadPath);
});

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

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

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

// Never choice.
Expand Down
4 changes: 2 additions & 2 deletions src/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ export class PowerShellProcess {
];

// Only add ExecutionPolicy param on Windows
if (utils.isWindowsOS()) {
if (utils.isWindows) {
powerShellArgs.push("-ExecutionPolicy", "Bypass");
}

const startEditorServices = "& '" +
PowerShellProcess.escapeSingleQuotes(startScriptPath) +
"' " + this.startArgs;

if (utils.isWindowsOS()) {
if (utils.isWindows) {
powerShellArgs.push(
"-Command",
startEditorServices);
Expand Down
1 change: 1 addition & 0 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ export class SessionManager implements Middleware {
await GitHubReleaseInformation.FetchLatestRelease(isPreRelease);

await InvokePowerShellUpdateCheck(
this,
this.languageServerClient,
localVersion,
this.versionDetails.architecture,
Expand Down
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import fs = require("fs");
import os = require("os");
import path = require("path");

export let PowerShellLanguageId = "powershell";
export const PowerShellLanguageId = "powershell";

export function ensurePathExists(targetPath: string) {
// Ensure that the path exists
Expand Down Expand Up @@ -116,6 +116,6 @@ export function getTimestampString() {
return `[${time.getHours()}:${time.getMinutes()}:${time.getSeconds()}]`;
}

export function isWindowsOS(): boolean {
return os.platform() === "win32";
}
export const isMacOS: boolean = process.platform === "darwin";
export const isWindows: boolean = process.platform === "win32";
export const isLinux: boolean = !isMacOS && !isWindows;