Skip to content

Commit 6b25505

Browse files
author
Philip Sampaio
committed
Fix processing of args for secundary PowerShell scripts
This is needed to preserve the contents of arguments that contain spaces when we call the main "bin/elixir.ps1" script using "Invoke-Expression".
1 parent c86133a commit 6b25505

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

bin/elixir.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ for ($i = 0; $i -lt $args.Count; $i++) {
198198
}
199199

200200
"--erl" {
201-
$beforeExtras.Add($args[++$i])
201+
$private:erlFlags = $args[++$i] -split " "
202+
$beforeExtras.AddRange($erlFlags)
202203
break
203204
}
204205

@@ -297,11 +298,13 @@ $beforeExtras.InsertRange(0, [string[]]@("-noshell", "-elixir_root", "$(Join-Pat
297298
# $ERTS_BIN=
298299
$ERTS_BIN = "$env:ERTS_BIN"
299300

300-
$ELIXIR_ERL_OPTIONS = "$env:ELIXIR_ERL_OPTIONS"
301-
302301
$allParams = New-Object Collections.Generic.List[String]
303302

304-
$allParams.Add($ELIXIR_ERL_OPTIONS)
303+
if ($null -ne $env:ELIXIR_ERL_OPTIONS) {
304+
$private:erlFlags = $env:ELIXIR_ERL_OPTIONS -split " "
305+
$allParams.AddRange($erlFlags)
306+
}
307+
305308
$allParams.AddRange($erlangParams)
306309
$allParams.AddRange($beforeExtras)
307310
$allParams.Add("-extra")
@@ -335,10 +338,10 @@ else {
335338
}
336339

337340
# We scape special chars using the Unix style of scaping, with "\".
338-
# But first we escape the double-quotes.
341+
# But first we escape the double-quotes, because for some reason they are not escaped in the same regex.
339342
$private:escaped = $allParams | ForEach-Object -Process { ($_ -replace "`"", "\`"") -replace "[^a-zA-Z0-9_/-]", "\$&" }
340343

341-
# The args are surrounded here for the same reasons
344+
# The args are surrounded here because we want to have only one argument for the entire command.
342345
$paramsPart = @("-daemon","`"$runErlPipe/`"", "`"$runErlLog/`"", "`"$($escaped -join " ")`"")
343346
}
344347

bin/elixirc.ps1

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,25 @@ Usage: $scriptName [elixir switches] [compiler switches] [.ex files]
2727
$scriptPath = Split-Path -Parent $PSCommandPath
2828
$command = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
2929

30-
Invoke-Expression "$command +elixirc $($args -join " ")"
30+
function QuoteString {
31+
param(
32+
[Parameter(ValueFromPipeline = $true)]
33+
[string] $Item
34+
)
35+
36+
# We surround the string with double quotes, in order to preserve its contents as
37+
# only one command arg.
38+
# This is needed because PowerShell consider spaces as separator of arguments.
39+
# The double quotes around will be removed when PowerShell process the argument.
40+
# See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.4#passing-quoted-strings-to-external-commands
41+
if ($Item.Contains(" ")) {
42+
'"' + $Item + '"'
43+
}
44+
else {
45+
$Item
46+
}
47+
}
48+
49+
$quotedArgs = $args | forEach-Object -Process { QuoteString($_) }
50+
51+
Invoke-Expression "$command +elixirc $($quotedArgs -join " ")"

bin/iex.ps1

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,27 @@ It accepts all other options listed by "elixir --help".
1919
exit
2020
}
2121

22+
function QuoteString {
23+
param(
24+
[Parameter(ValueFromPipeline = $true)]
25+
[string] $Item
26+
)
27+
28+
# We surround the string with double quotes, in order to preserve its contents as
29+
# only one command arg.
30+
# This is needed because PowerShell consider spaces as separator of arguments.
31+
# The double quotes around will be removed when PowerShell process the argument.
32+
# See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.4#passing-quoted-strings-to-external-commands
33+
if ($Item.Contains(" ")) {
34+
'"' + $Item + '"'
35+
}
36+
else {
37+
$Item
38+
}
39+
}
40+
2241
$scriptPath = Split-Path -Parent $PSCommandPath
2342
$command = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
43+
$quotedArgs = $args | forEach-Object -Process { QuoteString($_) }
2444

25-
Invoke-Expression "$command --no-halt --erl `"-user elixir`" +iex $($args -join " ")"
45+
Invoke-Expression "$command --no-halt --erl `"-user elixir`" +iex $($quotedArgs -join " ")"

bin/mix.ps1

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,25 @@ $scriptPath = Split-Path -Parent $PSCommandPath
44
$command = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
55
$mixFile = Join-Path -Path $scriptPath -ChildPath "mix"
66

7-
Invoke-Expression "$command $mixFile $($args -join " ")"
7+
function QuoteString {
8+
param(
9+
[Parameter(ValueFromPipeline = $true)]
10+
[string] $Item
11+
)
12+
13+
# We surround the string with double quotes, in order to preserve its contents as
14+
# only one command arg.
15+
# This is needed because PowerShell consider spaces as separator of arguments.
16+
# The double quotes around will be removed when PowerShell process the argument.
17+
# See: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-7.4#passing-quoted-strings-to-external-commands
18+
if ($Item.Contains(" ")) {
19+
'"' + $Item + '"'
20+
}
21+
else {
22+
$Item
23+
}
24+
}
25+
26+
$quotedArgs = $args | forEach-Object -Process { QuoteString($_) }
27+
28+
Invoke-Expression "$command $mixFile $($quotedArgs -join " ")"

0 commit comments

Comments
 (0)