#!/bin/sh # # To run locally without | sh: # # $ curl -fsS -o myapp_new.sh https://new.phoenixframework.org/myapp # $ sh myapp_new.sh # # Installs Elixir from Elixir's official isntall.sh script, then runs phx.new. # # See latest Elixir install.sh version at: # https://github.com/elixir-lang/elixir-lang.github.com/blob/main/install.sh set -eu echo_heading() { echo "\n\033[32m$1\033[0m" } main() { elixir_version='1.17.3' elixir_otp_release='27' otp_version='27.1.2' root_dir="$HOME/.elixir-install" # Install Elixir if needed if command -v elixir >/dev/null 2>&1; then echo_heading "Elixir is already installed ✓" else echo_heading "Installing Elixir..." if [ ! -d "$root_dir" ]; then mkdir -p "$root_dir" fi curl -fsSo "$root_dir/install.sh" "https://elixir-lang.org/install.sh" ( sh $root_dir/install.sh "elixir@$elixir_version" "otp@$otp_version" ) if [ $? -ne 0 ]; then echo "Failed to install elixir" exit 1 fi # Export the PATH so the current shell can find 'elixir' and 'mix' export PATH=$HOME/.elixir-install/installs/otp/$otp_version/bin:$PATH export PATH=$HOME/.elixir-install/installs/elixir/$elixir_version-otp-$elixir_otp_release/bin:$PATH fi # Use 'mix' to install 'phx_new' archive mix_cmd=$(command -v mix) if [ -z "$mix_cmd" ]; then echo "Error: mix command not found." exit 1 fi echo_heading "Installing phx_new archive..." mix archive.install hex phx_new --force app_name="myapp" app_args="$@" # Choose db adapter => postgres, mysql, sqlite db_option="" if command -v psql >/dev/null 2>&1; then echo_heading "Postgres is installed. Using Postgres for the Ecto adapter." # Do nothing, default is postgres elif command -v mysql >/dev/null 2>&1; then echo_heading "MySQL is installed. Using MySQL for the Ecto adapter." db_option="--database mysql" else echo_heading "Using SQLite for the Ecto adapter." db_option="--database sqlite3" fi echo_heading "Creating new Phoenix project '$app_name'..." mix phx.new "$app_name" $db_option $app_args --install --from-elixir-install echo_heading "$app_name created. Starting Phoenix server..." cd "$app_name" mix setup mix phx.server --open } main "$@"