-
Notifications
You must be signed in to change notification settings - Fork 510
/
Copy pathplatform.ts
212 lines (180 loc) · 7.81 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
/*---------------------------------------------------------
* Copyright (C) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------*/
import fs = require("fs");
import path = require("path");
import process = require("process");
import Settings = require("./settings");
const linuxLegacyExePath = "/usr/bin/powershell";
const linuxExePath = "/usr/bin/pwsh";
const linuxPreviewExePath = "/usr/bin/pwsh-preview";
const macOSLegacyExePath = "/usr/local/bin/powershell";
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,
};
}
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) {
powerShellExePath = macOSLegacyExePath;
if (fs.existsSync(macOSExePath)) {
powerShellExePath = macOSExePath;
} else if (fs.existsSync(macOSPreviewExePath)) {
powerShellExePath = macOSPreviewExePath;
}
} else if (platformDetails.operatingSystem === OperatingSystem.Linux) {
powerShellExePath = linuxLegacyExePath;
if (fs.existsSync(linuxExePath)) {
powerShellExePath = linuxExePath;
} else if (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;
}
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 installed and if so, make it available.
// If the defaultExePath is already pwsh-preview, then pwsh is not installed - nothing to do.
if (platformDetails.operatingSystem === OperatingSystem.MacOS) {
if ((defaultExePath === macOSExePath) && fs.existsSync(macOSPreviewExePath)) {
paths.push({
versionName: "PowerShell Core Preview",
exePath: macOSPreviewExePath,
});
}
} else if (platformDetails.operatingSystem === OperatingSystem.Linux) {
if ((defaultExePath === linuxExePath) && fs.existsSync(linuxPreviewExePath)) {
paths.push({
versionName: "PowerShell Core Preview",
exePath: linuxPreviewExePath,
});
}
}
}
// 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;
}