-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathactivate
More file actions
executable file
·494 lines (410 loc) · 9.57 KB
/
activate
File metadata and controls
executable file
·494 lines (410 loc) · 9.57 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/sh
# 4.2.0
BRO_COMMANDS="create remove list takeover update start goto"
bro_sed () {
if command -v gsed > /dev/null ; then
gsed $@
return 0
fi
sed $@
}
bro_expand () {
echo `sh -c "echo $1"`
}
bro_error () {
printf "Bro! $1.\n"
}
bro_usage () {
echo "Project management: bro <subcommand>"
echo "Available subcommands are:"
echo " create Create a new project."
echo " start Work on a project."
echo " list List available projects to work on."
echo " remove Delete an existing project."
echo " takeover Let bro handle existing project."
}
bro_goto_usage () {
echo "Usage: bro goto <project>"
echo " project Name of a project."
}
bro_create_usage () {
echo "Usage: bro create [-t template -p path] <project>"
echo " project Name of the project to be created."
echo " template Path to a template. It could be local directory or remote repo."
echo " path Absolute/relative path where the project directory is to be created."
}
bro_remove_usage () {
echo "Usage: bro remove <project>"
echo " project Name of the project to be removed."
}
bro_start_usage () {
echo "Usage: bro start <project>"
echo " project Name of the project to work on."
}
bro_takeover_usage () {
echo "Usage: bro takeover <path> <project>"
echo " path Path to existing project."
echo " project Name of the project."
}
bro_update () {
cwd=$(pwd)
cd $BRO_STATION
git pull origin master
cd $cwd
}
# setup a project
# like installing dependencies, git, preparing project structure etc
bro_setup () {
project=$1
bro_task setup $project
bro_init $project
}
bro_init () {
structure () {
tmux new -d -s $1
}
window () {
tmux new-window -n $1
}
run () {
tmux send "$@" ENTER
}
vsplit () {
tmux split-window -h -b
}
hsplit () {
tmux split-window -b
}
pane () {
case $1 in
up)
tmux select-pane -U
;;
right)
tmux select-pane -R
;;
down)
tmux select-pane -D
;;
left)
tmux select-pane -L
;;
esac
}
focus () {
if [[ $# = 2 ]]; then
tmux select-window -t $1
tmux select-pane -t $2
elif [[ $# = 1 ]]; then
tmux select-window -t $1
fi
}
connect () {
tmux kill-window -t 0
tmux a -t $1
}
bro_task init $@
unset -f structure
unset -f window
unset -f run
unset -f vsplit
unset -f hsplit
unset -f pane
unset -f focus
unset -f connect
}
bro_task () {
local task_file
task=$1
project=$2
project_reference=`bro_expand "$BRO_STATION/projects/$project"`
# check if the project exists
if [ ! -e "$project_reference" ]; then
bro_error "the project \"$project\" does not exist.\n Create it with $ bro create $1"
return 1
fi
cd $(cat $project_reference)
task_directory='./tasks'
if [ -d $task_directory ] ; then
case $task in
setup)
task_file=$(find $task_directory -type f -name 'setup' -o -name 'setup.*')
;;
init)
task_file=$(find $task_directory -type f -name 'init' -o -name 'init.*')
;;
exit)
task_file=$(find $task_directory -type f -name 'exit' -o -name 'exit.*')
;;
esac
fi
if [ ! -z $task_file ] ; then
chmod +x $task_file
first_line=$(bro_sed -n '1p' $task_file)
# remove task name and project name
shift
case $first_line in
'#!/bin/sh'|'#!/bin/bash'|'#!/bin/ksh'|'#!/bin/dash')
source $task_file $@
;;
*)
$task_file $@
;;
esac
fi
}
bro_start () {
possible_project=$1
if [ -z $possible_project ] ; then
bro_start_usage
fi
project_reference=`bro_expand "$BRO_STATION/projects/$possible_project"`
# if a tmux session already exists reconnect it
if [[ $(tmux ls 2>/dev/null | bro_sed -n "/$possible_project:/p") ]] ; then
tmux new-session -A -s $possible_project
fi
if [[ -f "$project_reference" ]] ; then
bro_init $@
else
bro_error "The project $possible_project does not exist."
fi
}
# creates a project
bro_create () {
local OPTIND template path opt project_path project_reference template_path
while getopts :t:p: opt; do
case $opt in
t) template=$OPTARG;;
p) path=$OPTARG;;
*) echo "Invalid option: -" $OPTARG;;
esac
done
## get project name
shift $(($OPTIND-1))
project=$@
if [ "$project" = "" ] && [ ! -z "$(echo $template | bro_sed '/.*\.git/p')" ] ; then
project="$(echo $template | bro_sed 's/.*\/\(.*\)\.git/\1/')"
fi
if [ "$project" = "" ] ; then
bro_error "you missed the project name"
bro_create_usage
return 1
fi
## set project path
if [ "$path" = "" ] ; then
project_path=`bro_expand "$WORKSTATION/$project"`
else
if [ "$(echo $path | bro_sed -n "/[~\/]/p")" = "" ] ; then
mkdir -p `bro_expand "$WORKSTATION/$path"`
project_path=`bro_expand "$WORKSTATION/$path/$project"`
else
project_path=`bro_expand "$path/$project"`
fi
fi
## set project reference path
project_reference=`bro_expand "$BRO_STATION/projects/$project"`
### check if the project already exists
if [ -e $project_reference ] ; then
bro_error "the project \"$project\" already exists"
return 1
fi
## set template
case $template in
"")
template_path=""
;;
# templates/name or name
git@*|http:*|https:*|/*|~*)
template_path=$template
;;
*)
template_path=`bro_expand "$WORKSTATION/$template"`
;;
esac
## create project directory at specified path
case $template_path in
# http or git
git@*|http:*|https:*)
git clone $template_path $project_path
if [ ! -e $project_path ] ; then
bro_error "it seems template at \"$template_path\" does not exist.\nPlease check the url or the internet connection."
return
fi
;;
# local path
*)
if [ ! -e $template_path ] ; then
bro_error "the bro template \"$template\" does not exist. Please create one at $WORKSTATION"
return
fi
mkdir -p $project_path
## copy files from template to project directory
if [ ! -z "$template_path" ] ; then
cp -a $template_path/. $project_path/
fi
;;
esac
## create project reference file
touch $project_reference
echo "$project_path" > $project_reference
## setup project
bro_setup $project
}
# remove a project
bro_remove () {
if [[ "$1" = "" ]] ; then
bro_remove_usage
return 1
fi
project=$1
project_reference=`bro_expand "$BRO_STATION/projects/$project"`
# check if the project already exists
if [ ! -e "$project_reference" ]; then
bro_error "the project \"$project\" does not exist"
return 1
fi
rm -f $project_reference
}
# list available projects
bro_list () {
ls $BRO_STATION/projects/
}
# takeover existing projects
bro_takeover () {
if [ $# = 0 ] ; then
bro_takeover_usage
return
fi
project_name=$(basename $1)
project_reference=$BRO_STATION/projects/$project_name
project_path=`cd $1 && pwd`
if [ -f $project_reference ] ; then
echo "Project already exists."
return
fi
if [ ! -d $project_path ] ; then
echo "$project_path does not exist."
return
fi
## create project reference file
touch $project_reference
echo $project_path > $project_reference
cd $project_path
bro_init $project_name
}
bro_exit () {
project=$(basename "`pwd`")
if [[ $(tmux ls 2>/dev/null | bro_sed -n "/$project:/p") ]]
then
tmux kill-session -t $project
return 0
fi
bro_task exit $project
}
bro_goto () {
possible_project=$1
if [ -z $possible_project ] ; then
bro_goto_usage
fi
project_reference=`bro_expand "$BRO_STATION/projects/$possible_project"`
if [[ -f "$project_reference" ]] ; then
cd $(cat $project_reference)
else
bro_error "The project $possible_project does not exist."
fi
}
bro () {
if [ "$#" = "0" ] ; then
bro_usage
return 1
fi
cmd=$1
project=$2
# local OPTIND
case $cmd in
goto)
shift
bro_goto $@
;;
create)
shift
bro_create $@
;;
remove)
bro_remove $project
;;
list)
bro_list
;;
takeover)
shift
bro_takeover $@
;;
exit)
bro_exit
;;
update)
bro_update
;;
exit)
bro_exit
;;
start)
shift
bro_start $@
;;
*)
bro_usage
;;
esac
}
# autocomplete
_bro () {
local cur prev
cur=${COMP_WORDS[COMP_CWORD]}
prev=${COMP_WORDS[COMP_CWORD-1]}
projects=$(ls $BRO_STATION/projects)
# $ bro
if (( $COMP_CWORD <= 1 )); then
COMPREPLY=( $(compgen -W '$BRO_COMMANDS' -- $cur) )
return 0
fi
if [[ "$prev" = "start" ]]; then
COMPREPLY=( $(compgen -W '$projects' -- $cur) )
return 0
fi
if [[ "$prev" = "goto" ]]; then
COMPREPLY=( $(compgen -W '$projects' -- $cur) )
return 0
fi
# if prev == <one of the projects>, look into the init.sh task if exists and display the functions
if [[ $(echo $projects | grep $prev) ]]; then
project_path=$(cat $BRO_STATION/projects/$prev)
init_path=$project_path/tasks/init.sh
if [[ -f $init_path ]]; then
# look for lines matching '<function name> ()' and omit the '()'
output=$(cat $init_path | grep -oE '^([a-z-]+) *\( *\)' | grep -Eo '[a-z][a-z-]+[a-z]')
COMPREPLY=( $(compgen -W '$output' -- $cur) )
fi
return 0
fi
# autocomplete options for create command
if [[ "$prev" = "create" ]]; then
COMPREPLY=( $(compgen -W '-p -t' -- $cur) )
return 0
fi
# autocomplete project names for remove command
if [[ "$prev" = "remove" ]]; then
COMPREPLY=( $(compgen -W '$projects' -- $cur) )
return 0
fi
if [[ "$prev" = "takeover" ]]; then
COMPREPLY=( $(compgen -f -- $cur) )
return 0
fi
# no auto completion for list command
if [[ "$prev" = "list" ]]; then
return 0
fi
}
complete -F _bro bro