]> Sergey Matveev's repositories - zsh-autoenv.git/blob - autoenv.zsh
Fix _autoenv_get_file_upwards for older Zsh
[zsh-autoenv.git] / autoenv.zsh
1 # Initially based on
2 # https://github.com/joshuaclayton/dotfiles/blob/master/zsh_profile.d/autoenv.zsh
3
4 export AUTOENV_ENV_FILENAME=$HOME/.env_auth
5
6 # Name of file to look for when entering directories.
7 : ${AUTOENV_FILE_ENTER:=.env}
8
9 # Name of file to look for when leaving directories.
10 # Requires AUTOENV_HANDLE_LEAVE=1.
11 : ${AUTOENV_FILE_LEAVE:=.env.leave}
12
13 # Look for .env in parent dirs?
14 : ${AUTOENV_LOOK_UPWARDS:=1}
15
16 # Handle leave events when changing away from a subtree, where an "enter"
17 # event was handled?
18 : ${AUTOENV_HANDLE_LEAVE:=1}
19
20
21 # Public helper functions, which can be used from your .env files:
22 #
23 # Source the next .env file from parent directories.
24 # This is useful if you want to use a base .env file for a directory subtree.
25 autoenv_source_parent() {
26   local parent_env_file=$(_autoenv_get_file_upwards $PWD)
27
28   if [[ -n $parent_env_file ]] \
29     && _autoenv_check_authorized_env_file $parent_env_file; then
30
31     local parent_env_dir=${parent_env_file:A:h}
32     _autoenv_source $parent_env_file enter $parent_env_dir
33   fi
34 }
35
36
37 # Internal: stack of entered (and handled) directories. {{{
38 _autoenv_stack_entered=()
39 typeset -A _autoenv_stack_entered_mtime
40 _autoenv_stack_entered_mtime=()
41
42 # Add an entry to the stack, and remember its mtime.
43 _autoenv_stack_entered_add() {
44   local env_file=$1
45
46   # Remove any existing entry.
47   _autoenv_stack_entered[$_autoenv_stack_entered[(i)$1]]=()
48
49   # Append it to the stack, and remember its mtime.
50   _autoenv_stack_entered+=($env_file)
51   _autoenv_stack_entered_mtime[$env_file]=$(_autoenv_get_file_mtime $env_file)
52 }
53
54 _autoenv_get_file_mtime() {
55   if [[ -f $1 ]]; then
56     zstat +mtime $1
57   else
58     echo 0
59   fi
60 }
61
62 # Remove an entry from the stack.
63 _autoenv_stack_entered_remove() {
64   local env_file=$1
65   _autoenv_stack_entered=(${_autoenv_stack_entered#$env_file})
66   _autoenv_stack_entered_mtime[$env_file]=
67 }
68
69 # Is the given entry already in the stack?
70 _autoenv_stack_entered_contains() {
71   local env_file=$1
72   if (( ${+_autoenv_stack_entered[(r)${env_file}]} )); then
73     # Entry is in stack.
74     if [[ $_autoenv_stack_entered_mtime[$env_file] == $(_autoenv_get_file_mtime $env_file) ]]; then
75       # Entry has the expected mtime.
76       return
77     fi
78   fi
79   return 1
80 }
81 # }}}
82
83 # Load zstat module, but only its builtin `zstat`.
84 zmodload -F zsh/stat b:zstat
85
86
87 _autoenv_hash_pair() {
88   local env_file=${1:A}
89   if (( $+2 )); then
90     env_shasum=$2
91   else
92     env_shasum=$(shasum $env_file | cut -d' ' -f1)
93   fi
94   echo "$env_file:$env_shasum:1"
95 }
96
97 _autoenv_authorized_env_file() {
98   local env_file=$1
99   local pair=$(_autoenv_hash_pair $env_file)
100   test -f $AUTOENV_ENV_FILENAME \
101     && \grep -qF $pair $AUTOENV_ENV_FILENAME
102 }
103
104 _autoenv_authorize() {
105   local env_file=$1
106   _autoenv_deauthorize $env_file
107   _autoenv_hash_pair $env_file >> $AUTOENV_ENV_FILENAME
108 }
109
110 _autoenv_deauthorize() {
111   local env_file=$1
112   if [[ -f $AUTOENV_ENV_FILENAME ]]; then
113     echo $(\grep -vF $env_file $AUTOENV_ENV_FILENAME) > $AUTOENV_ENV_FILENAME
114   fi
115 }
116
117 # This function can be mocked in tests
118 _autoenv_read_answer() {
119   local answer
120   read $=_AUTOENV_TEST_READ_ARGS -q answer
121   echo $answer
122 }
123
124 # Args: 1: absolute path to env file (resolved symlinks).
125 _autoenv_check_authorized_env_file() {
126   if ! [[ -f $1 ]]; then
127     return 1
128   fi
129   if ! _autoenv_authorized_env_file $1; then
130     echo "Attempting to load unauthorized env file: $1"
131     echo ""
132     echo "**********************************************"
133     echo ""
134     cat $1
135     echo ""
136     echo "**********************************************"
137     echo ""
138     echo -n "Would you like to authorize it? [y/N] "
139
140     local answer=$(_autoenv_read_answer)
141     echo
142     if [[ $answer != 'y' ]]; then
143       return 1
144     fi
145
146     _autoenv_authorize $1
147   fi
148   return 0
149 }
150
151 # Get directory of this file (absolute, with resolved symlinks).
152 _autoenv_this_dir=${0:A:h}
153
154 _autoenv_source() {
155   local env_file=$1
156   _autoenv_event=$2
157   local _autoenv_envfile_dir=$3
158
159   _autoenv_from_dir=$_autoenv_chpwd_prev_dir
160   _autoenv_to_dir=$PWD
161
162   # Source varstash library once.
163   if [[ -z "$functions[(I)autostash]" ]]; then
164     source $_autoenv_this_dir/lib/varstash
165     # NOTE: Varstash uses $PWD as default for varstash_dir, we might set it to
166     # ${env_file:h}.
167   fi
168
169   # Change to directory of env file, source it and cd back.
170   local new_dir=$PWD
171   builtin cd -q $_autoenv_envfile_dir
172   source $env_file
173   builtin cd -q $new_dir
174
175   # Unset vars set for enter/leave scripts.
176   # This should not get done for recursion (via autoenv_source_parent),
177   # and can be useful to have in general after autoenv was used.
178   # unset _autoenv_event _autoenv_from_dir _autoenv_to_dir
179 }
180
181 _autoenv_get_file_upwards() {
182   local look_from=${1:-$PWD}
183   local look_for=${2:-$AUTOENV_FILE_ENTER}
184
185   # Manually look in parent dirs. An extended Zsh glob should use Y1 for
186   # performance reasons, which is only available in zsh-5.0.5-146-g9381bb6.
187   local last
188   local parent_dir="$look_from/.."
189   while true; do
190     parent_dir=${parent_dir:A}
191     if [[ $parent_dir == $last ]]; then
192       break
193     fi
194     parent_file="${parent_dir}/${look_for}"
195
196     if [[ -f $parent_file ]]; then
197       echo $parent_file
198       break
199     fi
200
201     last=$parent_dir
202     parent_dir="${parent_dir}/.."
203   done
204 }
205
206
207 _autoenv_chpwd_prev_dir=$PWD
208 _autoenv_chpwd_handler() {
209   local env_file="$PWD/$AUTOENV_FILE_ENTER"
210
211   # Handle leave event for previously sourced env files.
212   if [[ $AUTOENV_HANDLE_LEAVE == 1 ]] && (( $#_autoenv_stack_entered )); then
213     local prev_file prev_dir
214     for prev_file in ${_autoenv_stack_entered}; do
215       prev_dir=${prev_file:A:h}
216       if ! [[ ${PWD}/ == ${prev_dir}/* ]]; then
217         local env_file_leave=$prev_dir/$AUTOENV_FILE_LEAVE
218         if _autoenv_check_authorized_env_file $env_file_leave; then
219           _autoenv_source $env_file_leave leave $prev_dir
220         fi
221         _autoenv_stack_entered_remove $prev_dir
222       fi
223     done
224   fi
225
226   if ! [[ -f $env_file ]] && [[ $AUTOENV_LOOK_UPWARDS == 1 ]]; then
227     env_file=$(_autoenv_get_file_upwards $PWD)
228     if [[ -z $env_file ]]; then
229       _autoenv_chpwd_prev_dir=$PWD
230       return
231     fi
232   fi
233
234   # Load the env file only once: check if $env_file is in the stack of entered
235   # directories.
236   if _autoenv_stack_entered_contains $env_file; then
237     _autoenv_chpwd_prev_dir=$PWD
238     return
239   fi
240
241   if ! _autoenv_check_authorized_env_file $env_file; then
242     _autoenv_chpwd_prev_dir=$PWD
243     return
244   fi
245
246   _autoenv_stack_entered_add $env_file
247
248   # Source the enter env file.
249   _autoenv_source $env_file enter $PWD
250
251   _autoenv_chpwd_prev_dir=$PWD
252 }
253
254 autoload -U add-zsh-hook
255 add-zsh-hook chpwd _autoenv_chpwd_handler
256
257 # Look in current directory already.
258 _autoenv_chpwd_handler