Skip to content

Add PowerShell scripts #13592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
ea7cd92
Add PowerShell script "bin/elixir.ps1"
philss Apr 29, 2024
7a5c0a1
Add powershell version of bin/elixirc
philss May 9, 2024
f15f107
Add powershell version of the bin/iex script
philss May 10, 2024
d028d47
Refactor bin/mix.ps1 to use the new bin/elixir.ps1
philss May 10, 2024
6ee3373
Apply formatter
philss May 10, 2024
fd40b20
Turn the scripts executables
philss May 13, 2024
7189a09
Make the bin/elixir.ps1 execute commands by default
philss May 13, 2024
9c6e461
Adaptations to fix the build
philss May 22, 2024
d0764be
Final adjustments to make the --pipe-to feature work
philss May 23, 2024
c96de96
Some fixes and suggestions from the code review
philss May 24, 2024
4909f12
Ignore standalone options for help in the loop
philss May 24, 2024
a46fbd6
Fix issues with escaping and arguments parsing
philss May 28, 2024
9efeaaf
Change PowerShell vars from PascalCase to camelCase
philss May 28, 2024
5c6289b
Use version 1.18-dev
philss May 28, 2024
c3e9a2f
Remove check about PS version
philss May 29, 2024
5b403e5
Fix processing of args for secundary PowerShell scripts
philss Jun 4, 2024
8dc4af3
Add tests covering both ".ps1" and ".bat" on Windows
philss Jun 4, 2024
9715b36
Fix check for Windows on PS 5.1
philss Jun 5, 2024
d44617a
Use the more common array declaration
philss Jun 5, 2024
655d6d6
Fix secundary scripts to accept args without quoting issues
philss Jun 6, 2024
335f64b
Format code
philss Jun 6, 2024
7f86c3d
Make explicit that the switch is for exact matches
philss Jun 6, 2024
b2181a4
Remove support for --pipe-to arg and ERTS_BIN env var
philss Jun 10, 2024
2a2e62d
Simplify CLI tests
philss Jun 10, 2024
89ef17d
Disable tests using ".ps1" scripts on Windows
philss Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
304 changes: 304 additions & 0 deletions bin/elixir.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
#!/usr/bin/env pwsh

$ELIXIR_VERSION = "1.18.0-dev"

$scriptPath = Split-Path -Parent $PSCommandPath
$erlExec = "erl"

# The iex.ps1, elixirc.ps1 and mix.ps1 scripts may populate this var.
if ($null -eq $allArgs) {
$allArgs = $args
}

