-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasker.sh
104 lines (95 loc) · 3.21 KB
/
tasker.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
#!/bin/bash
__tasker_WORK_DIR="/tmp/tasker"
rm -rf $__tasker_WORK_DIR
mkdir -p $__tasker_WORK_DIR
__tasker_CURRENT_TASK=""
mkdir $__tasker_WORK_DIR/tasks
function task() {
mkdir $__tasker_WORK_DIR/tasks/$1
__tasker_CURRENT_TASK=$1
unset -f met
unset -f meet
}
function doc() {
echo $1 >$__tasker_WORK_DIR/tasks/$__tasker_CURRENT_TASK/doc
}
function depends() {
echo $* >$__tasker_WORK_DIR/tasks/$__tasker_CURRENT_TASK/depends
}
function endtask() {
# save 'met' function
if typeset -F | grep 'declare -f met' >/dev/null; then
type met | tail +2 >$__tasker_WORK_DIR/tasks/$__tasker_CURRENT_TASK/met
fi
# save 'meet' function
if typeset -F | grep 'declare -f meet' >/dev/null; then
type meet | tail +2 >$__tasker_WORK_DIR/tasks/$__tasker_CURRENT_TASK/meet
fi
}
function check_and_do_task() {
# locals allow recursion
local doing_task=$1
local indent=$2
local task_dir=$__tasker_WORK_DIR/tasks/$doing_task
# make sure task exists
if [[ ! -d $task_dir ]]; then
echo "tasker:$indent Can't find task \"$doing_task\", exiting!"
exit 1
fi
# check if already met
if [[ -e $task_dir/met ]]; then
. $task_dir/met
if met; then
echo -e "tasker:$indent [\033[0;32m$doing_task\033[0m] Already met, we're done here."
return
else
echo -e "tasker:$indent [\033[0;33m$doing_task\033[0m] Not met, checking dependencies..."
fi
else
echo -e "tasker:$indent [\033[0;33m$doing_task\033[0m] No met block, checking dependencies..."
fi
# not already met, check dependencies
if [[ -e $task_dir/depends ]]; then
for task in $(cat $task_dir/depends); do
check_and_do_task $task "$indent"" | "
done
echo -e "tasker:$indent [\033[0;33m$doing_task\033[0m] ...dependencies sorted, doing meet..."
else
echo -e "tasker:$indent [\033[0;33m$doing_task\033[0m] ...no dependencies, doing meet..."
fi
# do meet
if [[ -f $task_dir/meet ]]; then
. $task_dir/meet
meet
if [[ ( ! -e $task_dir/met ) && $? != 0 ]]; then
echo -e "tasker:$indent [\033[1;31m$doing_task\033[0m] ...no met block and last command failed, exiting!"
exit 1
fi
echo -e "tasker:$indent [\033[0;33m$doing_task\033[0m] ...all done, verifying..."
else
echo -e "tasker:$indent [\033[0;33m$doing_task\033[0m] ...no meet, verifying..."
fi
# verify meet worked
if [[ -e $task_dir/met ]]; then
. $task_dir/met
if met; then
echo -e "tasker:$indent [\033[1;32m$doing_task\033[0m] ...satisfied met, nice."
return
else
echo -e "tasker:$indent [\033[1;31m$doing_task\033[0m] ...still not met, exiting!"
exit 1
fi
else
echo -e "tasker:$indent [\033[1;32m$doing_task\033[0m] ...no met to verify, all done."
fi
}
function do_action() {
if (( $# == 0 )); then
# echo "tasker: No parameter(s) or default task"
echo "tasker: No parameter, available tasks are [ $(ls -m $__tasker_WORK_DIR/tasks) ]"
else
check_and_do_task $1 ""
fi
rm -rf $__tasker_WORK_DIR
}
trap "do_action $*" EXIT