1
1
# !/usr/bin/env pwsh
2
2
3
- $MinPowerShellVersion = [semver ]::new( 7 , 2 , 0 )
3
+ $MinPowerShellVersion = [version ] " 7.2.0 "
4
4
5
- if ($MinPowerShellVersion.CompareTo ($PSVersionTable.PSVersion ) -eq 1 ) {
5
+ if ($MinPowerShellVersion.CompareTo ([ version ] $PSVersionTable.PSVersion ) -eq 1 ) {
6
6
Write-Error " This script requires PowerShell version 7.2 or above. Running on $ ( $PSVersionTable.PSVersion ) "
7
7
}
8
8
@@ -79,7 +79,30 @@ if (($Args.Count -eq 1) -and ($Args[0] -eq "--short-version")) {
79
79
80
80
if (($Args.Count -eq 0 ) -or (($Args.Count -eq 1 ) -and ($Args [0 ] -in @ (" -h" , " --help" )))) {
81
81
PrintElixirHelp
82
- exit
82
+ exit 1
83
+ }
84
+
85
+ function NormalizeArg {
86
+ param (
87
+ [Parameter (Mandatory = $true , ValueFromPipeline = $true )]
88
+ [string []] $Items
89
+ )
90
+ [string ]::Join(" ," , $Items )
91
+ }
92
+
93
+ function QuotedString {
94
+ param (
95
+ [Parameter (Mandatory = $true , ValueFromPipeline = $true )]
96
+ [string ] $Item
97
+ )
98
+
99
+ # We surround the string with double quotes, in order to preserve its contents.
100
+ if (-not $Item.StartsWith (" `" " ) -and $Item.Contains (" " )) {
101
+ " `" $Item `" "
102
+ }
103
+ else {
104
+ $Item
105
+ }
83
106
}
84
107
85
108
$ElixirParams = New-Object Collections.Generic.List[String ]
@@ -92,11 +115,25 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
92
115
93
116
switch ($Arg ) {
94
117
{ $_ -in @ (" -e" , " -r" , " -pr" , " -pa" , " -pz" , " --eval" , " --remsh" , " --dot-iex" , " --dbg" ) } {
95
- $ElixirParams.Add (" $Arg $ ( $Args [++ $i ]) " )
118
+ $private :NextArg = $Args [++ $i ] | NormalizeArg | QuotedString
119
+
120
+ $ElixirParams.Add (" $Arg $NextArg " )
121
+
122
+ break
123
+ }
124
+
125
+ { $_ -in @ (" -v" , " --version" , " --help" , " -h" ) } {
126
+ # Standalone options goes only once in the Elixir params, when they are empty.
127
+ if ($ElixirParams.Count -eq 0 ) {
128
+ $ElixirParams.Add ($Arg )
129
+ }
130
+ else {
131
+ $AllOtherParams.Add ($Arg )
132
+ }
96
133
break
97
134
}
98
135
99
- { $_ -in @ ( " -v " , " --version " , " -- no-halt" ) } {
136
+ " -- no-halt" {
100
137
$ElixirParams.Add ($Arg )
101
138
break
102
139
}
@@ -189,7 +226,7 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
189
226
exit
190
227
}
191
228
192
- $ElixirParams.Add (" --rpc-eval $Key $Value " )
229
+ $ElixirParams.Add (" --rpc-eval $Key $ ( QuotedString $ Value) " )
193
230
break
194
231
}
195
232
@@ -207,7 +244,7 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
207
244
exit
208
245
}
209
246
210
- $ErlangParams.Add (" -boot_var $Key $Value " )
247
+ $ErlangParams.Add (" -boot_var $Key $ ( QuotedString $ Value) " )
211
248
break
212
249
}
213
250
@@ -220,11 +257,13 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
220
257
}
221
258
222
259
Default {
223
- if ($Arg -is [string ]) {
224
- $AllOtherParams.Add ($Arg )
260
+ $private :Normalized = NormalizeArg $Arg
261
+ if ($Normalized.StartsWith (" -" )) {
262
+ $AllOtherParams.Add ($Normalized )
225
263
}
226
264
else {
227
- $AllOtherParams.Add ([string ]::Join(" ," , $Arg ))
265
+ # We add quotes to preserve contents, since the quotes are removed by Powershell.
266
+ $AllOtherParams.Add (" $ ( QuotedString $Normalized ) " )
228
267
}
229
268
break
230
269
}
@@ -234,7 +273,10 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
234
273
# Support for ANSI is only disable if TERM or NO_COLOR env vars are set.
235
274
# This will change the $PSStyle.OutputRendering property.
236
275
if ($PSStyle.OutputRendering -ne " PlainText" ) {
237
- $BeforeExtras.Insert (0 , " -elixir ansi_enabled true" )
276
+ # TODO: find a way to detect if the term is interactive on Windows.
277
+ if ($IsWindows -or (test - t 1 - a - t 2 )) {
278
+ $BeforeExtras.Insert (0 , " -elixir ansi_enabled true" )
279
+ }
238
280
}
239
281
240
282
if ($null -eq $UseIEx ) {
@@ -261,7 +303,7 @@ $AllParams.Add("-extra")
261
303
$AllParams.AddRange ($ElixirParams )
262
304
$AllParams.AddRange ($AllOtherParams )
263
305
264
- $ParamsPart = [string ]::Join(" " , ( $AllParams | Where-Object { $_ -ne " " }) )
306
+ $ParamsPart = [string ]::Join(" " , $AllParams )
265
307
266
308
$BinSuffix = " "
267
309
@@ -275,15 +317,10 @@ if ($ERTS_BIN) {
275
317
$BinPath = Join-Path - Path $ERTS_BIN - ChildPath $BinPath
276
318
}
277
319
278
- $Command = " $BinPath $ParamsPart "
279
-
280
- if ($ErlExec -eq " werl" ) {
281
- $Command = " start `"`" $Command "
282
- }
283
-
284
320
if ($Env: ELIXIR_CLI_DRY_RUN ) {
285
- Write-Host $Command
321
+ Write-Host " $BinPath $ParamsPart "
286
322
}
287
323
else {
288
- Invoke-Expression $Command
324
+ $Output = Start-Process - FilePath $BinPath - ArgumentList $ParamsPart - NoNewWindow - Wait - PassThru
325
+ exit $Output.ExitCode
289
326
}
0 commit comments