Skip to content

Commit a832aff

Browse files
author
Philip Sampaio
committed
Fix secundary scripts to accept args without quoting issues
We now define the prepended args in an array and we "source" the main script in order to process with those args. This way it's easier to just pass down the args and let the main script normalize them.
1 parent 8f9efa7 commit a832aff

File tree

4 files changed

+46
-87
lines changed

4 files changed

+46
-87
lines changed

bin/elixir.ps1

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ $ELIXIR_VERSION = "1.18.0-dev"
55
$scriptPath = Split-Path -Parent $PSCommandPath
66
$erlExec = "erl"
77

8+
# The iex.ps1, elixirc.ps1 and mix.ps1 scripts may populate this var.
9+
if ($null -eq $allArgs) {
10+
$allArgs = $args
11+
}
12+
813
function PrintElixirHelp {
914
$scriptName = Split-Path -Leaf $PSCommandPath
1015
$help = @"
@@ -66,12 +71,12 @@ See run_erl to learn more. To reattach, run: to_erl PIPEDIR.
6671
Write-Host $help
6772
}
6873

69-
if (($args.Count -eq 1) -and ($args[0] -eq "--short-version")) {
74+
if (($allArgs.Count -eq 1) -and ($allArgs[0] -eq "--short-version")) {
7075
Write-Host "$ELIXIR_VERSION"
7176
exit
7277
}
7378

74-
if (($args.Count -eq 0) -or (($args.Count -eq 1) -and ($args[0] -in @("-h", "--help")))) {
79+
if (($allArgs.Count -eq 0) -or (($allArgs.Count -eq 1) -and ($allArgs[0] -in @("-h", "--help")))) {
7580
PrintElixirHelp
7681
exit 1
7782
}
@@ -111,12 +116,12 @@ $allOtherParams = @()
111116
$runErlPipe = $null
112117
$runErlLog = $null
113118

114-
for ($i = 0; $i -lt $args.Count; $i++) {
115-
$private:arg = $args[$i]
119+
for ($i = 0; $i -lt $allArgs.Count; $i++) {
120+
$private:arg = $allArgs[$i]
116121

117122
switch ($arg) {
118123
{ $_ -in @("-e", "-r", "-pr", "-pa", "-pz", "--eval", "--remsh", "--dot-iex", "--dbg") } {
119-
$private:nextArg = NormalizeArg($args[++$i])
124+
$private:nextArg = NormalizeArg($allArgs[++$i])
120125

121126
$elixirParams += $arg
122127
$elixirParams += $nextArg
@@ -142,7 +147,7 @@ for ($i = 0; $i -lt $args.Count; $i++) {
142147

143148
"--cookie" {
144149
$erlangParams += "-setcookie"
145-
$erlangParams += $args[++$i]
150+
$erlangParams += $allArgs[++$i]
146151
break
147152
}
148153

@@ -153,52 +158,52 @@ for ($i = 0; $i -lt $args.Count; $i++) {
153158

154159
"--name" {
155160
$erlangParams += "-name"
156-
$erlangParams += $args[++$i]
161+
$erlangParams += $allArgs[++$i]
157162
break
158163
}
159164

160165
"--sname" {
161166
$erlangParams += "-sname"
162-
$erlangParams += $args[++$i]
167+
$erlangParams += $allArgs[++$i]
163168
break
164169
}
165170

166171
"--boot" {
167172
$erlangParams += "-boot"
168-
$erlangParams += $args[++$i]
173+
$erlangParams += $allArgs[++$i]
169174
break
170175
}
171176

172177
"--erl-config" {
173178
$erlangParams += "-config"
174-
$erlangParams += $args[++$i]
179+
$erlangParams += $allArgs[++$i]
175180
break
176181
}
177182

178183
"--vm-args" {
179184
$erlangParams += "-args_file"
180-
$erlangParams += $args[++$i]
185+
$erlangParams += $allArgs[++$i]
181186
break
182187
}
183188

184189
"--logger-otp-reports" {
185-
$private:tempVal = $args[$i + 1]
190+
$private:tempVal = $allArgs[$i + 1]
186191
if ($tempVal -in @("true", "false")) {
187-
$erlangParams.AddRange([string[]]@("-logger", "handle_otp_reports", $args[++$i]))
192+
$erlangParams += @("-logger", "handle_otp_reports", $allArgs[++$i])
188193
}
189194
break
190195
}
191196

192197
"--logger-sasl-reports" {
193-
$private:tempVal = $args[$i + 1]
198+
$private:tempVal = $allArgs[$i + 1]
194199
if ($tempVal -in @("true", "false")) {
195-
$erlangParams.AddRange([string[]]@("-logger", "handle_sasl_reports", $args[++$i]))
200+
$erlangParams += @("-logger", "handle_sasl_reports", $allArgs[++$i])
196201
}
197202
break
198203
}
199204

200205
"--erl" {
201-
$private:erlFlags = $args[++$i] -split " "
206+
$private:erlFlags = $allArgs[++$i] -split " "
202207
$beforeExtras += $erlFlags
203208
break
204209
}
@@ -216,8 +221,8 @@ for ($i = 0; $i -lt $args.Count; $i++) {
216221
}
217222

218223
"--rpc-eval" {
219-
$private:key = $args[++$i]
220-
$private:value = $args[++$i]
224+
$private:key = $allArgs[++$i]
225+
$private:value = $allArgs[++$i]
221226

222227
if ($null -eq $key) {
223228
Write-Error "--rpc-eval: NODE must be present"
@@ -236,8 +241,8 @@ for ($i = 0; $i -lt $args.Count; $i++) {
236241
}
237242

238243
"--boot-var" {
239-
$private:key = $args[++$i]
240-
$private:value = $args[++$i]
244+
$private:key = $allArgs[++$i]
245+
$private:value = $allArgs[++$i]
241246

242247
if ($null -eq $key) {
243248
Write-Error "--boot-var: VAR must be present"
@@ -256,8 +261,8 @@ for ($i = 0; $i -lt $args.Count; $i++) {
256261
}
257262

258263
"--pipe-to" {
259-
$runErlPipe = $args[++$i]
260-
$runErlLog = $args[++$i]
264+
$runErlPipe = $allArgs[++$i]
265+
$runErlLog = $allArgs[++$i]
261266

262267
if ($null -eq $runErlPipe) {
263268
Write-Error "--pipe-to: PIPEDIR must be present"

bin/elixirc.ps1

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,11 @@ Usage: $scriptName [elixir switches] [compiler switches] [.ex files]
2525
}
2626

2727
$scriptPath = Split-Path -Parent $PSCommandPath
28-
$command = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
29-
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-
}
28+
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
29+
30+
$prependedArgs = @("+elixirc")
4831

49-
$quotedArgs = $args | forEach-Object -Process { QuoteString($_) }
32+
$allArgs = $prependedArgs + $args
5033

51-
Invoke-Expression "$command +elixirc $($quotedArgs -join " ")"
34+
# The dot is going to evaluate the script with the vars defined here.
35+
. $elixirMainScript

bin/iex.ps1

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,27 +19,12 @@ 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-
4122
$scriptPath = Split-Path -Parent $PSCommandPath
42-
$command = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
43-
$quotedArgs = $args | forEach-Object -Process { QuoteString($_) }
23+
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
24+
25+
$prependedArgs = @("--no-halt", "--erl", "-user elixir", "+iex")
26+
27+
$allArgs = $prependedArgs + $args
4428

45-
Invoke-Expression "$command --no-halt --erl `"-user elixir`" +iex $($quotedArgs -join " ")"
29+
# The dot is going to evaluate the script with the vars defined here.
30+
. $elixirMainScript

bin/mix.ps1

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,13 @@
11
#!/usr/bin/env pwsh
22

33
$scriptPath = Split-Path -Parent $PSCommandPath
4-
$command = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
5-
$mixFile = Join-Path -Path $scriptPath -ChildPath "mix"
4+
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
65

7-
function QuoteString {
8-
param(
9-
[Parameter(ValueFromPipeline = $true)]
10-
[string] $Item
11-
)
6+
$mixFile = Join-Path -Path $scriptPath -ChildPath "mix"
127

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-
}
8+
$prependedArgs = @($mixFile)
259

26-
$quotedArgs = $args | forEach-Object -Process { QuoteString($_) }
10+
$allArgs = $prependedArgs + $args
2711

28-
Invoke-Expression "$command $mixFile $($quotedArgs -join " ")"
12+
# The dot is going to evaluate the script with the vars defined here.
13+
. $elixirMainScript

0 commit comments

Comments
 (0)