-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathzsh-asdf-direnv.plugin.zsh
executable file
·99 lines (77 loc) · 2.91 KB
/
zsh-asdf-direnv.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env zsh
# zsh plugin to load asdf & asdf-direnv
# set asdf install directory if not already specified
[[ -z "$ASDF_DIR" ]] && export ASDF_DIR="$HOME/.asdf"
# simple logging function
_zsh_asdf_direnv_log() {
local weight=$1
local colour=$2
local msg=$3
local format_start="%F{$colour}"
local format_end="%f"
if [ $weight = "bold" ]; then
format_start="%B$format_start"
format_end="$format_end%b"
fi
# print -P for proper formatting of %F & %B
print -P "$format_start\[zsh-asdf-direnv] $msg$format_end"
}
# install direnv
_zsh_asdf_direnv_install_direnv() {
_zsh_asdf_direnv_log "bold" "blue" "Installing direnv..."
# make sure asdf is loaded
source "$ASDF_DIR/asdf.sh"
# if the direnv asdf plugin isn't installed, add it
if ! asdf plugin list 2>/dev/null | grep direnv >/dev/null; then
_zsh_asdf_direnv_log "none" "blue" "-> Adding plugin..."
asdf plugin add direnv
fi
# if there isn't a version of direnv installed, install the latest
if test ! "$(asdf list direnv 2>/dev/null | wc -l)" -gt 0; then
_zsh_asdf_direnv_log "none" "blue" "-> Installing latest direnv..."
asdf install direnv latest
asdf global direnv latest
fi
_zsh_asdf_direnv_log "bold" "green" "-> direnv install OK"
}
# install asdf
_zsh_asdf_direnv_install_asdf() {
_zsh_asdf_direnv_log "none" "blue" "#############################################"
_zsh_asdf_direnv_log "bold" "blue" "Installing asdf..."
_zsh_asdf_direnv_log "none" "blue" "-> cloning asdf into home dir: $ASDF_DIR"
# if asdf isn't already installed, clone the repo
if test -d "$ASDF_DIR"; then
_zsh_asdf_direnv_log "bold" "blue" "-> asdf home dir already exists, skipping..."
else
# clone the repo to the home dir
git clone https://github.com/asdf-vm/asdf.git "$ASDF_DIR"
# checkout the latest release
git -C "$ASDF_DIR" checkout -q "$(git -C $ASDF_DIR describe --abbrev=0 --tags)"
# show the status of the install
if test $? -ne 0; then
_zsh_asdf_direnv_log "bold" "red" "-> asdf install failed."
else
_zsh_asdf_direnv_log "bold" "green" "-> asdf install OK"
fi
fi
_zsh_asdf_direnv_log "none" "blue" "#############################################"
}
# source asdf and run the direnv hook, after adding completions to path
_zsh_asdf_direnv_load() {
# if specified only load the asdf wrapper, leaving the shims out of path
if test "$ZSH_ASDF_DIRENV_LIBONLY" = "true"; then
PATH="$PATH:$ASDF_DIR/bin"
source "$ASDF_DIR/lib/asdf.sh"
else
source "$ASDF_DIR/asdf.sh"
fi
fpath+="$ASDF_DIR/completions"
eval "$(asdf exec direnv hook zsh)"
}
# if asdf isn't installed, install it
[[ ! -d "$ASDF_DIR" ]] && _zsh_asdf_direnv_install_asdf
local asdf_direnv_dir
asdf_direnv_dir="${ASDF_DATA_DIR:-"$ASDF_DIR/installs/direnv"}"
[[ ! -d "$asdf_direnv_dir" ]] && _zsh_asdf_direnv_install_direnv
# if asdf is installed, load it
[[ -d "$ASDF_DIR" ]] && _zsh_asdf_direnv_load