forked from PowerShell/vscode-powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplatform.ts
180 lines (150 loc) · 5.89 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
/*---------------------------------------------------------
* 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
}
export 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) {
if (use32Bit) {
powerShellExePath =
platformDetails.isOS64Bit && platformDetails.isProcess64Bit
? SysWow64PowerShellPath
: System32PowerShellPath
}
else {
powerShellExePath =
!platformDetails.isOS64Bit || platformDetails.isProcess64Bit
? System32PowerShellPath
: SysnativePowerShellPath
}
}
else if (platformDetails.operatingSystem == OperatingSystem.MacOS) {
powerShellExePath = "/usr/local/bin/powershell";
if (fs.existsSync("/usr/local/bin/pwsh")) {
powerShellExePath = "/usr/local/bin/pwsh";
}
}
else if (platformDetails.operatingSystem == OperatingSystem.Linux) {
powerShellExePath = "/usr/bin/powershell";
if (fs.existsSync("/usr/bin/pwsh")) {
powerShellExePath = "/usr/bin/pwsh";
}
}
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: 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 getAvailablePowerShellExes(platformDetails: PlatformDetails): PowerShellExeDetails[] {
var paths: PowerShellExeDetails[] = [];
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 => {
let exePath = path.join(item, "pwsh.exe");
if (!fs.existsSync(exePath)) {
exePath = path.join(item, "powershell.exe");
}
return {
versionName: `PowerShell Core ${path.parse(item).base}`,
exePath: exePath
};
});
if (psCorePaths) {
paths = paths.concat(psCorePaths);
}
}
}
else {
paths.push({
versionName: "PowerShell Core",
exePath: this.getDefaultPowerShellPath(platformDetails)
});
}
return paths;
}