1
- # PowerShell Editor Services Bootstrapper Script
2
- # ----------------------------------------------
3
- # This script contains startup logic for the PowerShell Editor Services
4
- # module when launched by an editor. It handles the following tasks:
5
- #
6
- # - Verifying the existence of dependencies like PowerShellGet
7
- # - Verifying that the expected version of the PowerShellEditorServices module is installed
8
- # - Installing the PowerShellEditorServices module if confirmed by the user
9
- # - Creating named pipes for the language and debug services to use (if using named pipes)
10
- # - Starting the language and debug services from the PowerShellEditorServices module
11
- #
12
- # NOTE: If editor integration authors make modifications to this
13
- # script, please consider contributing changes back to the
14
- # canonical version of this script at the PowerShell Editor
15
- # Services GitHub repository:
16
- #
17
- # https://github.com/PowerShell/PowerShellEditorServices/blob/master/module/PowerShellEditorServices/Start-EditorServices.ps1
1
+ <#
2
+ . SYNOPSIS
3
+ Starts the language and debug services from the PowerShellEditorServices module.
4
+ . DESCRIPTION
5
+ PowerShell Editor Services Bootstrapper Script
6
+ ----------------------------------------------
7
+ This script contains startup logic for the PowerShell Editor Services
8
+ module when launched by an editor. It handles the following tasks:
9
+
10
+ - Verifying the existence of dependencies like PowerShellGet
11
+ - Verifying that the expected version of the PowerShellEditorServices module is installed
12
+ - Installing the PowerShellEditorServices module if confirmed by the user
13
+ - Creating named pipes for the language and debug services to use (if using named pipes)
14
+ - Starting the language and debug services from the PowerShellEditorServices module
15
+ . INPUTS
16
+ None
17
+ . OUTPUTS
18
+ None
19
+ . NOTES
20
+ If editor integration authors make modifications to this script, please
21
+ consider contributing changes back to the canonical version of this script
22
+ at the PowerShell Editor Services GitHub repository:
23
+ https://github.com/PowerShell/PowerShellEditorServices/blob/master/module/PowerShellEditorServices/Start-EditorServices.ps1'
24
+ #>
18
25
[CmdletBinding (DefaultParameterSetName = " NamedPipe" )]
19
26
param (
20
27
[Parameter (Mandatory = $true )]
65
72
[switch ]
66
73
$ConfirmInstall ,
67
74
68
- [Parameter (ParameterSetName = " Stdio" , Mandatory = $true )]
75
+ [Parameter (ParameterSetName = " Stdio" , Mandatory = $true )]
69
76
[switch ]
70
77
$Stdio ,
71
78
@@ -154,9 +161,6 @@ if ($host.Runspace.LanguageMode -eq 'ConstrainedLanguage') {
154
161
ExitWithError " PowerShell is configured with an unsupported LanguageMode (ConstrainedLanguage), language features are disabled."
155
162
}
156
163
157
- # Are we running in PowerShell 5 or later?
158
- $isPS5orLater = $PSVersionTable.PSVersion.Major -ge 5
159
-
160
164
# If PSReadline is present in the session, remove it so that runspace
161
165
# management is easier
162
166
if ((Microsoft.PowerShell.Core\Get-Module PSReadline).Count -gt 0 ) {
@@ -173,8 +177,8 @@ function Test-ModuleAvailable($ModuleName, $ModuleVersion) {
173
177
Log " Testing module availability $ModuleName $ModuleVersion "
174
178
175
179
$modules = Microsoft.PowerShell.Core\Get-Module - ListAvailable $moduleName
176
- if ($modules -ne $null ) {
177
- if ($ModuleVersion -ne $null ) {
180
+ if ($null -ne $modules ) {
181
+ if ($null -ne $ModuleVersion ) {
178
182
foreach ($module in $modules ) {
179
183
if ($module.Version.Equals ($moduleVersion )) {
180
184
Log " $ModuleName $ModuleVersion found"
@@ -193,7 +197,6 @@ function Test-ModuleAvailable($ModuleName, $ModuleVersion) {
193
197
}
194
198
195
199
function New-NamedPipeName {
196
-
197
200
# We try 10 times to find a valid pipe name
198
201
for ($i = 0 ; $i -lt 10 ; $i ++ ) {
199
202
$PipeName = " PSES_$ ( [System.IO.Path ]::GetRandomFileName()) "
@@ -202,6 +205,7 @@ function New-NamedPipeName {
202
205
return $PipeName
203
206
}
204
207
}
208
+
205
209
ExitWithError " Could not find valid a pipe name."
206
210
}
207
211
@@ -213,15 +217,14 @@ function Get-NamedPipePath {
213
217
$PipeName
214
218
)
215
219
216
- if (-not $IsLinux -and -not $IsMacOS ) {
220
+ if (( $PSVersionTable .PSVersion.Major -le 5 ) -or $IsWindows ) {
217
221
return " \\.\pipe\$PipeName " ;
218
222
}
219
223
else {
220
224
# Windows uses NamedPipes where non-Windows platforms use Unix Domain Sockets.
221
225
# the Unix Domain Sockets live in the tmp directory and are prefixed with "CoreFxPipe_"
222
226
return (Join-Path - Path ([System.IO.Path ]::GetTempPath()) - ChildPath " CoreFxPipe_$PipeName " )
223
227
}
224
-
225
228
}
226
229
227
230
# Returns True if it's a valid pipe name
@@ -236,7 +239,7 @@ function Test-NamedPipeName {
236
239
)
237
240
238
241
$path = Get-NamedPipePath - PipeName $PipeName
239
- return -not (Test-Path $path )
242
+ return ! (Test-Path $path )
240
243
}
241
244
242
245
function Set-NamedPipeMode {
@@ -247,7 +250,7 @@ function Set-NamedPipeMode {
247
250
$PipeFile
248
251
)
249
252
250
- if ($IsWindows ) {
253
+ if (( $PSVersionTable .PSVersion.Major -le 5 ) -or $IsWindows ) {
251
254
return
252
255
}
253
256
@@ -268,33 +271,37 @@ function Set-NamedPipeMode {
268
271
LogSection " Console Encoding"
269
272
Log $OutputEncoding
270
273
271
- function Test-NamedPipeName-OrCreate-IfNull {
274
+ function Get-ValidatedNamedPipeName {
272
275
param (
273
276
[string ]
274
277
$PipeName
275
278
)
276
- if (-not $PipeName ) {
279
+
280
+ # If no PipeName is passed in, then we create one that's guaranteed to be valid
281
+ if (! $PipeName ) {
277
282
$PipeName = New-NamedPipeName
278
283
}
279
- else {
280
- if (-not (Test-NamedPipeName - PipeName $PipeName )) {
281
- ExitWithError " Pipe name supplied is already taken: $PipeName "
282
- }
284
+ elseif (! (Test-NamedPipeName - PipeName $PipeName )) {
285
+ ExitWithError " Pipe name supplied is already in use: $PipeName "
283
286
}
287
+
284
288
return $PipeName
285
289
}
286
290
287
291
function Set-PipeFileResult {
288
292
param (
289
293
[Hashtable ]
290
294
$ResultTable ,
295
+
291
296
[string ]
292
297
$PipeNameKey ,
298
+
293
299
[string ]
294
300
$PipeNameValue
295
301
)
302
+
296
303
$ResultTable [$PipeNameKey ] = Get-NamedPipePath - PipeName $PipeNameValue
297
- if ($ IsLinux -or $IsMacOS ) {
304
+ if (( $PSVersionTable .PSVersion.Major -ge 6 ) -and ( $ IsLinux -or $IsMacOS ) ) {
298
305
Set-NamedPipeMode - PipeFile $ResultTable [$PipeNameKey ]
299
306
}
300
307
}
@@ -349,11 +356,12 @@ try {
349
356
- WaitForDebugger:$WaitForDebugger.IsPresent
350
357
break
351
358
}
359
+
352
360
" NamedPipeSimplex" {
353
- $LanguageServiceInPipeName = Test-NamedPipeName - OrCreate - IfNull $LanguageServiceInPipeName
354
- $LanguageServiceOutPipeName = Test-NamedPipeName - OrCreate - IfNull $LanguageServiceOutPipeName
355
- $DebugServiceInPipeName = Test-NamedPipeName - OrCreate - IfNull $DebugServiceInPipeName
356
- $DebugServiceOutPipeName = Test-NamedPipeName - OrCreate - IfNull $DebugServiceOutPipeName
361
+ $LanguageServiceInPipeName = Get-ValidatedNamedPipeName $LanguageServiceInPipeName
362
+ $LanguageServiceOutPipeName = Get-ValidatedNamedPipeName $LanguageServiceOutPipeName
363
+ $DebugServiceInPipeName = Get-ValidatedNamedPipeName $DebugServiceInPipeName
364
+ $DebugServiceOutPipeName = Get-ValidatedNamedPipeName $DebugServiceOutPipeName
357
365
358
366
$editorServicesHost = Start-EditorServicesHost `
359
367
- HostName $HostName `
@@ -377,9 +385,10 @@ try {
377
385
Set-PipeFileResult $resultDetails " debugServiceWritePipeName" $DebugServiceOutPipeName
378
386
break
379
387
}
388
+
380
389
Default {
381
- $LanguageServicePipeName = Test-NamedPipeName - OrCreate - IfNull $LanguageServicePipeName
382
- $DebugServicePipeName = Test-NamedPipeName - OrCreate - IfNull $DebugServicePipeName
390
+ $LanguageServicePipeName = Get-ValidatedNamedPipeName $LanguageServicePipeName
391
+ $DebugServicePipeName = Get-ValidatedNamedPipeName $DebugServicePipeName
383
392
384
393
$editorServicesHost = Start-EditorServicesHost `
385
394
- HostName $HostName `
@@ -417,7 +426,7 @@ catch [System.Exception] {
417
426
418
427
Log " ERRORS caught starting up EditorServicesHost"
419
428
420
- while ($e -ne $null ) {
429
+ while ($null -ne $e ) {
421
430
$errorString = $errorString + ($e.Message + " `r`n " + $e.StackTrace + " `r`n " )
422
431
$e = $e.InnerException ;
423
432
Log $errorString
@@ -438,7 +447,7 @@ catch [System.Exception] {
438
447
439
448
Log " ERRORS caught while waiting for EditorServicesHost to complete execution"
440
449
441
- while ($e -ne $null ) {
450
+ while ($null -ne $e ) {
442
451
$errorString = $errorString + ($e.Message + " `r`n " + $e.StackTrace + " `r`n " )
443
452
$e = $e.InnerException ;
444
453
Log $errorString
0 commit comments