-
Notifications
You must be signed in to change notification settings - Fork 18
/
prm.sh
executable file
·374 lines (352 loc) · 13.4 KB
/
prm.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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
#!/usr/bin/env bash
# Copyright (c) 2015 Eivind Arvesen. All Rights Reserved.
COPY="Written by Eivind Arvesen, 2015."
VERSION=0.6.0
SOURCE="prm MUST be sourced - not run in a subshell.\ni.e. '. ./prm'\n"
function return_error() {
# Print error message and return error code
if [ "$2" ]; then
echo "$2"
fi
if [ "$1" ]; then
return "$1"
else
return 1
fi
}
# Test if prm is run in subshell or sourced
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
return_error 1 "$(printf "$SOURCE")"
exit
fi
prm_dir="${PRM_DIR:-$HOME/.prm}"
if [ ! -d "$prm_dir/.common" ]; then
# Create deepest directory needed (including its parents)
mkdir -p "$prm_dir/.common"
fi
if [[ $(basename "$SHELL") = zsh ]]; then
prompt_var=RPROMPT
else
prompt_var=PS1
fi
function prm_load() {
# Loader-function to enable reusable components in projects
if [ "$1" ]; then
if [ -f "$prm_dir/.common/$1.sh" ]; then
. "$prm_dir/.common/$1.sh"
else
return_error 1 "Could not load user script $1"
fi
else
return_error 1 "No script to load named"
fi
}
function prm_help() {
# Help-Screen
prm_usage
echo ""
echo "Options:"
echo " active List active project instances."
echo " add <project name> Add project(s)."
echo " copy <old> <new> Copy project."
echo " edit <project name> Edit project(s)."
echo " list List all projects."
echo " remove <project name> Remove project(s)."
echo " rename <old> <new> Rename project."
echo " start <project name> Start project."
echo " stop Stop active project."
echo " -h --help Display this information."
echo " -v --version Display version info."
echo ""
echo "Please report bugs at https://github.com/EivindArvesen/prm"
printf "Remember that $SOURCE"
}
function prm_usage() {
# Usage-sentence
echo "usage: prm <option> [<args>] ..."
}
function check_editor() {
# Checking if editor-variable is set
if [ -z "$EDITOR" ]; then
echo "\$EDITOR is not set."
echo "You will not be able to add, copy or edit projects."
return 1
fi
}
function set_prompt_start() {
# Change prompt to include name of active prm project
if [ ! -e "$prm_dir/.prompt-$$.tmp" ]; then
cur_prompt=""
eval "cur_prompt=\$$prompt_var"
echo "$cur_prompt" > "$prm_dir/.prompt-$$.tmp"
eval "export $prompt_var"
eval $prompt_var="'[$1] $cur_prompt'"
else
eval "export $prompt_var"
eval $prompt_var="'[$1] $(cat "$prm_dir/.prompt-$$.tmp")'"
fi
}
function set_prompt_finish() {
# Revert prompt to what it was before prm was activated
eval "export $prompt_var"
eval $prompt_var="'$(cat "$prm_dir/.prompt-$$.tmp")'"
}
function check_project_name() {
# Verify that project name is not blacklisted (reserved)
#prompt, path, active, common
case "$1" in
.*|*.tmp)
echo "$1: Illegal name"
return 1
esac
}
function edit_scripts() {
# Open project start- and stop- scripts in $EDITOR
if [ $prm_use_cygpath ]; then
# Allow Cygwin users to use the full Windows path
# (ex: C:\\Users\\...) when editing the script.
$EDITOR `cygpath.exe -d "$prm_dir/$1/start.sh"` && $EDITOR `cygpath.exe -d "$prm_dir/$1/stop.sh"`
else
$EDITOR "$prm_dir/$1/start.sh" && $EDITOR "$prm_dir/$1/stop.sh"
fi
}
function cleanup() {
# Clean dead project "instances"
cd "$prm_dir" >/dev/null 2>&1 || return_error 1 "Directory $prm_dir does not exist."
if [ -n "$(find . -maxdepth 1 -name '.active*' -print -quit)" ]; then
for instance in .active*; do
pid=${instance%.*}
pid=${pid##*-}
if (! ps -p "$pid" > /dev/null); then
rm -f "$prm_dir/.active-$pid.tmp" "$prm_dir/.path-$pid.tmp" "$prm_dir/.prompt-$pid.tmp"
fi
done
fi
cd - >/dev/null 2>&1 || return_error 1 "Previous directory not available."
}
function prm_run() {
# Handle args + main functionality
case "$1" in
# Test args
active)
# List active project "instances"
cd "$prm_dir" >/dev/null 2>&1 || return_error 1 "Directory $prm_dir does not exist."
while IFS= read -r -d '' instance; do
pid=${instance%.*}
pid=${pid##*-}
if (ps -p "$pid" > /dev/null); then
echo "$pid $(cat "$instance")"
fi
done < <(find . -maxdepth 1 -name '.active*' -print0 -quit)
cd - >/dev/null 2>&1 || return_error 1 "Previous directory not available."
;;
add)
# Add project
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -d "$prm_dir/$argument" ]; then
return_error 1 "Project $argument already exists"
return 1
else
check_project_name "$argument" || return
check_editor || return
mkdir -p "$prm_dir/$argument"
printf "#!/usr/bin/env bash\n\n# This script will run when STARTING the project \"%s\"\n# Here you might want to cd into your project directory, activate virtualenvs, etc.\n\n# The currently active project is available via \$PRM_ACTIVE_PROJECT\n# Command line arguments can be used, \$3 would be the first argument after your project name.\n\n" "$argument" > "$prm_dir/$argument/start.sh"
printf "#!/usr/bin/env bash\n\n# This script will run when STOPPING the project \"%s\"\n# Here you might want to deactivate virtualenvs, clean up temporary files, etc.\n\n# The currently active project is available via \$PRM_ACTIVE_PROJECT\n# Command line arguments can be used, \$3 would be the first argument after your project name.\n\n" "$argument" > "$prm_dir/$argument/stop.sh"
edit_scripts $argument
echo "Added project $argument"
fi
done
else
return_error 1 "No name given"
return 1
fi
;;
copy)
# Copy project
if [ "$2" ]; then
if [ ! -d "$prm_dir/$2" ]; then
return_error 1 "$2: No such project"
return 1
else
if [ "$3" ]; then
if [ -d "$prm_dir/$3" ]; then
return_error 1 "Project $3 already exists"
return 1
else
check_project_name "$3" || return
check_editor || return
cp -r "$prm_dir/$2" "$prm_dir/$3"
sed -i -e "s/\"$2\"/\"$3\"/g" $prm_dir/$3/*.sh
edit_scripts $3
echo "Copied project $2 to $3"
fi
else
return_error 1 "No new name given"
return 1
fi
fi
else
return_error 1 "No name given"
return 1
fi
;;
edit)
# Edit project
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -d "$prm_dir/$argument" ]; then
check_editor || return
edit_scripts $argument
echo "Edited project $argument"
else
return_error 1 "$argument: No such project"
return 1
fi
done
else
return_error 1 "No name given"
return 1
fi
;;
list)
# List projects
if [ ! "$(find "$prm_dir" -type d | wc -l)" -gt 2 ]; then
return_error 1 "No projects exist"
return 1
else
cd "$prm_dir/" >/dev/null 2>&1 || return_error 1 "Directory $prm_dir does not exist."
for active in ./*; do
basename "$active"
done
cd - >/dev/null 2>&1 || return_error 1 "Previous directory not available."
fi
;;
remove)
# Remove project
if [ "$2" ]; then
for argument in "${@:2}"; do
if [ -e "$prm_dir/.active-$$.tmp" ] && [ "$(cat "$prm_dir/.active-$$.tmp")" = "$argument" ]; then
return_error 1 "Stop project $argument before trying to remove it"
return 1
else
if [ -d "$prm_dir/$argument" ]; then
rm -rf "${prm_dir:?}/$argument/"
echo "Removed project $argument"
else
return_error 1 "$argument: No such project"
return 1
fi
fi
done
else
return_error 1 "No name given"
return 1
fi
;;
rename)
# Rename project
if [ -e "$prm_dir/.active-$$.tmp" ] && [ "$(cat "$prm_dir/.active-$$.tmp")" = "$2" ]; then
return_error 1 "Stop project $2 before trying to rename it"
return 1
else
if [ "$2" ]; then
if [ ! -d "$prm_dir/$2" ]; then
return_error 1 "$2: No such project"
return 1
else
if [ "$3" ]; then
if [ -d "$prm_dir/$3" ]; then
return_error 1 "Project $3 already exists"
return 1
else
mv "$prm_dir/$2" "$prm_dir/$3"
echo "Renamed project $2 $3"
fi
else
return_error 1 "No new name given"
return 1
fi
fi
else
return_error 1 "No name given"
return 1
fi
fi
;;
start)
# Start project
if [ "$2" ]; then
if [ -d "$prm_dir/$2" ]; then
if [ -e "$prm_dir/.active-$$.tmp" ] && [ "$(cat "$prm_dir/.active-$$.tmp")" = "$2" ]; then
return_error 1 "Project $2 is already active"
return 1
else
if [ ! -e "$prm_dir/.path-$$.tmp" ]; then
pwd > "$prm_dir/.path-$$.tmp"
fi
if [ -e "$prm_dir/.active-$$.tmp" ]; then
. "$prm_dir/$(cat "$prm_dir/.active-$$.tmp")/stop.sh"
PRM_ACTIVE_PROJECT=""
fi
if [ -e "$prm_dir/$2/start.sh" ] && [ -e "$prm_dir/$2/stop.sh" ]; then
echo "$2" > "$prm_dir/.active-$$.tmp"
set_prompt_start "$2"
echo "Starting project $2"
. "$prm_dir/$2/start.sh"
PRM_ACTIVE_PROJECT=$2
else
return_error 1 "Cannot start project $2: Project has no scripts"
return 1
fi
fi
else
return_error 1 "$2: No such project"
return 1
fi
else
return_error 1 "No name given"
return 1
fi
;;
stop)
# Stop project
if [ -e "$prm_dir/.active-$$.tmp" ]; then
. "$prm_dir/$(cat "$prm_dir/.active-$$.tmp")/stop.sh" || return_error 1 "Cannot stop project $PRM_ACTIVE_PROJECT: Project has no stop script"
echo "Stopping project $(cat "$prm_dir/.active-$$.tmp")"
rm -f "$prm_dir/.active-$$.tmp"
cd "$(cat "$prm_dir/.path-$$.tmp")" >/dev/null 2>&1 || return_error 1 "Could not change directory to original path."
rm -f "$prm_dir/.path-$$.tmp"
set_prompt_finish
rm -f "$prm_dir/.prompt-$$.tmp"
PRM_ACTIVE_PROJECT=""
else
return_error 1 "No active project"
return 1
fi
;;
-h|--help)
# Help-Screen
prm_help
;;
-v|--version)
# Version-Screen
echo "prm $VERSION."
echo "$COPY"
;;
*)
# Anything else
if [ -z "$1" ]; then
# Bare command
prm_help
else
# Error-Screen
return_error 1 "prm: illegal option -- $1 (see \"prm --help\" for help)"
prm_usage
return 1
fi
;;
esac
cleanup
}
prm_run "$@"