function PrintElixirHelp {
$scriptName = Split-Path -Leaf $PSCommandPath
$help = @"
Usage: $scriptName [options] [.exs file] [data]

## General options

-e "COMMAND" Evaluates the given command (*)
-h, --help Prints this message (standalone)
-r "FILE" Requires the given files/patterns (*)
-S SCRIPT Finds and executes the given script in `$PATH
-pr "FILE" Requires the given files/patterns in parallel (*)
-pa "PATH" Prepends the given path to Erlang code path (*)
-pz "PATH" Appends the given path to Erlang code path (*)
-v, --version Prints Erlang/OTP and Elixir versions (standalone)

--erl "SWITCHES" Switches to be passed down to Erlang (*)
--eval "COMMAND" Evaluates the given command, same as -e (*)
--logger-otp-reports BOOL Enables or disables OTP reporting
--logger-sasl-reports BOOL Enables or disables SASL reporting
--no-halt Does not halt the Erlang VM after execution
--short-version Prints Elixir version (standalone)

Options given after the .exs file or -- are passed down to the executed code.
Options can be passed to the Erlang runtime using `$ELIXIR_ERL_OPTIONS or --erl.

## Distribution options

The following options are related to node distribution.

--cookie COOKIE Sets a cookie for this distributed node
--hidden Makes a hidden node
--name NAME Makes and assigns a name to the distributed node
--rpc-eval NODE "COMMAND" Evaluates the given command on the given remote node (*)
--sname NAME Makes and assigns a short name to the distributed node

--name and --sname may be set to undefined so one is automatically generated.

## Release options

The following options are generally used under releases.

--boot "FILE" Uses the given FILE.boot to start the system
--boot-var VAR "VALUE" Makes `$VAR available as VALUE to FILE.boot (*)
--erl-config "FILE" Loads configuration in FILE.config written in Erlang (*)
--vm-args "FILE" Passes the contents in file as arguments to the VM

--pipe-to is not supported via PowerShell.

** Options marked with (*) can be given more than once.
** Standalone options can't be combined with other options.
"@

Write-Host $help
}

if (($allArgs.Count -eq 1) -and ($allArgs[0] -eq "--short-version")) {
Write-Host "$ELIXIR_VERSION"
exit
}

if (($allArgs.Count -eq 0) -or (($allArgs.Count -eq 1) -and ($allArgs[0] -in @("-h", "--help")))) {
PrintElixirHelp
exit 1
}

function NormalizeArg {
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[string[]] $Items
)
$Items -join ","
}

function QuoteString {
param(
[Parameter(ValueFromPipeline = $true)]
[string] $Item
)

# We surround the string with double quotes, in order to preserve its contents as
# only one command arg.
# This is needed because PowerShell consider spaces as separator of arguments.
# The double quotes around will be removed when PowerShell process the argument.
# 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
if ($Item.Contains(" ")) {
'"' + $Item + '"'
}
else {
$Item
}
}

$elixirParams = @()
$erlangParams = @()
$beforeExtras = @()
$allOtherParams = @()

$runErlPipe = $null
$runErlLog = $null

for ($i = 0; $i -lt $allArgs.Count; $i++) {
$private:arg = $allArgs[$i]

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

$elixirParams += $arg
$elixirParams += $nextArg

break
}

{ $_ -in @("-v", "--version") } {
# Standalone options goes only once in the Elixir params, when they are empty.
if (($elixirParams.Count -eq 0) -and ($allOtherParams.Count -eq 0)) {
$elixirParams += $arg
}
else {
$allOtherParams += $arg
}
break
}

"--no-halt" {
$elixirParams += $arg
break
}

"--cookie" {
$erlangParams += "-setcookie"
$erlangParams += $allArgs[++$i]
break
}

"--hidden" {
$erlangParams += "-hidden"
break
}

"--name" {
$erlangParams += "-name"
$erlangParams += $allArgs[++$i]
break
}

"--sname" {
$erlangParams += "-sname"
$erlangParams += $allArgs[++$i]
break
}

"--boot" {
$erlangParams += "-boot"
$erlangParams += $allArgs[++$i]
break
}

"--erl-config" {
$erlangParams += "-config"
$erlangParams += $allArgs[++$i]
break
}

"--vm-args" {
$erlangParams += "-args_file"
$erlangParams += $allArgs[++$i]
break
}

"--logger-otp-reports" {
$private:tempVal = $allArgs[$i + 1]
if ($tempVal -in @("true", "false")) {
$erlangParams += @("-logger", "handle_otp_reports", $allArgs[++$i])
}
break
}

"--logger-sasl-reports" {
$private:tempVal = $allArgs[$i + 1]
if ($tempVal -in @("true", "false")) {
$erlangParams += @("-logger", "handle_sasl_reports", $allArgs[++$i])
}
break
}

"--erl" {
$private:erlFlags = $allArgs[++$i] -split " "
$beforeExtras += $erlFlags
break
}

"+iex" {
$elixirParams += "+iex"
$useIex = $true

break
}

"+elixirc" {
$elixirParams += "+elixirc"
break
}

"--rpc-eval" {
$private:key = $allArgs[++$i]
$private:value = $allArgs[++$i]

if ($null -eq $key) {
Write-Error "--rpc-eval: NODE must be present"
exit 1
}

if ($null -eq $value) {
Write-Error "--rpc-eval: COMMAND for the '$key' node must be present"
exit 1
}

$elixirParams += "--rpc-eval"
$elixirParams += $key
$elixirParams += $value
break
}

"--boot-var" {
$private:key = $allArgs[++$i]
$private:value = $allArgs[++$i]

if ($null -eq $key) {
Write-Error "--boot-var: VAR must be present"
exit 1
}

if ($null -eq $value) {
Write-Error "--boot-var: Value for the '$key' var must be present"
exit 1
}

$elixirParams += "-boot_var"
$elixirParams += $key
$elixirParams += $value
break
}

Default {
$private:normalized = NormalizeArg $arg
$allOtherParams += $normalized
break
}
}
}

if ($null -eq $useIEx) {
$beforeExtras = @("-s", "elixir", "start_cli") + $beforeExtras
}

$beforeExtras = @("-pa", "$(Join-Path $scriptPath -ChildPath "../lib/elixir/ebin")") + $beforeExtras
$beforeExtras = @("-noshell", "-elixir_root", "$(Join-Path $scriptPath -ChildPath "../lib")") + $beforeExtras

$allParams = @()

if ($null -ne $env:ELIXIR_ERL_OPTIONS) {
$private:erlFlags = $env:ELIXIR_ERL_OPTIONS -split " "
$allParams += $erlFlags
}

$allParams += $erlangParams
$allParams += $beforeExtras
$allParams += "-extra"
$allParams += $elixirParams
$allParams += $allOtherParams

$binSuffix = ""

# The variable is available after PowerShell 7.2. Previous to that, PS only worked on Windows.
if ($isWindows -or ($null -eq $isWindows)) {
$binSuffix = ".exe"
}

$binPath = "$erlExec$binSuffix"

# We double the double-quotes because they are going to be escaped by arguments parsing.
$paramsPart = $allParams | ForEach-Object -Process { QuoteString($_ -replace "`"", "`"`"") }

if ($env:ELIXIR_CLI_DRY_RUN) {
Write-Host "$binPath $paramsPart"
}
else {
$output = Start-Process -FilePath $binPath -ArgumentList $paramsPart -NoNewWindow -Wait -PassThru
exit $output.ExitCode
}
35 changes: 35 additions & 0 deletions bin/elixirc.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env pwsh

$scriptName = Split-Path -Leaf $PSCommandPath

if (($args.Count -eq 0) -or ($args[0] -in @("-h", "--help"))) {
Write-Host @"
Usage: $scriptName [elixir switches] [compiler switches] [.ex files]

-h, --help Prints this message and exits
-o The directory to output compiled files
-v, --version Prints Elixir version and exits (standalone)

--ignore-module-conflict Does not emit warnings if a module was previously defined
--no-debug-info Does not attach debug info to compiled modules
--no-docs Does not attach documentation to compiled modules
--profile time Profile the time to compile modules
--verbose Prints compilation status
--warnings-as-errors Treats warnings as errors and returns non-zero exit status

** Options given after -- are passed down to the executed code
** Options can be passed to the Erlang runtime using ELIXIR_ERL_OPTIONS
** Options can be passed to the Erlang compiler using ERL_COMPILER_OPTIONS
"@
exit
}

$scriptPath = Split-Path -Parent $PSCommandPath
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"

$prependedArgs = @("+elixirc")

$allArgs = $prependedArgs + $args

# The dot is going to evaluate the script with the vars defined here.
. $elixirMainScript
30 changes: 30 additions & 0 deletions bin/iex.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env pwsh

$scriptName = Split-Path -Leaf $PSCommandPath

if ($args[0] -in @("-h", "--help")) {
Write-Host @"
Usage: $scriptName [options] [.exs file] [data]

The following options are exclusive to IEx:

--dbg pry Sets the backend for Kernel.dbg/2 to IEx.pry/0
--dot-iex "FILE" Evaluates FILE, line by line, to set up IEx' environment.
Defaults to evaluating .iex.exs or ~/.iex.exs, if any exists.
If FILE is empty, then no file will be loaded.
--remsh NAME Connects to a node using a remote shell.

It accepts all other options listed by "elixir --help".
"@
exit
}

$scriptPath = Split-Path -Parent $PSCommandPath
$elixirMainScript = Join-Path -Path $scriptPath -ChildPath "elixir.ps1"

$prependedArgs = @("--no-halt", "--erl", "-user elixir", "+iex")

$allArgs = $prependedArgs + $args

# The dot is going to evaluate the script with the vars defined here.
. $elixirMainScript
Loading