Skip to content
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

Add ASDF_KERL_PATH env var to allow using local kerl via custom path #291

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,10 @@ Overriding the default kerl version shouldn't ever be necessary, but if you want
```shell
export ASDF_KERL_VERSION="2.1.1"
```

If you'd prefer to have `asdf-erlang` use an existing local version of `kerl`, you can set:

```shell
# Must point to the executable, not its parent directory
export ASDF_KERL_PATH="~/Git/kerl/kerl"
```
24 changes: 11 additions & 13 deletions bin/install
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Unoffical Bash "strict mode"
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
#ORIGINAL_IFS=$IFS
IFS=$'\t\n' # Stricter IFS settings

# shellcheck source=lib/utils.sh
source "$(dirname "$0")/../lib/utils.sh"
asdf_bin_dir=$(dirname "$0")
# shellcheck source=../lib/utils.sh
source "$asdf_bin_dir/../lib/utils.sh"

install_erlang() {
ensure_kerl_setup
Expand All @@ -17,21 +17,19 @@ install_erlang() {

export MAKEFLAGS="-j$ASDF_CONCURRENCY"

$(kerl_path) delete installation "$build_name" || true
$(kerl_path) delete build "$build_name" || true
"$KERL_PATH" delete installation "$build_name" || true
"$KERL_PATH" delete build "$build_name" || true

if [ "$ASDF_INSTALL_TYPE" = "ref" ]; then
$(kerl_path) build git "${OTP_GITHUB_URL:-https://github.com/erlang/otp.git}" "$ASDF_INSTALL_VERSION" "$build_name"
"$KERL_PATH" build git "${OTP_GITHUB_URL:-https://github.com/erlang/otp.git}" "$ASDF_INSTALL_VERSION" "$build_name"
else
$(kerl_path) build "$ASDF_INSTALL_VERSION" "$build_name"
"$KERL_PATH" build "$ASDF_INSTALL_VERSION" "$build_name"
fi

# We hide all output from this command so the
# "You can activate this installation running the following command:"
# that doesn't apply is hidden
$(kerl_path) install "$build_name" "$ASDF_INSTALL_PATH" >/dev/null 2>&1
$(kerl_path) cleanup "$ASDF_INSTALL_VERSION"

# We filter the output of the install command to hide irrelevant messages:
# "You can activate/leave this installation running the following command"
"$KERL_PATH" install 2>&1 | grep -v 'activate|leave'
"$KERL_PATH" cleanup "$ASDF_INSTALL_VERSION"
link_app_executables "$ASDF_INSTALL_PATH"
}

Expand Down
8 changes: 4 additions & 4 deletions bin/list-all
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
# Unoffical Bash "strict mode"
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
#ORIGINAL_IFS=$IFS
IFS=$'\t\n' # Stricter IFS settings

# shellcheck source=lib/utils.sh
source "$(dirname "$0")/../lib/utils.sh"
asdf_bin_dir=$(dirname "$0")
# shellcheck source=../lib/utils.sh
source "$asdf_bin_dir/../lib/utils.sh"

list_all() {
ensure_kerl_setup

"$(kerl_path)" list releases | sed -e 's:Run.*::' | tr '\n' ' '
"$KERL_PATH" list releases | sed -e 's:Run.*::' | tr '\n' ' '
}

list_all
12 changes: 6 additions & 6 deletions bin/uninstall
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
# Unoffical Bash "strict mode"
# http://redsymbol.net/articles/unofficial-bash-strict-mode/
set -euo pipefail
#ORIGINAL_IFS=$IFS
IFS=$'\t\n' # Stricter IFS settings

# shellcheck source=lib/utils.sh
source "$(dirname "$0")/../lib/utils.sh"
asdf_bin_dir=$(dirname "$0")
# shellcheck source=../lib/utils.sh
source "$asdf_bin_dir/../lib/utils.sh"

ensure_kerl_setup

Expand All @@ -17,9 +17,9 @@ if [ "$ASDF_INSTALL_VERSION" == "" ]; then
fi

# Remove the installation and build if the installation was done by kerl
if $(kerl_path) list installations | grep "$ASDF_INSTALL_PATH"; then
$(kerl_path) delete build "asdf_$ASDF_INSTALL_VERSION" || true
$(kerl_path) delete installation "$ASDF_INSTALL_PATH" || true
if "$KERL_PATH" list installations | grep "$ASDF_INSTALL_PATH"; then
"$KERL_PATH" delete build "asdf_$ASDF_INSTALL_VERSION" || true
"$KERL_PATH" delete installation "$ASDF_INSTALL_PATH" || true
fi

# Finally make sure the directory is actually removed regardless of what the
Expand Down
62 changes: 43 additions & 19 deletions lib/utils.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export KERL_VERSION="${ASDF_KERL_VERSION:-2.6.0}"
#!/usr/bin/env bash

handle_failure() {
function=$1
Expand All @@ -18,39 +18,63 @@ ensure_kerl_setup() {
handle_failure update_available_versions 'Failed to update available versions'
}

get_parent_dir() {
local asdf_script
local asdf_bin_dir
asdf_script=$(realpath "$0")
asdf_bin_dir=$(dirname "$asdf_script")

# A height of 0 represents the asdf script's directory
local height="${1}"

for ((i = 0; i < height; i++)); do
asdf_bin_dir=$(dirname "$asdf_bin_dir")
done

printf "%s\\n" "$asdf_bin_dir"
}

ensure_kerl_installed() {
if [ ! -f "$(kerl_path)" ]; then
if [ -z "$KERL_PATH" ]; then
export KERL_PATH
KERL_PATH="$(get_parent_dir 1)/kerl"
fi

local installed_kerl_version
installed_kerl_version=$($KERL_PATH version)

if [ ! -f "$KERL_PATH" ]; then
# Print to stderr so asdf doesn't assume this string is a list of versions
printf "%s\\n" "Kerl not found at expected path ($KERL_PATH), downloading version $KERL_VERSION...\n" >&2
download_kerl
elif [ "$("$(kerl_path)" version)" != "$KERL_VERSION" ]; then
# If the kerl file already exists and the version does not match, remove
# it and download the correct version
rm "$(kerl_path)"
elif [ "$($KERL_PATH version)" != "$KERL_VERSION" ]; then
printf "%s\\n" "Found Kerl at expected path ($KERL_PATH), but installed version ($installed_kerl_version) differs from requested version ($KERL_VERSION), redownloading...\n" >&2
# If the kerl file already exists and the version does not match, remove it and download the correct version
rm "$KERL_PATH"
download_kerl
else
printf "%s\\n" "Using installed kerl version ($installed_kerl_version) at path: $KERL_PATH\n" >&2
fi
}

download_kerl() {
# Print to stderr so asdf doesn't assume this string is a list of versions
printf "Downloading kerl...\\n" >&2
local kerl_url="https://raw.githubusercontent.com/kerl/kerl/$KERL_VERSION/kerl"

local kerl_url="https://raw.githubusercontent.com/kerl/kerl/${KERL_VERSION}/kerl"

curl -Lo "$(kerl_path)" "$kerl_url"
chmod +x "$(kerl_path)"
}

kerl_path() {
printf "%s\\n" "$(dirname "$(dirname "$0")")/kerl"
curl -Lo "$KERL_PATH" "$kerl_url"
chmod +x "$KERL_PATH"
}

set_kerl_env() {
export KERL_VERSION="${ASDF_KERL_VERSION:-2.6.0}"
export KERL_PATH="${ASDF_KERL_PATH:-}"
export KERL_DOWNLOAD_DIR="${ASDF_DOWNLOAD_PATH:-}"
export KERL_BUILD_BACKEND="git"

local kerl_home
kerl_home="$(dirname "$(dirname "$0")")/kerl-home"
kerl_home="$(get_parent_dir 1)/kerl-home"
mkdir -p "$kerl_home"
export KERL_BASE_DIR="$kerl_home"
export KERL_BUILD_BACKEND="git"
export KERL_CONFIG="$kerl_home/.kerlrc"
export KERL_DOWNLOAD_DIR="${ASDF_DOWNLOAD_PATH:-}"
}

update_available_versions() {
Expand Down