3
3
*--------------------------------------------------------*/
4
4
5
5
import fs = require( "fs" ) ;
6
- import os = require( "os" ) ;
7
6
import path = require( "path" ) ;
8
7
import process = require( "process" ) ;
9
- import vscode = require( "vscode" ) ;
10
8
import Settings = require( "./settings" ) ;
11
9
10
+ const linuxExePath = "/usr/bin/pwsh" ;
11
+ const linuxPreviewExePath = "/usr/bin/pwsh-preview" ;
12
+
13
+ const snapExePath = "/snap/bin/pwsh" ;
14
+ const snapPreviewExePath = "/snap/bin/pwsh-preview" ;
15
+
16
+ const macOSExePath = "/usr/local/bin/pwsh" ;
17
+ const macOSPreviewExePath = "/usr/local/bin/pwsh-preview" ;
18
+
12
19
export enum OperatingSystem {
13
20
Unknown ,
14
21
Windows ,
@@ -47,6 +54,13 @@ export function getPlatformDetails(): IPlatformDetails {
47
54
} ;
48
55
}
49
56
57
+ /**
58
+ * Gets the default instance of PowerShell for the specified platform.
59
+ * On Windows, the default version of PowerShell is "Windows PowerShell".
60
+ * @param platformDetails Specifies information about the platform - primarily the operating system.
61
+ * @param use32Bit On Windows, this boolean determines whether the 32-bit version of Windows PowerShell is returned.
62
+ * @returns A string containing the path of the default version of PowerShell.
63
+ */
50
64
export function getDefaultPowerShellPath (
51
65
platformDetails : IPlatformDetails ,
52
66
use32Bit : boolean = false ) : string | null {
@@ -68,14 +82,21 @@ export function getDefaultPowerShellPath(
68
82
: SysnativePowerShellPath ;
69
83
}
70
84
} else if ( platformDetails . operatingSystem === OperatingSystem . MacOS ) {
71
- powerShellExePath = "/usr/local/bin/powershell" ;
72
- if ( fs . existsSync ( "/usr/local/bin/pwsh" ) ) {
73
- powerShellExePath = "/usr/local/bin/pwsh" ;
85
+ // Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
86
+ powerShellExePath = macOSExePath ;
87
+ if ( ! fs . existsSync ( macOSExePath ) && fs . existsSync ( macOSPreviewExePath ) ) {
88
+ powerShellExePath = macOSPreviewExePath ;
74
89
}
75
90
} else if ( platformDetails . operatingSystem === OperatingSystem . Linux ) {
76
- powerShellExePath = "/usr/bin/powershell" ;
77
- if ( fs . existsSync ( "/usr/bin/pwsh" ) ) {
78
- powerShellExePath = "/usr/bin/pwsh" ;
91
+ // Always default to the stable version of PowerShell (if installed) but handle case of only Preview installed
92
+ // as well as the Snaps case - https://snapcraft.io/
93
+ powerShellExePath = linuxExePath ;
94
+ if ( ! fs . existsSync ( linuxExePath ) && fs . existsSync ( linuxPreviewExePath ) ) {
95
+ powerShellExePath = linuxPreviewExePath ;
96
+ } else if ( fs . existsSync ( snapExePath ) ) {
97
+ powerShellExePath = snapExePath ;
98
+ } else if ( fs . existsSync ( snapPreviewExePath ) ) {
99
+ powerShellExePath = snapPreviewExePath ;
79
100
}
80
101
}
81
102
@@ -108,6 +129,12 @@ export function fixWindowsPowerShellPath(powerShellExePath: string, platformDeta
108
129
return powerShellExePath ;
109
130
}
110
131
132
+ /**
133
+ * Gets a list of all available PowerShell instance on the specified platform.
134
+ * @param platformDetails Specifies information about the platform - primarily the operating system.
135
+ * @param sessionSettings Specifies the user/workspace settings. Additional PowerShell exe paths loaded from settings.
136
+ * @returns An array of IPowerShellExeDetails objects with the PowerShell name & exe path for each instance found.
137
+ */
111
138
export function getAvailablePowerShellExes (
112
139
platformDetails : IPlatformDetails ,
113
140
sessionSettings : Settings . ISettings | undefined ) : IPowerShellExeDetails [ ] {
@@ -162,13 +189,25 @@ export function getAvailablePowerShellExes(
162
189
}
163
190
} else {
164
191
// Handle Linux and macOS case
165
- paths . push ( {
166
- versionName : "PowerShell Core" ,
167
- exePath : this . getDefaultPowerShellPath ( platformDetails ) ,
192
+ let exePaths : string [ ] ;
193
+
194
+ if ( platformDetails . operatingSystem === OperatingSystem . Linux ) {
195
+ exePaths = [ linuxExePath , snapExePath , linuxPreviewExePath , snapPreviewExePath ] ;
196
+ } else {
197
+ exePaths = [ macOSExePath , macOSPreviewExePath ] ;
198
+ }
199
+
200
+ exePaths . forEach ( ( exePath ) => {
201
+ if ( fs . existsSync ( exePath ) ) {
202
+ paths . push ( {
203
+ versionName : "PowerShell Core" + ( / - p r e v i e w / . test ( exePath ) ? " Preview" : "" ) ,
204
+ exePath,
205
+ } ) ;
206
+ }
168
207
} ) ;
169
208
}
170
209
171
- // When unit testing, we don't have session settings to test so skip reading this setting
210
+ // When unit testing, we don't have session settings available to test, so skip reading this setting
172
211
if ( sessionSettings ) {
173
212
// Add additional PowerShell paths as configured in settings
174
213
for ( const additionalPowerShellExePath of sessionSettings . powerShellAdditionalExePaths ) {
0 commit comments