Skip to content

Commit 6603ff7

Browse files
author
Philip Sampaio
committed
Final adjustments to make the --pipe-to feature work
1 parent 8da5087 commit 6603ff7

File tree

1 file changed

+83
-30
lines changed

1 file changed

+83
-30
lines changed

bin/elixir.ps1

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,10 @@ function QuotedString {
9696
[string] $Item
9797
)
9898

99-
# We surround the string with double quotes, in order to preserve its contents.
99+
# We surround the string with double quotes, in order to preserve its contents as
100+
# only one command arg.
101+
# This is needed because PowerShell will consider a new argument when it sees a
102+
# space char.
100103
if (-not $Item.StartsWith("`"") -and $Item.Contains(" ")) {
101104
"`"$Item`""
102105
}
@@ -110,14 +113,18 @@ $ErlangParams = New-Object Collections.Generic.List[String]
110113
$BeforeExtras = New-Object Collections.Generic.List[String]
111114
$AllOtherParams = New-Object Collections.Generic.List[String]
112115

116+
$RunErlPipe = $null
117+
$RunErlLog = $null
118+
113119
for ($i = 0; $i -lt $Args.Count; $i++) {
114120
$private:Arg = $Args[$i]
115121

116122
switch ($Arg) {
117123
{ $_ -in @("-e", "-r", "-pr", "-pa", "-pz", "--eval", "--remsh", "--dot-iex", "--dbg") } {
118124
$private:NextArg = $Args[++$i] | NormalizeArg | QuotedString
119125

120-
$ElixirParams.Add("$Arg $NextArg")
126+
$ElixirParams.Add($Arg)
127+
$ElixirParams.Add($NextArg)
121128

122129
break
123130
}
@@ -139,7 +146,8 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
139146
}
140147

141148
"--cookie" {
142-
$ErlangParams.Add("-setcookie $($Args[++$i])")
149+
$ErlangParams.Add("-setcookie")
150+
$ErlangParams.Add($Args[++$i])
143151
break
144152
}
145153

@@ -149,44 +157,47 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
149157
}
150158

151159
"--name" {
152-
$ErlangParams.Add("-name $($Args[++$i])")
160+
$ErlangParams.Add("-name")
161+
$ErlangParams.Add($Args[++$i])
153162
break
154163
}
155164

156165
"--sname" {
157-
$ErlangParams.Add("-sname $($Args[++$i])")
166+
$ErlangParams.Add("-sname")
167+
$ErlangParams.Add($Args[++$i])
158168
break
159169
}
160170

161171
"--boot" {
162-
$ErlangParams.Add("-boot $($Args[++$i])")
172+
$ErlangParams.Add("-boot")
173+
$ErlangParams.Add($Args[++$i])
163174
break
164175
}
165176

166177
"--erl-config" {
167-
$ErlangParams.Add("-config $($Args[++$i])")
178+
$ErlangParams.Add("-config")
179+
$ErlangParams.Add($Args[++$i])
168180
break
169181
}
170182

171183
"--vm-args" {
172-
$ErlangParams.Add("-args_file $($Args[++$i])")
184+
$ErlangParams.Add("-args_file")
185+
$ErlangParams.Add($Args[++$i])
173186
break
174187
}
175188

176189
"--logger-otp-reports" {
177190
$private:TempVal = $Args[$i + 1]
178191
if ($TempVal -in @("true", "false")) {
179-
180-
$ErlangParams.Add("-logger handle_otp_reports $($Args[++$i])")
192+
$ErlangParams.AddRange([string[]]@("-logger", "handle_otp_reports", $Args[++$i]))
181193
}
182194
break
183195
}
184196

185197
"--logger-sasl-reports" {
186198
$private:TempVal = $Args[$i + 1]
187199
if ($TempVal -in @("true", "false")) {
188-
189-
$ErlangParams.Add("-logger handle_sasl $($Args[++$i])")
200+
$ErlangParams.AddRange([string[]]@("-logger", "handle_sasl", $Args[++$i]))
190201
}
191202
break
192203
}
@@ -210,23 +221,28 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
210221
break
211222
}
212223

213-
"+elixirc" { $ElixirParams.Add("+elixirc"); break }
224+
"+elixirc" {
225+
$ElixirParams.Add("+elixirc")
226+
break
227+
}
214228

215229
"--rpc-eval" {
216230
$private:Key = $Args[++$i]
217231
$private:Value = $Args[++$i]
218232

219233
if ($null -eq $Key) {
220234
Write-Error "--rpc-eval: NODE must be present"
221-
exit
235+
exit 1
222236
}
223237

224238
if ($null -eq $Value) {
225239
Write-Error "--rpc-eval: COMMAND for the '$Key' node must be present"
226-
exit
240+
exit 1
227241
}
228242

229-
$ElixirParams.Add("--rpc-eval $Key $(QuotedString $Value)")
243+
$ElixirParams.Add("--rpc-eval")
244+
$ElixirParams.Add($Key)
245+
$ElixirParams.Add("$(QuotedString $Value)")
230246
break
231247
}
232248

@@ -236,23 +252,39 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
236252

237253
if ($null -eq $Key) {
238254
Write-Error "--boot-var: VAR must be present"
239-
exit
255+
exit 1
240256
}
241257

242258
if ($null -eq $Value) {
243259
Write-Error "--boot-var: Value for the '$Key' var must be present"
244-
exit
260+
exit 1
245261
}
246262

247-
$ErlangParams.Add("-boot_var $Key $(QuotedString $Value)")
263+
$ElixirParams.Add("-boot_var")
264+
$ElixirParams.Add($Key)
265+
$ElixirParams.Add("$(QuotedString $Value)")
248266
break
249267
}
250268

251269
"--pipe-to" {
252-
$private:Key = $Args[++$i]
253-
$private:Value = $Args[++$i]
270+
$RunErlPipe = $Args[++$i]
271+
$RunErlLog = $Args[++$i]
272+
273+
if ($null -eq $RunErlPipe) {
274+
Write-Error "--pipe-to: PIPEDIR must be present"
275+
exit 1
276+
}
277+
278+
if ($null -eq $RunErlLog) {
279+
Write-Error "--pipe-to: PIPELOG must be present"
280+
exit 1
281+
}
282+
283+
if ($RunErlPipe.EndsWith("/") -or $RunErlLog.EndsWith("/")) {
284+
Write-Error "--pipe-to: PIPEDIR and PIPELOG must not end with a slash"
285+
exit 1
286+
}
254287

255-
Write-Warning "--pipe-to: Option is not yet supported. Ignoring $Key $Value"
256288
break
257289
}
258290

@@ -262,7 +294,6 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
262294
$AllOtherParams.Add($Normalized)
263295
}
264296
else {
265-
# We add quotes to preserve contents, since the quotes are removed by Powershell.
266297
$AllOtherParams.Add("$(QuotedString $Normalized)")
267298
}
268299
break
@@ -275,16 +306,16 @@ for ($i = 0; $i -lt $Args.Count; $i++) {
275306
if ($PSStyle.OutputRendering -ne "PlainText") {
276307
# TODO: find a way to detect if the term is interactive on Windows.
277308
if ($IsWindows -or (test -t 1 -a -t 2)) {
278-
$BeforeExtras.Insert(0, "-elixir ansi_enabled true")
309+
$BeforeExtras.InsertRange(0, [string[]]@("-elixir", "ansi_enabled", "true"))
279310
}
280311
}
281312

282313
if ($null -eq $UseIEx) {
283-
$BeforeExtras.Insert(0, "-s elixir start_cli")
314+
$BeforeExtras.InsertRange(0, [string[]]@("-s", "elixir", "start_cli"))
284315
}
285316

286-
$BeforeExtras.Insert(0, "-pa $(Join-Path $ScriptPath -ChildPath "../lib/elixir/ebin")")
287-
$BeforeExtras.Insert(0, "-noshell -elixir_root $(Join-Path $ScriptPath -ChildPath "../lib")")
317+
$BeforeExtras.InsertRange(0, [string[]]@("-pa", "$(Join-Path $ScriptPath -ChildPath "../lib/elixir/ebin")"))
318+
$BeforeExtras.InsertRange(0, [string[]]@("-noshell", "-elixir_root", "$(Join-Path $ScriptPath -ChildPath "../lib")"))
288319

289320
# One MAY change ERTS_BIN= but you MUST NOT change
290321
# ERTS_BIN=$ERTS_BIN as it is handled by Elixir releases.
@@ -303,8 +334,6 @@ $AllParams.Add("-extra")
303334
$AllParams.AddRange($ElixirParams)
304335
$AllParams.AddRange($AllOtherParams)
305336

306-
$ParamsPart = [string]::Join(" ", $AllParams)
307-
308337
$BinSuffix = ""
309338

310339
if ($IsWindows) {
@@ -317,10 +346,34 @@ if ($ERTS_BIN) {
317346
$BinPath = Join-Path -Path $ERTS_BIN -ChildPath $BinPath
318347
}
319348

349+
if ($null -eq $RunErlPipe) {
350+
$ParamsPart = [string]::Join(" ", $AllParams)
351+
}
352+
else {
353+
$private:OrigBinPath = $BinPath
354+
$ErlExec = "run_erl"
355+
$BinPath = "$ErlExec$BinSuffix"
356+
357+
if ($ERTS_BIN) {
358+
$BinPath = Join-Path -Path $ERTS_BIN -ChildPath $BinPath
359+
}
360+
361+
$AllParams.Insert(0, $OrigBinPath)
362+
363+
# We only scape double-quotes because the command will be surrounded by double-quotes.
364+
$private:Escaped = $AllParams | ForEach-Object -Process { $_ -replace "`"", "\$&" }
365+
366+
$ParamsPart = "-daemon `"$RunErlPipe/`" `"$RunErlLog/`" `"$([string]::Join(" ", $Escaped))`""
367+
}
368+
320369
if ($Env:ELIXIR_CLI_DRY_RUN) {
321370
Write-Host "$BinPath $ParamsPart"
322371
}
323372
else {
324-
$Output = Start-Process -FilePath $BinPath -ArgumentList $ParamsPart -NoNewWindow -Wait -PassThru
373+
if ($RunErlPipe) {
374+
$null = New-Item -Path "." -ItemType "directory" -Name "$RunErlPipe" -Force
375+
$null = New-Item -Path "." -ItemType "directory" -Name "$RunErlLog" -Force
376+
}
377+
$Output = Start-Process -FilePath $BinPath -ArgumentList "$ParamsPart" -NoNewWindow -Wait -PassThru
325378
exit $Output.ExitCode
326379
}

0 commit comments

Comments
 (0)