Skip to content

Commit 86ab1c6

Browse files
committed
Export stdlib path as ENV variable
Utilize the `which` feature of the command asdf, mise or rtx to automatically detect the elixir installation path. Since there is no `which` like command in vfox, so if user pick vfox as version package manager, the ENV variable `ELX_STDLIB_PATH` won't be exported.
1 parent e741cbf commit 86ab1c6

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

scripts/launch.fish

+25-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,26 @@
88

99
# First order of business, see whether we can setup asdf
1010

11+
function readlink_f
12+
cd (dirname $argv[1]) || exit 1
13+
set filename (basename $argv[1])
14+
if test -L $filename
15+
readlink_f (readlink $filename)
16+
else
17+
echo (pwd -P)"/$filename"
18+
end
19+
end
20+
21+
function export_stdlib_path
22+
set -l current_dir (pwd)
23+
24+
set -l which_elixir_expr $argv[1]
25+
set -gx ELX_STDLIB_PATH (readlink_f (eval $which_elixir_expr) | string replace -r '(.*)\/bin\/elixir' '$1')
26+
# readlink_f changes the current directory (since fish doesn't have
27+
# subshells), so it needs to be restored.
28+
cd $current_dir
29+
end
30+
1131
echo "Looking for asdf install" >&2
1232

1333
# Check if we have the asdf binary for version >= 0.16.0
@@ -25,6 +45,7 @@ if test -n "$asdf"
2545
else
2646
echo "asdf executable found at $asdf. Using ASDF_DIR=$ASDF_DIR, ASDF_DATA_DIR=$ASDF_DATA_DIR." >&2
2747
end
48+
export_stdlib_path "asdf which elixir"
2849
else
2950
# Fallback to old method for asdf version <= 0.15.x
3051
test -n "$ASDF_DIR"; or set ASDF_DIR "$HOME/.asdf"
@@ -33,6 +54,7 @@ else
3354
echo "Legacy pre v0.16.0 asdf install found at $ASDF_SH, sourcing" >&2
3455
# Source the old asdf.sh script for versions <= 0.15.0
3556
source "$ASDF_SH"
57+
export_stdlib_path "asdf which elixir"
3658
else
3759
echo "asdf not found" >&2
3860
echo "Looking for mise executable" >&2
@@ -41,6 +63,7 @@ else
4163
if test -n "$mise"
4264
echo "mise executable found at $mise, activating" >&2
4365
source ( "$mise" env -s fish )
66+
export_stdlib_path "mise which elixir"
4467
else
4568
echo "mise not found" >&2
4669
echo "Looking for rtx executable" >&2
@@ -49,6 +72,7 @@ else
4972
if test -n "$rtx"
5073
echo "rtx executable found at $rtx, activating" >&2
5174
source ( "$rtx" env -s fish )
75+
export_stdlib_path "rtx which elixir"
5276
else
5377
echo "rtx not found" >&2
5478
echo "Looking for vfox executable" >&2
@@ -59,6 +83,7 @@ else
5983
source ( "$vfox" activate fish )
6084
else
6185
echo "vfox not found" >&2
86+
export_stdlib_path "which elixir"
6287
end
6388
end
6489
end
@@ -84,16 +109,6 @@ end
84109
# script so we can correctly configure the Erlang library path to
85110
# include the local .ez files, and then do what we were asked to do.
86111

87-
function readlink_f
88-
cd (dirname $argv[1]) || exit 1
89-
set filename (basename $argv[1])
90-
if test -L $filename
91-
readlink_f (readlink $filename)
92-
else
93-
echo (pwd -P)"/$filename"
94-
end
95-
end
96-
97112
if test -z "$ELS_INSTALL_PREFIX"
98113
set -l current_dir (pwd)
99114
set scriptpath (dirname (readlink_f (status -f)))

scripts/launch.sh

+23-10
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,24 @@ esac
3838
# First order of business, see whether we can setup asdf
3939
echo "Looking for asdf install" >&2
4040

41+
readlink_f () {
42+
cd "$(dirname "$1")" > /dev/null || exit 1
43+
filename="$(basename "$1")"
44+
if [ -h "$filename" ]; then
45+
readlink_f "$(readlink "$filename")"
46+
else
47+
echo "$(pwd -P)/$filename"
48+
fi
49+
}
50+
51+
export_stdlib_path () {
52+
which_elixir_expr=$1
53+
stdlib_path=$(eval "$which_elixir_expr")
54+
stdlib_real_path=$(readlink_f "$stdlib_path")
55+
ELX_STDLIB_PATH=$(echo "$stdlib_real_path" | sed "s/\(.*\)\/bin\/elixir/\1/")
56+
export ELX_STDLIB_PATH
57+
}
58+
4159
# Check if we have the asdf binary for version >= 0.16.0
4260
if command -v asdf >/dev/null 2>&1; then
4361
asdf_version=$(asdf --version 2>/dev/null)
@@ -58,6 +76,7 @@ if command -v asdf >/dev/null 2>&1; then
5876
else
5977
>&2 echo "asdf executable found at $(command -v asdf). Using ASDF_DIR=${ASDF_DIR}, ASDF_DATA_DIR=${ASDF_DATA_DIR}."
6078
fi
79+
export_stdlib_path "asdf which elixir"
6180
else
6281
# Fallback to old method for version <= 0.15.x
6382
ASDF_DIR=${ASDF_DIR:-"${HOME}/.asdf"}
@@ -66,6 +85,7 @@ else
6685
>&2 echo "Legacy pre v0.16.0 asdf install found at $ASDF_SH, sourcing"
6786
# Source the old asdf.sh script for versions <= 0.15.0
6887
. "$ASDF_SH"
88+
export_stdlib_path "asdf which elixir"
6989
else
7090
>&2 echo "asdf not found"
7191
>&2 echo "Looking for mise executable"
@@ -74,6 +94,7 @@ else
7494
if command -v mise >/dev/null 2>&1; then
7595
>&2 echo "mise executable found at $(command -v mise), activating"
7696
eval "$($(command -v mise) env -s "$preferred_shell")"
97+
export_stdlib_path "mise which elixir"
7798
else
7899
>&2 echo "mise not found"
79100
>&2 echo "Looking for rtx executable"
@@ -82,6 +103,7 @@ else
82103
if command -v rtx >/dev/null 2>&1; then
83104
>&2 echo "rtx executable found at $(command -v rtx), activating"
84105
eval "$($(command -v rtx) env -s "$preferred_shell")"
106+
export_stdlib_path "rtx which elixir"
85107
else
86108
>&2 echo "rtx not found"
87109
>&2 echo "Looking for vfox executable"
@@ -92,6 +114,7 @@ else
92114
eval "$($(command -v vfox) activate "$preferred_shell")"
93115
else
94116
>&2 echo "vfox not found"
117+
export_stdlib_path "which elixir"
95118
fi
96119
fi
97120
fi
@@ -115,16 +138,6 @@ fi
115138
# script so we can correctly configure the Erlang library path to
116139
# include the local .ez files, and then do what we were asked to do.
117140

118-
readlink_f () {
119-
cd "$(dirname "$1")" > /dev/null || exit 1
120-
filename="$(basename "$1")"
121-
if [ -h "$filename" ]; then
122-
readlink_f "$(readlink "$filename")"
123-
else
124-
echo "$(pwd -P)/$filename"
125-
fi
126-
}
127-
128141
if [ -z "${ELS_INSTALL_PREFIX}" ]; then
129142
SCRIPT=$(readlink_f "$0")
130143
SCRIPTPATH=$(dirname "$SCRIPT")

0 commit comments

Comments
 (0)