I ported elixir.bat and mix.bat to PowerShell

I was really annoyed by the Terminate batch job (Y/N)? prompt after hitting CTRL-C, that is caused due to elixir.bat and mix.bat being legacy batch scripts. Batch scripts do that, but it’s unintuitive.

I ported both to PowerShell scripts and the problem went away. I’m sharing this for anyone who is annoyed in the same way.

Just in case this may be interesting to package into elixir by default, I’d be happy to clean this up a little and work with someone who uses both commands more often to make it stable. It works for me, but I only use mix phx.new and mix phx.server, so I’m unable to test other arguments at the moment.

elixir.ps1

$elixirVersion = "1.14.0"

if (!$args -or $args[0] -cin ("--help", "-h", "/h", "/?") ) {
    @'
Usage: elixir.ps1 [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 $env: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)

  --app APP                    Starts the given app and its dependencies (*)
  --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)
  --werl                       Uses Erlang's Windows shell GUI (Windows only)

Options given after the .exs file or -- are passed down to the executed code.
Options can be passed to the Erlang runtime using $env: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

## 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 on Windows. If set, Elixir won't boot.

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

if ($args[0] -eq "--short-version") {
    $ElixirVersion
    exit
}

# Arguments for Elixir
$elixirArgs = @()

# Arguments for Erlang
$erlangArgs = @()

# Optional arguments before the "-extra" parameter
$beforeExtra = @()

# Designates which mode / Elixir component to run as
$runMode = "elixir"

:argparseloop for ($i = 0; $i -lt $args.Count; $i++) {
    switch ($args[$i]) {
        # Execution Options
        "--werl" {
            $useWerl = $true
            break
        }
        "+iex" {
            $elixirArgs += "+iex"
            $runMode = "iex"
            break
        }
        "+elixirc" {
            $elixirArgs += "+elixirc"
            $runMode = "elixirc"
            break
        }
        # Elixir Arguments
        { $_ -in "-e", "--eval", "-r", "-pr", "-pa", "-pz", "-app", "-remsh", "-dot-iex" } {
            $elixirArgs += "{0} {1}" -f $args[$i], $args[++$i]
            break
        }
        "--rpc-eval" {
            $elixirArgs += "--rpc-eval {0} {1}" -f $args[++$i], $args[++$i]
            break
        }
        # Erlang Arguments
        "--boot" {
            $erlangArgs += "-boot {0}" -f $args[++$i]
            break
        }
        "--name" {
            $erlangArgs += "-name {0}" -f $args[++$i]
            break
        }
        "--sname" {
            $erlangArgs += "-sname {0}" -f $args[++$i]
            break
        }
        "--erl-config" {
            $erlangArgs += "-config {0}" -f $args[++$i]
            break
        }
        "--cookie" {
            $erlangArgs += "-setcookie {0}" -f $args[++$i]
            break
        }
        "--vm-args" {
            $erlangArgs += "-args_file {0}" -f $args[++$i]
            break
        }
        "--boot-var" {
            $erlangArgs += ("-boot_var {0} {1}" -f $args[++$i], $args[++$i])
            break
        }
        "--hidden" {
            $erlangArgs += "-hidden"
            break
        }
        "--detached" {
            $erlangArgs += "-detached"
            Write-Warning "The --detached option is deprecated!"
            break
        }
        "--logger-otp-reports" {
            $erlangArgs += "-logger handle_otp_reports {0}" -f $args[++$i]
            break
        }
        "--logger-sasl-reports" {
            $erlangArgs += "-logger handle_sasl_reports {0}" -f $args[++$i]
            break
        }
        "--erl" {
            $beforeExtra += "{0}" -f $args[++$i]
            break
        }
        "--pipe-to" {
            Write-Warning "Option --pipe-to is not supported on Windows!"
            break
        }
        # [.exs file] and [data]
        Default {
            $elixirArgs += $args[$i..$args.GetUpperBound(0)]
            break argparseloop
        }
    }
}

$extraPaths = @("-pa") + (Get-Item "$PSScriptRoot\..\lib\*\ebin").FullName

if ($useWerl) {
    $erlExecutable = "werl.exe"
} else {
    $erlExecutable = "erl.exe"
}

if ($env:ERTS_BIN) {
    $erlExecutable = Join-Path $env:ERTS_BIN $erlExecutable
}

# Enable colors if console host is Windows Terminal
if ($env:WT_PROFILE_ID -or $env:WT_SESSION) {
    $beforeExtra = "-elixir ansi_enabled true".split() + $beforeExtra
}

if ($runMode -ne "iex") {
    $beforeExtra = "-noshell -s elixir start_cli".split() + $beforeExtra
}

& $erlExecutable $extraPaths $env:ELIXIR_ERL_OPTIONS $erlangArgs $beforeExtra "-extra" $elixirArgs

mix.ps1

& $PSScriptRoot\elixir.ps1 $PSScriptRoot\mix @args
10 Likes