Skip to content

Commit 48c7f25

Browse files
author
Cees de Groot
authored
Rework wrapper scripts a bit (#190)
* Rework wrapper scripts a bit * Apply feedback from `shellcheck` * XDG all the things * Add some documentation * Remove debugging code
1 parent 4f835c7 commit 48c7f25

File tree

4 files changed

+84
-32
lines changed

4 files changed

+84
-32
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ Run `mix compile`, then `mix elixir_ls.release -o <release_dir>`. This builds th
132132

133133
If you're packaging these archives in an IDE plugin, make sure to build using the minimum supported OTP version for the best backwards-compatibility. Alternatively, you can use a [precompiled release](https://github.com/JakeBecker/elixir-ls/releases).
134134

135+
### Local setup
136+
137+
Because ElixirLS may get launched from an IDE that itself got launched from a graphical shell, the environment may not
138+
be complete enough to run or even find the correct Elixir/OTP version. The wrapper scripts try to configure `asdf-vm`
139+
if available, but that may not be what you want or need. Therefore, prior to executing Elixir, the script will source
140+
`~/.config/elixir_ls/setup.sh`, if available. The environment variable `ELS_MODE` is set to either `debugger` or
141+
`language_server` to help you decide what to do inside the script, if needed.
142+
135143
## Acknowledgements and related projects
136144

137145
ElixirLS isn't the first frontend-independent server for Elixir language support. The original was [Alchemist Server](https://github.com/tonini/alchemist-server/), which powers the [Alchemist](https://github.com/tonini/alchemist.el) plugin for Emacs. Another project, [Elixir Sense](https://github.com/msaraiva/elixir_sense), builds upon Alchemist and powers the [Elixir plugin for Atom](https://github.com/msaraiva/atom-elixir) as well as another VS Code plugin, [VSCode Elixir](https://github.com/fr1zle/vscode-elixir). ElixirLS uses Elixir Sense for several code insight features. Credit for those projects goes to their respective authors.

apps/elixir_ls_utils/priv/debugger.sh

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22
# Launches the debugger. This script must be in the same directory as the compiled .ez archives.
33

4-
[ -f "$HOME/.asdf/asdf.sh" ] && . "$HOME/.asdf/asdf.sh"
4+
dir=$(dirname "$0")
55

6-
readlink_f () {
7-
cd "$(dirname "$1")" > /dev/null
8-
filename="$(basename "$1")"
9-
if [ -h "$filename" ]; then
10-
readlink_f "$(readlink "$filename")"
11-
else
12-
echo "`pwd -P`/$filename"
13-
fi
14-
}
6+
export ELS_MODE=debugger
7+
export ELS_SCRIPT="ElixirLS.Debugger.CLI.main()"
158

16-
SCRIPT=$(readlink_f $0)
17-
SCRIPTPATH=`dirname $SCRIPT`
18-
export ERL_LIBS="$SCRIPTPATH:$ERL_LIBS"
19-
20-
elixir -e "ElixirLS.Debugger.CLI.main()"
9+
exec "${dir}/launch.sh"
Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
1-
#!/usr/bin/env bash
1+
#!/bin/sh
22
# Launches the language server. This script must be in the same directory as the compiled .ez archives.
33

4-
[ -f "$HOME/.asdf/asdf.sh" ] && . "$HOME/.asdf/asdf.sh"
4+
dir=$(dirname "$0")
55

6-
readlink_f () {
7-
cd "$(dirname "$1")" > /dev/null
8-
filename="$(basename "$1")"
9-
if [ -h "$filename" ]; then
10-
readlink_f "$(readlink "$filename")"
11-
else
12-
echo "`pwd -P`/$filename"
13-
fi
14-
}
6+
export ELS_MODE=language_server
7+
export ELS_SCRIPT="ElixirLS.LanguageServer.CLI.main()"
158

16-
SCRIPT=$(readlink_f $0)
17-
SCRIPTPATH=`dirname $SCRIPT`
18-
export ERL_LIBS="$SCRIPTPATH:$ERL_LIBS"
19-
20-
elixir -e "ElixirLS.LanguageServer.CLI.main()"
9+
exec "${dir}/launch.sh"

apps/elixir_ls_utils/priv/launch.sh

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#!/bin/sh
2+
# Actual launcher. This does the hard work of figuring out the best way
3+
# to launch the language server or the debugger.
4+
#
5+
6+
# Running this script is a one-time action per project launch, so we opt for
7+
# code simplicity instead of performance. Hence some potentially redundant
8+
# moves here.
9+
10+
# First order of business, see whether we can setup asdf-vm
11+
12+
did_relaunch=$1
13+
14+
asdf_vm="${HOME}/.asdf/asdf.sh"
15+
if test -f "${asdf_vm}"
16+
then
17+
# asdf-vm does not support the plain posix shell. Figure out
18+
# which one we need and relaunch ourselves with that.
19+
case "${did_relaunch}" in
20+
"")
21+
if which bash >/dev/null
22+
then
23+
exec "$(which bash)" "$0" relaunch
24+
elif which zsh >/dev/null
25+
then
26+
exec "$(which zsh)" "$0" relaunch
27+
fi
28+
;;
29+
*)
30+
# We have an arg2, so we got relaunched. Therefore, e're running in
31+
# a shell that can asdf-vm.
32+
. "${asdf_vm}"
33+
;;
34+
esac
35+
fi
36+
37+
# In case that people want to tweak the path, which Elixir to use, or
38+
# whatever prior to launching the language server or the debugger, we
39+
# give them the chance here. ELS_MODE will be set for
40+
# the really complex stuff. Use an XDG compliant path.
41+
42+
els_setup="${HOME}/.config/elixir_ls/setup.sh"
43+
if test -f "${els_setup}"
44+
then
45+
. "${els_setup}"
46+
fi
47+
48+
# Setup done. Make sure that we have the proper actual path to this
49+
# script so we can correctly configure the Erlang library path to
50+
# include the local .ez files, and then do what we were asked to do.
51+
52+
readlink_f () {
53+
cd "$(dirname "$1")" > /dev/null
54+
filename="$(basename "$1")"
55+
if [ -h "$filename" ]; then
56+
readlink_f "$(readlink "$filename")"
57+
else
58+
echo "$(pwd -P)/$filename"
59+
fi
60+
}
61+
62+
SCRIPT=$(readlink_f "$0")
63+
SCRIPTPATH=$(dirname "$SCRIPT")
64+
export ERL_LIBS="$SCRIPTPATH:$ERL_LIBS"
65+
66+
exec elixir -e "$ELS_SCRIPT"

0 commit comments

Comments
 (0)