-
Notifications
You must be signed in to change notification settings - Fork 6
/
zsh-direnv.plugin.zsh
executable file
·125 lines (107 loc) · 3.49 KB
/
zsh-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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env zsh
#####################
# COMMONS
#####################
autoload colors is-at-least
#########################
# CONSTANT
#########################
BOLD="bold"
NONE="NONE"
#########################
# PLUGIN MAIN
#########################
[[ -z "$DIRENV_HOME" ]] && export DIRENV_HOME="$HOME/.direnv"
ZSH_DIRENV_VERSION_FILE=${DIRENV_HOME}/version.txt
#########################
# Functions
#########################
_zsh_direnv_log() {
local font=$1
local color=$2
local msg=$3
if [ "$font" = $BOLD ]
then
echo $fg_bold[$color] "[zsh-direnv-plugin] $msg" $reset_color
else
echo $fg[$color] "[zsh-direnv-plugin] $msg" $reset_color
fi
}
_zsh_direnv_last_version() {
echo $(curl -s https://api.github.com/repos/direnv/direnv/releases/latest | grep tag_name | cut -d '"' -f 4)
}
_zsh_direnv_download_install() {
local version=$1
local machine
case "$(uname -m)" in
x86_64)
machine=amd64
;;
arm64)
machine=arm64
;;
aarch64)
machine=arm64
;;
i686 | i386)
machine=386
;;
*)
_zsh_direnv_log $BOLD "red" "Machine $(uname -m) not supported by this plugin"
return 1
;;
esac
# if on Darwin, trim $OSTYPE to match the direnv release
[[ "$OSTYPE" == "darwin"* ]] && local OSTYPE=darwin
_zsh_direnv_log $NONE "blue" " -> download and install direnv ${version}"
curl -o "${DIRENV_HOME}/direnv" -fsSL https://github.com/direnv/direnv/releases/download/${version}/direnv.${OSTYPE%-*}-${machine}
chmod +x "${DIRENV_HOME}/direnv"
echo ${version} > ${ZSH_DIRENV_VERSION_FILE}
}
_zsh_direnv_install() {
_zsh_direnv_log $NONE "blue" "#############################################"
_zsh_direnv_log $BOLD "blue" "Installing direnv..."
_zsh_direnv_log $NONE "blue" "-> creating direnv home dir : ${DIRENV_HOME}"
mkdir -p ${DIRENV_HOME} || _zsh_direnv_log $NONE "green" "dir already exist"
local last_version=$(_zsh_direnv_last_version)
_zsh_direnv_log $NONE "blue" "-> retrieve last version of direnv..."
_zsh_direnv_download_install ${last_version}
if [ $? -ne 0 ]
then
_zsh_direnv_log $BOLD "red" "Install KO"
else
_zsh_direnv_log $BOLD "green" "Install OK"
fi
_zsh_direnv_log $NONE "blue" "#############################################"
}
update_zsh_direnv() {
_zsh_direnv_log $NONE "blue" "#############################################"
_zsh_direnv_log $BOLD "blue" "Checking new version of direnv..."
local current_version=$(cat ${ZSH_DIRENV_VERSION_FILE})
local last_version=$(_zsh_direnv_last_version)
if is-at-least ${last_version#v*} ${current_version#v*}
then
_zsh_direnv_log $BOLD "green" "Already up to date, current version : ${current_version}"
else
_zsh_direnv_log $NONE "blue" "-> Updating direnv..."
_zsh_direnv_download_install ${last_version}
_zsh_direnv_log $BOLD "green" "Update OK"
fi
_zsh_direnv_log $NONE "blue" "#############################################"
}
_zsh_direnv_load() {
# export PATH if needed
local -r plugin_dir=${DIRENV_HOME}
# Add the plugin bin directory path if it doesn't exist in $PATH.
if [[ -z ${path[(r)$plugin_dir]} ]]; then
path+=($plugin_dir)
fi
eval "$(direnv hook zsh)"
}
# install direnv if it isnt already installed
[[ ! -f "${ZSH_DIRENV_VERSION_FILE}" ]] && _zsh_direnv_install
# load direnv if it is installed
if [[ -f "${ZSH_DIRENV_VERSION_FILE}" ]]; then
_zsh_direnv_load
fi
unset -f _zsh_direnv_install _zsh_direnv_load