Skip to content

Commit

Permalink
WIP: Add support for side-by-side PS Core preview on Linux/macOS (#1366)
Browse files Browse the repository at this point in the history
* Add support for side-by-side PS Core preview on Linux/macOS

This PR supports listing the symlinks for pwsh & pwsh-preview.

The preferred default version is pwsh if both are present however, the
default will be pwsh-preview if it's installed and pwsh isn't.

So this only support side-by-side on Linux/macOS up to two versions:
stable and preview.

* Adjust name of default PS Core on Linux/macOS

* Make a bit DRY-er. Thx @tylerl0706

* Remove support for pre-ship PS Core Linux/macOS binary name - powershell

* Add support for PowerShell Core installed in a Snap

* Simplify impl of getting available PS on Linux/macOS
  • Loading branch information
rkeithhill authored and Robert Holt committed Jul 11, 2018
1 parent e414739 commit ef65989
Showing 1 changed file with 51 additions and 12 deletions.
63 changes: 51 additions & 12 deletions src/platform.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
*--------------------------------------------------------*/

import fs = require("fs");
import os = require("os");
import path = require("path");
import process = require("process");
import vscode = require("vscode");
import Settings = require("./settings");

const linuxExePath = "/usr/bin/pwsh";
const linuxPreviewExePath = "/usr/bin/pwsh-preview";

const snapExePath = "/snap/bin/pwsh";
const snapPreviewExePath = "/snap/bin/pwsh-preview";

const macOSExePath = "/usr/local/bin/pwsh";
const macOSPreviewExePath = "/usr/local/bin/pwsh-preview";

export enum OperatingSystem {
Unknown,
Windows,
Expand Down Expand Up @@ -47,6 +54,13 @@ export function getPlatformDetails(): IPlatformDetails {
};
}

/**
* Gets the default instance of PowerShell for the specified platform.
* On Windows, the default version of PowerShell is "Windows PowerShell".
* @param platformDetails Specifies information about the platform - primarily the operating system.
* @param use32Bit On Windows, this boolean determines whether the 32-bit version of Windows PowerShell is returned.
* @returns A string containing the path of the default version of PowerShell.
*/
export function getDefaultPowerShellPath(
platformDetails: IPlatformDetails,
use32Bit: boolean = false): string | null {
Expand All @@ -68,14 +82,21 @@ export function getDefaultPowerShellPath(
: SysnativePowerShellPath;
}
} else if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
powerShellExePath = "/usr/local/bin/powershell";
if (fs.existsSync("/usr/local/bin/pwsh")) {
powerShellExePath = "/usr/local/bin/pwsh";
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
powerShellExePath = macOSExePath;
if (!fs.existsSync(macOSExePath) && fs.existsSync(macOSPreviewExePath)) {
powerShellExePath = macOSPreviewExePath;
}
} else if (platformDetails.operatingSystem === OperatingSystem.Linux) {
powerShellExePath = "/usr/bin/powershell";
if (fs.existsSync("/usr/bin/pwsh")) {
powerShellExePath = "/usr/bin/pwsh";
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
// as well as the Snaps case - https://snapcraft.io/
powerShellExePath = linuxExePath;
if (!fs.existsSync(linuxExePath) && fs.existsSync(linuxPreviewExePath)) {
powerShellExePath = linuxPreviewExePath;
} else if (fs.existsSync(snapExePath)) {
powerShellExePath = snapExePath;
} else if (fs.existsSync(snapPreviewExePath)) {
powerShellExePath = snapPreviewExePath;
}
}

Expand Down Expand Up @@ -108,6 +129,12 @@ export function fixWindowsPowerShellPath(powerShellExePath: string, platformDeta
return powerShellExePath;
}

/**
* Gets a list of all available PowerShell instance on the specified platform.
* @param platformDetails Specifies information about the platform - primarily the operating system.
* @param sessionSettings Specifies the user/workspace settings. Additional PowerShell exe paths loaded from settings.
* @returns An array of IPowerShellExeDetails objects with the PowerShell name & exe path for each instance found.
*/
export function getAvailablePowerShellExes(
platformDetails: IPlatformDetails,
sessionSettings: Settings.ISettings | undefined): IPowerShellExeDetails[] {
Expand Down Expand Up @@ -162,13 +189,25 @@ export function getAvailablePowerShellExes(
}
} else {
// Handle Linux and macOS case
paths.push({
versionName: "PowerShell Core",
exePath: this.getDefaultPowerShellPath(platformDetails),
let exePaths: string[];

if (platformDetails.operatingSystem === OperatingSystem.Linux) {
exePaths = [ linuxExePath, snapExePath, linuxPreviewExePath, snapPreviewExePath ];
} else {
exePaths = [ macOSExePath, macOSPreviewExePath ];
}

exePaths.forEach((exePath) => {
if (fs.existsSync(exePath)) {
paths.push({
versionName: "PowerShell Core" + (/-preview/.test(exePath) ? " Preview" : ""),
exePath,
});
}
});
}

// When unit testing, we don't have session settings to test so skip reading this setting
// When unit testing, we don't have session settings available to test, so skip reading this setting
if (sessionSettings) {
// Add additional PowerShell paths as configured in settings
for (const additionalPowerShellExePath of sessionSettings.powerShellAdditionalExePaths) {
Expand Down

0 comments on commit ef65989

Please sign in to comment.