-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathplatform.ts
222 lines (189 loc) · 8.58 KB
/
platform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import fs = require("fs");
import path = require("path");
import process = require("process");
import Settings = require("./settings");
const linuxExePath = "/usr/bin/pwsh";
const linuxPreviewExePath = "/usr/bin/pwsh-preview";
const macOSExePath = "/usr/local/bin/pwsh";
const macOSPreviewExePath = "/usr/local/bin/pwsh-preview";
export enum OperatingSystem {
Unknown,
Windows,
MacOS,
Linux,
}
export interface IPlatformDetails {
operatingSystem: OperatingSystem;
isOS64Bit: boolean;
isProcess64Bit: boolean;
}
export interface IPowerShellExeDetails {
versionName: string;
exePath: string;
}
export function getPlatformDetails(): IPlatformDetails {
let 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;
}
const isProcess64Bit = process.arch === "x64";
return {
operatingSystem,
isOS64Bit: isProcess64Bit || process.env.hasOwnProperty("PROCESSOR_ARCHITEW6432"),
isProcess64Bit,
};
}
/**
* 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 will find the 32-bit version of Windows PowerShell.
* @returns A string containing the path of the default version of PowerShell.
*/
export function getDefaultPowerShellPath(
platformDetails: IPlatformDetails,
use32Bit: boolean = false): string | null {
let powerShellExePath;
// 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) {
if (use32Bit) {
powerShellExePath =
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
? SysWow64PowerShellPath
: System32PowerShellPath;
} else {
powerShellExePath =
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
? System32PowerShellPath
: SysnativePowerShellPath;
}
} else if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
// 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) {
// Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
powerShellExePath = linuxExePath;
if (!fs.existsSync(linuxExePath) && fs.existsSync(linuxPreviewExePath)) {
powerShellExePath = linuxPreviewExePath;
}
}
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");
export const WindowsPowerShell64BitLabel = "Windows PowerShell (x64)";
export const WindowsPowerShell32BitLabel = "Windows PowerShell (x86)";
const powerShell64BitPathOn32Bit = SysnativePowerShellPath.toLocaleLowerCase();
const powerShell32BitPathOn64Bit = SysWow64PowerShellPath.toLocaleLowerCase();
export function fixWindowsPowerShellPath(powerShellExePath: string, platformDetails: IPlatformDetails): string {
const 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;
}
/**
* 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[] {
let paths: IPowerShellExeDetails[] = [];
if (platformDetails.operatingSystem === OperatingSystem.Windows) {
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,
});
}
const psCoreInstallPath =
(!platformDetails.isProcess64Bit ? process.env.ProgramW6432 : process.env.ProgramFiles) + "\\PowerShell";
if (fs.existsSync(psCoreInstallPath)) {
const arch = platformDetails.isProcess64Bit ? "(x64)" : "(x86)";
const psCorePaths =
fs.readdirSync(psCoreInstallPath)
.map((item) => path.join(psCoreInstallPath, item))
.filter((item) => {
const exePath = path.join(item, "pwsh.exe");
return fs.lstatSync(item).isDirectory() && fs.existsSync(exePath);
})
.map((item) => ({
versionName: `PowerShell Core ${path.parse(item).base} ${arch}`,
exePath: path.join(item, "pwsh.exe"),
}));
if (psCorePaths) {
paths = paths.concat(psCorePaths);
}
}
} else {
// Handle Linux and macOS case
const defaultExePath = this.getDefaultPowerShellPath(platformDetails);
paths.push({
versionName: "PowerShell Core" + (/-preview/.test(defaultExePath) ? " Preview" : ""),
exePath: defaultExePath,
});
// If defaultExePath is pwsh, check to see if pwsh-preview is also installed and if so, make it available.
// If the defaultExePath is already pwsh-preview, then pwsh is not installed - nothing to do.
let osExePath;
let osPreviewExePath;
if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
osExePath = macOSExePath;
osPreviewExePath = macOSPreviewExePath;
} else if (platformDetails.operatingSystem === OperatingSystem.Linux) {
osExePath = linuxExePath;
osPreviewExePath = linuxPreviewExePath;
}
if ((osExePath === defaultExePath) && fs.existsSync(osPreviewExePath)) {
paths.push({
versionName: "PowerShell Core Preview",
exePath: osPreviewExePath,
});
}
}
// When unit testing, we don't have session settings to test so skip reading this setting
if (sessionSettings) {
// Add additional PowerShell paths as configured in settings
for (const additionalPowerShellExePath of sessionSettings.powerShellAdditionalExePaths) {
paths.push({
versionName: additionalPowerShellExePath.versionName,
exePath: additionalPowerShellExePath.exePath,
});
}
}
return paths;
}