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