-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathworkit.sh
258 lines (218 loc) · 6.39 KB
/
workit.sh
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# Functions to load working code projects
#
# heavily influenced/copied from Doug Hellmanns, virtualenvwrapper
# http://bitbucket.org/dhellmann/virtualenvwrapper/overview/
#
# set -x
# TODO
# virtualenv sets the VIRTUAL_ENV system variable, need to replicate a bit
# source the helper script functions
# assuming its in the same dir as this file
BASEDIR=`dirname $0`
source $BASEDIR/process_functions.sh
# You can override this setting in your .zshrc
if [ "$WORKIT_HOME" = "" ]
then
WORKIT_HOME=( "$HOME/src" )
export WORKIT_HOME
fi
# Normalize the directory name in case it includes
# relative path components.
# this broke hard for some reason so forget normalizing for now.
# it's probably some sort of subshell thing again, but
# for now just leave it be and look it up later
# for ((i=1;i<=${#WORKIT_HOME};i++)); do
# rpath=$WORKIT_HOME[$i]
# echo $rpath
# WORKIT_HOME[$i]=$(/bin/zsh -c 'cd "$rpath"; pwd')
# done
# export WORKIT_HOME
### Functions
# Verify that the WORKON_HOME directory exists
function verify_workit_home () {
for zpath in $WORKIT_HOME; do
if [ ! -d "$zpath" ]
then
echo "ERROR: projects directory '$zpath' does not exist." >&2
return 1
fi
done
return 0
}
# Verify that the requested project exists
function verify_workit_project () {
typeset env_name="$1"
proj_count=0
proj_list=()
for zpath in $WORKIT_HOME; do
target_path="$zpath/$env_name"
if [[ -d $target_path ]]; then
proj_list+=("$target_path")
((proj_count+=1))
fi
done
if [[ $proj_count -eq 1 ]]; then
echo "$proj_list[1]"
return 0
else
select item in $proj_list
do
case "$item" in
*)
echo "$item"
break
;;
esac
done
return 0
fi
}
# Verify that the active project exists
function verify_active_project () {
if [ ! -n "${PROJECT_DIR}" ] || [ ! -d "${PROJECT_DIR}" ]
then
echo "ERROR: no project active, or active project is missing" >&2
return 1
fi
return 0
}
# source the pre/post hooks
function workit_source_hook () {
scriptname="$1"
if [ -f "$scriptname" ]
then
source "$scriptname"
fi
}
# run the pre/post hooks
function workit_run_hook () {
scriptname="$1"
shift
if [ -x "$scriptname" ]
then
"$scriptname" "$@"
fi
}
# Create a new project, in the WORKIT_HOME.
#
# Usage: mkworkit [options] PROJNAME
function mkworkit () {
verify_workit_home || return 1
if [[ "$1" == "" ]]; then
echo "\nUsage: mkworkit [project_name]\n"
return 1
fi
workit_home_count=${#WORKIT_HOME}
if [ $workit_home_count -gt 1 ]
then
select proj_path in $WORKIT_HOME
do
case "$proj_path" in
*)
break
;;
esac
done
else
proj_path=$WORKIT_HOME
fi
eval "projname=\$$#"
proj_workit_path="$proj_path/$projname/"
# test for existing proj dir, if not create it, otherwise add
# the post* script files to the existing dir
if [ ! -d $proj_workit_path ]
then
(cd "$proj_path" &&
mkdir -p "$proj_workit_path"
)
else
(cd "$proj_workit_path")
fi
touch "$proj_workit_path/postactivate" &&
touch "$proj_workit_path/postdeactivate" &&
chmod +x "$proj_workit_path/postactivate" "$proj_workit_path/postdeactivate"
# If they passed a help option or got an error from virtualenv,
# the environment won't exist. Use that to tell whether
# we should switch to the environment and run the hook.
[ ! -d "$proj_path/$envname" ] && return 0
workit "$projname"
#workit_source_hook "$WORKIT_HOME/postmkvirtualenv"
}
# List the available environments.
function show_workit_projects () {
verify_workit_home || return 1
# NOTE: DO NOT use ls here because colorized versions spew control characters
# into the output list.
all=()
for ((i=1;i<=${#WORKIT_HOME};i++)); do
echo "\n========================================"
echo "Workit directory $WORKIT_HOME[$i]:\n"
ls --color=auto -C $WORKIT_HOME[$i]
#dirs=$( cd "$WORKIT_HOME[$i]"; for f in *; do [[ -d $f ]] && echo $f; done )
#all+=("\n\n$dirs")
done
#echo $all
}
# list the available workit home directories for adding a new project to
function show_workit_home_options () {
verify_workit_home || return 1
for ((i=1;i<=${#WORKIT_HOME};i++)); do
proj=${WORKIT_HOME[$i]}
echo "$i - $proj"
done
}
# List or change workit projects
#
# Usage: workit [environment_name]
#
function workit () {
typeset PROJ_NAME="$1"
if [ "$PROJ_NAME" = "" ]
then
show_workit_projects
return 1
fi
PROJ_PATH=$( verify_workit_project "$PROJ_NAME" )
if [ ! -d $PROJ_PATH ]
then
return 1
else
export PROJ_PATH
fi
verify_workit_home || return 1
# Deactivate any current environment "destructively"
# before switching so we use our override function,
# if it exists.
type deactivate >/dev/null 2>&1
if [ $? -eq 0 ]
then
deactivate
fi
cd $PROJ_PATH
# Save the deactivate function from virtualenv
# virtualenvwrapper_saved_deactivate=$(typeset -f deactivate)
# Replace the deactivate() function with a wrapper.
eval 'function deactivate () {
# # Call the local hook before the global so we can undo
# # any settings made by the local postactivate first.
# virtualenvwrapper_source_hook "$VIRTUAL_ENV/bin/predeactivate"
# virtualenvwrapper_source_hook "$WORKON_HOME/predeactivate"
#
# env_postdeactivate_hook="$VIRTUAL_ENV/bin/postdeactivate"
#
# # Restore the original definition of deactivate
# eval "$virtualenvwrapper_saved_deactivate"
# # Instead of recursing, this calls the now restored original function.
# deactivate
# virtualenvwrapper_source_hook "$env_postdeactivate_hook"
workit_source_hook "postdeactivate"
}'
workit_source_hook "postactivate"
# workit_source_hook "$project/postactivate"
return 0
}
#
# Set up tab completion. (Adapted from Arthur Koziel's version at
# http://arthurkoziel.com/2008/10/11/virtualenvwrapper-bash-completion/)
#
compctl -g "`show_workit_projects`" workit