-
Notifications
You must be signed in to change notification settings - Fork 498
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix issues with resolving correct PowerShell paths in 64-bit VS Code
This change introduces a new module that contains all the logic for determining the current OS platform, process architecture and OS architecture for use in determining available Windows PowerShell EXE paths. This module helps resolve problems that were introduced by the new 64-bit distribution of Visual Studio Code. Fixes #1008 Fixes #1007 Fixes #1006 Fixes #993
- Loading branch information
Showing
2 changed files
with
217 additions
and
81 deletions.
There are no files selected for viewing
This file contains 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,163 @@ | ||
/*--------------------------------------------------------- | ||
* Copyright (C) Microsoft Corporation. All rights reserved. | ||
*--------------------------------------------------------*/ | ||
|
||
import fs = require('fs'); | ||
import os = require('os'); | ||
import path = require('path'); | ||
import vscode = require('vscode'); | ||
import process = require('process'); | ||
import Settings = require('./settings'); | ||
|
||
export enum OperatingSystem { | ||
Unknown, | ||
Windows, | ||
MacOS, | ||
Linux | ||
} | ||
|
||
export interface PlatformDetails { | ||
operatingSystem: OperatingSystem | ||
isOS64Bit: boolean | ||
isProcess64Bit: boolean | ||
} | ||
|
||
interface PowerShellExeDetails { | ||
versionName: string; | ||
exePath: string; | ||
} | ||
|
||
export function getPlatformDetails(): PlatformDetails { | ||
var operatingSystem = OperatingSystem.Unknown; | ||
|
||
if (process.platform === "win32") { | ||
operatingSystem = OperatingSystem.Windows; | ||
} | ||
else if (process.platform === "darwin") { | ||
operatingSystem = OperatingSystem.MacOS; | ||
} | ||
else if (process.platform === "linux") { | ||
operatingSystem = OperatingSystem.Linux; | ||
} | ||
|
||
let isProcess64Bit = process.arch === "x64"; | ||
|
||
return { | ||
operatingSystem: operatingSystem, | ||
isOS64Bit: isProcess64Bit || process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432'), | ||
isProcess64Bit: isProcess64Bit | ||
} | ||
} | ||
|
||
export function getDefaultPowerShellPath( | ||
platformDetails: PlatformDetails, | ||
use32Bit: boolean = false): string | null { | ||
|
||
var powerShellExePath = undefined; | ||
|
||
// Find the path to powershell.exe based on the current platform | ||
// and the user's desire to run the x86 version of PowerShell | ||
if (platformDetails.operatingSystem == OperatingSystem.Windows) { | ||
powerShellExePath = | ||
use32Bit || !process.env.hasOwnProperty('PROCESSOR_ARCHITEW6432') | ||
? System32PowerShellPath | ||
: SysnativePowerShellPath | ||
} | ||
else if (platformDetails.operatingSystem == OperatingSystem.MacOS) { | ||
powerShellExePath = "/usr/local/bin/powershell"; | ||
} | ||
else if (platformDetails.operatingSystem == OperatingSystem.Linux) { | ||
powerShellExePath = "/usr/bin/powershell"; | ||
} | ||
|
||
return powerShellExePath; | ||
} | ||
|
||
export function getWindowsSystemPowerShellPath(systemFolderName: string) { | ||
return `${process.env.windir}\\${systemFolderName}\\WindowsPowerShell\\v1.0\\powershell.exe` | ||
} | ||
|
||
export const System32PowerShellPath = getWindowsSystemPowerShellPath('System32'); | ||
export const SysnativePowerShellPath = getWindowsSystemPowerShellPath('Sysnative'); | ||
export const SysWow64PowerShellPath = getWindowsSystemPowerShellPath('SysWow64'); | ||
|
||
const powerShell64BitPathOn32Bit = SysnativePowerShellPath.toLocaleLowerCase(); | ||
const powerShell32BitPathOn64Bit = SysWow64PowerShellPath.toLocaleLowerCase(); | ||
|
||
export function fixWindowsPowerShellPath(powerShellExePath: string, platformDetails: PlatformDetails): string { | ||
let lowerCasedPath = powerShellExePath.toLocaleLowerCase(); | ||
|
||
if ((platformDetails.isProcess64Bit && (lowerCasedPath === powerShell64BitPathOn32Bit)) || | ||
(!platformDetails.isProcess64Bit && (lowerCasedPath === powerShell32BitPathOn64Bit))) { | ||
return System32PowerShellPath; | ||
} | ||
|
||
// If the path doesn't need to be fixed, return the original | ||
return powerShellExePath; | ||
} | ||
|
||
export function getPowerShellExeItems(platformDetails: PlatformDetails): PowerShellExeDetails[] { | ||
|
||
var paths: PowerShellExeDetails[] = []; | ||
|
||
const windowsPowerShell64BitLabel = "Windows PowerShell (x64)"; | ||
const windowsPowerShell32BitLabel = "Windows PowerShell (x86)"; | ||
|
||
if (platformDetails.operatingSystem === OperatingSystem.Windows) { | ||
const psCoreInstallPath = | ||
(!platformDetails.isProcess64Bit ? process.env.ProgramW6432 : process.env.ProgramFiles) + '\\PowerShell'; | ||
|
||
if (platformDetails.isProcess64Bit) { | ||
paths.push({ | ||
versionName: windowsPowerShell64BitLabel, | ||
exePath: System32PowerShellPath | ||
}) | ||
|
||
paths.push({ | ||
versionName: windowsPowerShell32BitLabel, | ||
exePath: SysWow64PowerShellPath | ||
}) | ||
} | ||
else { | ||
if (platformDetails.isOS64Bit) { | ||
paths.push({ | ||
versionName: windowsPowerShell64BitLabel, | ||
exePath: SysnativePowerShellPath | ||
}) | ||
} | ||
|
||
paths.push({ | ||
versionName: windowsPowerShell32BitLabel, | ||
exePath: System32PowerShellPath | ||
}) | ||
} | ||
|
||
if (fs.existsSync(psCoreInstallPath)) { | ||
var psCorePaths = | ||
fs.readdirSync(psCoreInstallPath) | ||
.map(item => path.join(psCoreInstallPath, item)) | ||
.filter(item => fs.lstatSync(item).isDirectory()) | ||
.map(item => { | ||
return { | ||
versionName: `PowerShell Core ${path.parse(item).base}`, | ||
exePath: path.join(item, "powershell.exe") | ||
}; | ||
}); | ||
|
||
if (psCorePaths) { | ||
paths = paths.concat(psCorePaths); | ||
} | ||
} | ||
} | ||
else { | ||
paths.push({ | ||
versionName: "PowerShell Core", | ||
exePath: | ||
os.platform() === "darwin" | ||
? "/usr/local/bin/powershell" | ||
: "/usr/bin/powershell" | ||
}); | ||
} | ||
|
||
return paths; | ||
} |
This file contains 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