Skip to content

Commit ac933f7

Browse files
author
Philip Sampaio
authored
Add PowerShell scripts (#13592)
The idea is to provide them as alternative to bat scripts and potentially replace all bat scripts in the future. One benefit of using PowerShell is that we can also run it on Linux systems, facilitating dev and testing.
1 parent 63d0932 commit ac933f7

File tree

6 files changed

+462
-72
lines changed

6 files changed

+462
-72
lines changed

bin/elixir.ps1

+304
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,304 @@
1+
#!/usr/bin/env pwsh
2+
3+
$ELIXIR_VERSION = "1.18.0-dev"
4+
5+
$scriptPath = Split-Path -Parent $PSCommandPath
6+
$erlExec = "erl"
7+
8+
# The iex.ps1, elixirc.ps1 and mix.ps1 scripts may populate this var.
9+
if ($null -eq $allArgs) {
10+
$allArgs = $args
11+
}
12+
13+
function PrintElixirHelp {
14+
$scriptName = Split-Path -Leaf $PSCommandPath
15+
$help = @"
16+
Usage: $scriptName [options] [.exs file] [data]
17+
18+
## General options
19+
20+
-e "COMMAND" Evaluates the given command (*)
21+
-h, --help Prints this message (standalone)
22+
-r "FILE" Requires the given files/patterns (*)
23+
-S SCRIPT Finds and executes the given script in `$PATH
24+
-pr "FILE" Requires the given files/patterns in parallel (*)
25+
-pa "PATH" Prepends the given path to Erlang code path (*)
26+
-pz "PATH" Appends the given path to Erlang code path (*)
27+
-v, --version Prints Erlang/OTP and Elixir versions (standalone)
28+
29+
--erl "SWITCHES" Switches to be passed down to Erlang (*)
30+
--eval "COMMAND" Evaluates the given command, same as -e (*)
31+
--logger-otp-reports BOOL Enables or disables OTP reporting
32+
--logger-sasl-reports BOOL Enables or disables SASL reporting
33+
--no-halt Does not halt the Erlang VM after execution
34+
--short-version Prints Elixir version (standalone)
35+
36+
Options given after the .exs file or -- are passed down to the executed code.
37+
Options can be passed to the Erlang runtime using `$ELIXIR_ERL_OPTIONS or --erl.
38+
39+
## Distribution options
40+
41+
The following options are related to node distribution.
42+
43+
--cookie COOKIE Sets a cookie for this distributed node
44+
--hidden Makes a hidden node
45+
--name NAME Makes and assigns a name to the distributed node
46+
--rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
47+
--sname NAME Makes and assigns a short name to the distributed node
48+
49+
--name and --sname may be set to undefined so one is automatically generated.
50+
51+
## Release options
52+
53+
The following options are generally used under releases.
54+
55+
--boot "FILE" Uses the given FILE.boot to start the system
56+
--boot-var VAR "VALUE" Makes `$VAR available as VALUE to FILE.boot (*)
57+
--erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
58+
--vm-args "FILE" Passes the contents in file as arguments to the VM
59+
60+
--pipe-to is not supported via PowerShell.
61+
62+
** Options marked with (*) can be given more than once.
63+
** Standalone options can't be combined with other options.
64+
"@
65+
66+
Write-Host $help
67+
}
68+
69+
if (($allArgs.Count -eq 1) -and ($allArgs[0] -eq "--short-version")) {
70+
Write-Host "$ELIXIR_VERSION"
71+
exit
72+
}
73+
74+
if (($allArgs.Count -eq 0) -or (($allArgs.Count -eq 1) -and ($allArgs[0] -in @("-h", "--help")))) {
75+
PrintElixirHelp
76+
exit 1
77+
}
78+
79+
function NormalizeArg {
80+
param(
81+
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
82+
[string[]] $Items
83+
)
84+
$Items -join ","
85+
}
86+
87+
function QuoteString {
88+
param(
89+
[Parameter(ValueFromPipeline = $true)]
90+
[string] $Item
91+
)
92+
93+
# We surround the string with double quotes, in order to preserve its contents as
94+
# only one command arg.
95+
# This is needed because PowerShell consider spaces as separator of arguments.
96+
# The double quotes around will be removed when PowerShell process the argument.
97+
# 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
98+
if ($Item.Contains(" ")) {
99+
'"' + $Item + '"'
100+
}
101+
else {
102+
$Item
103+
}
104+
}
105+
106+
$elixirParams = @()
107+
$erlangParams = @()
108+
$beforeExtras = @()
109+
$allOtherParams = @()
110+
111+
$runErlPipe = $null
112+
$runErlLog = $null
113+
114+
for ($i = 0; $i -lt $allArgs.Count; $i++) {
115+
$private:arg = $allArgs[$i]
116+
117+
switch -exact ($arg) {
118+
{ $_ -in @("-e", "-r", "-pr", "-pa", "-pz", "--eval", "--remsh", "--dot-iex", "--dbg") } {
119+
$private:nextArg = NormalizeArg($allArgs[++$i])
120+
121+
$elixirParams += $arg
122+
$elixirParams += $nextArg
123+
124+
break
125+
}
126+
127+
{ $_ -in @("-v", "--version") } {
128+
# Standalone options goes only once in the Elixir params, when they are empty.
129+
if (($elixirParams.Count -eq 0) -and ($allOtherParams.Count -eq 0)) {
130+
$elixirParams += $arg
131+
}
132+
else {
133+
$allOtherParams += $arg
134+
}
135+
break
136+
}
137+
138+
"--no-halt" {
139+
$elixirParams += $arg
140+
break
141+
}
142+
143+
"--cookie" {
144+
$erlangParams += "-setcookie"
145+
$erlangParams += $allArgs[++$i]
146+
break
147+
}
148+
149+
"--hidden" {
150+
$erlangParams += "-hidden"
151+
break
152+
}
153+
154+
"--name" {
155+
$erlangParams += "-name"
156+
$erlangParams += $allArgs[++$i]
157+
break
158+
}
159+
160+
"--sname" {
161+
$erlangParams += "-sname"
162+
$erlangParams += $allArgs[++$i]
163+
break
164+
}
165+
166+
"--boot" {
167+
$erlangParams += "-boot"
168+
$erlangParams += $allArgs[++$i]
169+
break
170+
}
171+
172+
"--erl-config" {
173+
$erlangParams += "-config"
174+
$erlangParams += $allArgs[++$i]
175+
break
176+
}
177+
178+
"--vm-args" {
179+
$erlangParams += "-args_file"
180+
$erlangParams += $allArgs[++$i]
181+
break
182+
}
183+
184+
"--logger-otp-reports" {
185+
$private:tempVal = $allArgs[$i + 1]
186+
if ($tempVal -in @("true", "false")) {
187+
$erlangParams += @("-logger", "handle_otp_reports", $allArgs[++$i])
188+
}
189+
break
190+
}
191+
192+
"--logger-sasl-reports" {
193+
$private:tempVal = $allArgs[$i + 1]
194+
if ($tempVal -in @("true", "false")) {
195+
$erlangParams += @("-logger", "handle_sasl_reports", $allArgs[++$i])
196+
}
197+
break
198+
}
199+
200+
"--erl" {
201+
$private:erlFlags = $allArgs[++$i] -split " "
202+
$beforeExtras += $erlFlags
203+
break
204+
}
205+
206+
"+iex" {
207+
$elixirParams += "+iex"
208+
$useIex = $true
209+
210+
break
211+
}
212+
213+
"+elixirc" {
214+
$elixirParams += "+elixirc"
215+
break
216+
}
217+
218+
"--rpc-eval" {
219+
$private:key = $allArgs[++$i]
220+
$private:value = $allArgs[++$i]
221+
222+
if ($null -eq $key) {
223+
Write-Error "--rpc-eval: NODE must be present"
224+
exit 1
225+
}
226+
227+
if ($null -eq $value) {
228+
Write-Error "--rpc-eval: COMMAND for the '$key' node must be present"
229+
exit 1
230+
}
231+
232+
$elixirParams += "--rpc-eval"
233+
$elixirParams += $key
234+
$elixirParams += $value
235+
break
236+
}
237+
238+
"--boot-var" {
239+
$private:key = $allArgs[++$i]
240+
$private:value = $allArgs[++$i]
241+
242+
if ($null -eq $key) {
243+
Write-Error "--boot-var: VAR must be present"
244+
exit 1
245+
}
246+
247+
if ($null -eq $value) {
248+
Write-Error "--boot-var: Value for the '$key' var must be present"
249+
exit 1
250+
}
251+
252+
$elixirParams += "-boot_var"
253+
$elixirParams += $key
254+
$elixirParams += $value
255+
break
256+
}
257+
258+
Default {
259+
$private:normalized = NormalizeArg $arg
260+
$allOtherParams += $normalized
261+
break
262+
}
263+
}
264+
}
265+
266+
if ($null -eq $useIEx) {
267+
$beforeExtras = @("-s", "elixir", "start_cli") + $beforeExtras
268+
}
269+
270+
$beforeExtras = @("-pa", "$(Join-Path $scriptPath -ChildPath "../lib/elixir/ebin")") + $beforeExtras
271+
$beforeExtras = @("-noshell", "-elixir_root", "$(Join-Path $scriptPath -ChildPath "../lib")") + $beforeExtras
272+
273+
$allParams = @()
274+
275+
if ($null -ne $env:ELIXIR_ERL_OPTIONS) {
276+
$private:erlFlags = $env:ELIXIR_ERL_OPTIONS -split " "
277+
$allParams += $erlFlags
278+
}
279+
280+
$allParams += $erlangParams
281+
$allParams += $beforeExtras
282+
$allParams += "-extra"
283+
$allParams += $elixirParams
284+
$allParams += $allOtherParams
285+
286+
$binSuffix = ""
287+
288+
# The variable is available after PowerShell 7.2. Previous to that, PS only worked on Windows.
289+
if ($isWindows -or ($null -eq $isWindows)) {
290+
$binSuffix = ".exe"
291+
}
292+
293+
$binPath = "$erlExec$binSuffix"
294+
295+
# We double the double-quotes because they are going to be escaped by arguments parsing.
296+
$paramsPart = $allParams | ForEach-Object -Process { QuoteString($_ -replace "`"", "`"`"") }
297+
298+
if ($env:ELIXIR_CLI_DRY_RUN) {
299+
Write-Host "$binPath $paramsPart"
300+
}
301+
else {
302+
$output = Start-Process -FilePath $binPath -ArgumentList $paramsPart -NoNewWindow -Wait -PassThru
303+
exit $output.ExitCode
304+
}

bin/elixirc.ps1

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env pwsh
2+
3+
$scriptName = Split-Path -Leaf $PSCommandPath
4+
5+
if (($args.Count -eq 0) -or ($args[0] -in @("-h", "--help"))) {
6+
Write-Host @"
7+
Usage: $scriptName [elixir switches] [compiler switches] [.ex files]
8+
9+
-h, --help Prints this message and exits
10+
-o The directory to output compiled files
11+
-v, --version Prints Elixir version and exits (standalone)
12+
13+
--ignore-module-conflict Does not emit warnings if a module was previously defined
14+
--no-debug-info Does not attach debug info to compiled modules
15+
--no-docs Does not attach documentation to compiled modules
16+
--profile time Profile the time to compile modules
17+
--verbose Prints compilation status
18+
--warnings-as-errors Treats warnings as errors and returns non-zero exit status
19+
20+
** Options given after -- are passed down to the executed code
21+
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
22+
** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS
23+
"@
24+
exit
25+
}
26+
27+
$scriptPath = Split-Path -Parent $PSCommandPath
28+
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
29+
30+
$prependedArgs = @("+elixirc")
31+
32+
$allArgs = $prependedArgs + $args
33+
34+
# The dot is going to evaluate the script with the vars defined here.
35+
. $elixirMainScript

bin/iex.ps1

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/usr/bin/env pwsh
2+
3+
$scriptName = Split-Path -Leaf $PSCommandPath
4+
5+
if ($args[0] -in @("-h", "--help")) {
6+
Write-Host @"
7+
Usage: $scriptName [options] [.exs file] [data]
8+
9+
The following options are exclusive to IEx:
10+
11+
--dbg pry Sets the backend for Kernel.dbg/2 to IEx.pry/0
12+
--dot-iex "FILE" Evaluates FILE, line by line, to set up IEx' environment.
13+
Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
14+
If FILE is empty, then no file will be loaded.
15+
--remsh NAME Connects to a node using a remote shell.
16+
17+
It accepts all other options listed by "elixir --help".
18+
"@
19+
exit
20+
}
21+
22+
$scriptPath = Split-Path -Parent $PSCommandPath
23+
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"
24+
25+
$prependedArgs = @("--no-halt", "--erl", "-user elixir", "+iex")
26+
27+
$allArgs = $prependedArgs + $args
28+
29+
# The dot is going to evaluate the script with the vars defined here.
30+
. $elixirMainScript

0 commit comments

Comments
 (0)