-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtasklib.sh
More file actions
executable file
·413 lines (357 loc) · 8.31 KB
/
tasklib.sh
File metadata and controls
executable file
·413 lines (357 loc) · 8.31 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#!/usr/bin/env sh
# tasklib.sh is a collection of shell functions to help a programmer
# nimbly set up new tasks of mutiple repositories at a time using git
# worktrees, and also maintain a set of environment variables per
# task.
if [ -z "$TASKS_DIR" ]
then
export TASKS_DIR="$HOME/tasks/"
fi
if [ -z "$REPOS_DIR" ]
then
export REPOS_DIR="$HOME/repos/"
fi
# t.rcinit performs all rc init functions -- put this into your rc
# file after sourcing tasklib.sh.
t_rcinit() {
if [ -n "$TASK_SHELL_ENTER" ]
then
export TASK="$TASK_SHELL_ENTER"
unset TASK_SHELL_ENTER
t_enter "$TASK"
fi
}
t_err() {
echo "tasklib error:" "$@" >&2
}
t_log() {
echo "[tasklib]" "$@"
}
t_dbg() {
if [ -n "$tdebug" ]
then
echo "[tasklib:debug]" "$@"
fi
}
# t.new creates a new task, by creating the directories for it and
# sourcing the create rc file and the flavor rc file if a --flavor
# argument was passed.
t_new() {
argslist=""
task=""
for arg in "$@"
do
case "$arg" in
--*=*)
# Remove prefix, export
export "${arg#--}"
argslist="${argslist}\n${arg#--}"
t_dbg "exported: ${arg#--}"
;;
--*)
export "${arg#--}=1"
argslist="${argslist}\n${arg#--}=1"
t_dbg "exported bool: ${arg#--}=1"
;;
*)
task="$arg"
t_dbg "setting task: $task"
break
;;
esac
done
t_dbg "args: ${argslist}"
if [ -z "$task" ]
then
echo "t.new needs a task, usage:"
echo "t.new [options...] <task-name>"
return
fi
export TASK="$task"
mkdir -p "$TASKS_DIR/$task/src"
mkdir -p "$TASKS_DIR/$task/etc"
if ! cd "$TASKS_DIR/$task"
then
t_err "unable to cd to task directory"
return
fi
export TASK_LOC="$TASKS_DIR/$task"
if [ -f "$HOME/.trc/create" ]
then
# shellcheck disable=SC1091
. "$HOME/.trc/create"
fi
if [ -n "$flavor" ] && [ -d "$HOME/.trc/flavors/$flavor" ]
then
echo ". $HOME/.trc/flavors/$flavor" >> "$TASKS_DIR/$TASK/etc/.taskrc"
fi
{
printf "%b\n" "${argslist}";
printf "TASK=%s\n" "$TASK";
printf "TASK_LOC=%s\n" "$TASK_LOC";
} >> "$TASK_LOC/etc/.taskrc"
echo "ok, created $TASK in $TASKS_DIR/$TASK."
if [ -z "$noenter" ]
then
t_enter "$TASK"
fi
}
alias t.new=t_new
# t_enter enters the ticket environment in the current shell instance.
t_enter() {
if [ -z "$1" ]
then
echo "you need to specify a task as the first argument."
return
fi
glbl_rcfile="$HOME/.trc/enter"
if [ -f "$glbl_rcfile" ]
then
t_dbg "sourcing global rcfile: $glbl_rcfile"
# shellcheck disable=SC1090
. "$glbl_rcfile"
fi
export TASK="$1"
lcl_rcfile="$TASKS_DIR/$1/etc/.taskrc"
if [ -f "$lcl_rcfile" ]
then
t_dbg "sourcing local rcfile: $lcl_rcfile"
# shellcheck disable=SC1090
. "$lcl_rcfile"
fi
if [ -d "$TASK_LOC" ]
then
if ! cd "$TASK_LOC"
then
t_err "unable to cd to task location $TASK_LOC"
return
fi
else
echo "there is no task $1?"
fi
}
alias t.enter=t_enter
t_shell() {
if [ -z "$1" ]
then
t_err "you need to specify a task as the first argument."
return
fi
TASK_SHELL_ENTER="$1" "$SHELL" -i
}
alias t.sh=t_shell
t_worktree() {
if [ -z "$1" ]
then
t_err "you need to specify a repository as the first argument."
return
fi
if [ -z "$TASK" ]
then
t_err "you are not currently working on a task."
return
fi
repodir="$REPOS_DIR/$1"
if ! [ -d "$repodir" ]
then
t_err "the repo directory: $repodir doesn't exist."
return
fi
if [ -z "$2" ]
then
wdir="$TASK_LOC/src/$1"
elif [ -d "$(dirname "$TASK_LOC/src/$2")" ]
then
wdir="$TASK_LOC/src/$2"
t_log "cloning relative path $2 to $wdir"
else
wdir="$2"
fi
if [ -z "$3" ]
then
head="origin/HEAD"
else
t_log "using starting point $3"
head="$3"
fi
git -C "$repodir" worktree add -b "$TASK" "$wdir" "$head"
}
alias t.worktree=t_worktree
t_cd() {
if [ -z "$1" ]
then
cd "$TASK_LOC" || {
t_err "unable to cd to task location: $TASK_LOC.";
return;
}
elif [ -d "$TASK_LOC/$1" ]
then
cd "$TASK_LOC/$1" || {
t_err "unable to cd to location: $TASK_LOC/$1";
return;
}
elif [ -d "$TASK_LOC/src/$1" ]
then
cd "$TASK_LOC/src/$1" || {
t_err "unable to cd to location: $TASK_LOC/src/$1";
return;
}
else
found=$(find "$TASK_LOC" -name "$1" | head -1)
if [ -z "$found" ]
then
t_err "unable to find $1"
return
fi
if [ -d "$found" ]
then
cd "$found" || {
t_err "can't cd to $found";
return;
}
else
cd "$(dirname "$found")" || {
t_err "can't cd to $(dirname "$found")";
return;
}
fi
fi
}
alias t.cd=t_cd
t_install_files() {
if ! [ -e "./.git" ] || ! [ -f "./tasklib.sh" ]
then
t_err "can't install from here, I need to be in the tasklib git."
return
fi
t_log "making the .trc directory"
mkdir -p "$HOME/.trc"
t_log "making tasklib symlink..."
if [ -e "$HOME/.trc/tasklib.sh" ]
then
unlink "$HOME/.trc/tasklib.sh" || {
t_err "tasklib.sh already is installed and can't be removed";
return
}
fi
ln -s "$PWD/tasklib.sh" "$HOME/.trc/tasklib.sh"
mkdir -p "$HOME/.trc/flavors"
t_dbg "the flavor, arg 1 is: $1"
if [ -n "$1" ] && [ -d "$PWD/templates/$1" ]
then
t_log "copying template: $1"
cp -R "$PWD/templates/$1/" "$HOME/.trc/" || {
t_err "copying template $1 failed.";
return;
}
elif ! [ -f "$HOME/.trc/create" ] && ! [ -f "$HOME/.trc/enter" ]
then
t_log "no template specified, creating touchfiles..."
printf "# This script sourced on create\n" > "$HOME/.trc/create"
printf "# This script sourced on enter\n" > "$HOME/.trc/enter"
mkdir -p "$HOME/.trc/flavors" || {
t_err "unable to make flavors dir"
}
fi
chmod a+x "$HOME/.trc/create"
chmod a+x "$HOME/.trc/enter"
}
__t_rcfile() {
if echo "$SHELL" | grep "zsh" >/dev/null
then
if [ -f "$HOME/.zprofile" ]
then
echo "$HOME/.zprofile"
else
echo "$HOME/.zshrc"
fi
return
fi
echo "$HOME/.bashrc"
}
# t_install_rc installs the rc hooks -- source the tasklib.sh file,
# and execute the t_rcinit func.
t_install_rc() {
t_log "installing hooks in $(__t_rcfile)"
# shellcheck disable=SC2016
if ! grep '. $HOME/.trc/tasklib.sh' "$(__t_rcfile)" > /dev/null
then
{
echo "# source tasklib.sh - task utilities";
# shellcheck disable=SC2016
echo '. $HOME/.trc/tasklib.sh';
} >> "$(__t_rcfile)"
fi
if ! grep 't_rcinit' "$(__t_rcfile)" > /dev/null
then
echo "t_rcinit" >> "$(__t_rcfile)"
fi
}
t_set() {
if [ -z "$TASK_LOC" ]
then
t_err "not in a task"
return
fi
if (echo "$1" | grep -E '^[a-zA-Z_0-9]+=.*$')
then
echo "$1" >> "$TASK_LOC/etc/.taskrc"
export "${1?}"
t_log "Set var $1"
elif [ -n "$1" ] && [ -n "$2" ]
then
echo "$1=$2" >> "$TASK_LOC/etc/.taskrc"
export "${1?}=${2?}"
t_log "Set var $1=$2"
fi
}
t_help() {
cat <<_EOF_
tasklib.sh functions --
t_new [options...] <task-name> - create a new task.
This creates a new task by creating the task directory layout and
sourcing the rc file in $HOME/.trc/create, and entering the new
task with t_enter.
each option is stripped of initial dashes and exported in the
local task rc file so that when you enter the task those env
vars are created.
special options --
flavor -- if --flavor=<flavor> is passed then the task rc file
will also source the rcfile for the flavor in
\$HOME/.trc/flavors/<flavor> on entry.
t_enter <task-name> - enters the task. This means sourcing the task
rc file in \$TASK_LOC/etc/.taskrc, then cd'ing to \$TASK_LOC.
t_shell <task-name> - enters the task in a new subshell. This
requires the correct rc setup, where you run the t_rcinit func in
your rc file.
t_worktree <repository> - creates a git worktree for the repository
in the task's source folder, using the current task name as a branch.
t_set <var>=<value> or t_set var value - adds the export of
var=value to the current taskrc file.
_EOF_
}
alias t.help=t_help
t_task() {
echo "$TASK"
}
alias t.task=t_task
# t_install installs tasklib.sh locally.
t_install() {
t_log "installing files..."
t_install_files "$@"
t_install_rc "$@"
}
t_update() {
t_err "not implemented"
}
case "$1" in
install)
shift
t_install "$@"
;;
update)
t_update
;;
*)
;;
esac