-
Notifications
You must be signed in to change notification settings - Fork 0
/
dev
executable file
·111 lines (89 loc) · 2.35 KB
/
dev
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
#!/usr/bin/env bash
if [ $BASH_VERSINFO -lt 4 ]
then
echo "This script requires at least bash version 4"
exit 1
fi
if [ `uname -s` == 'Darwin' ]
then
READLINK_BIN=`which greadlink`
else
READLINK_BIN=`which readlink`
fi
CURRENT_DIR=$(pwd)
BASE_PATH=$(dirname $($READLINK_BIN -f $0))
COMMANDS_PATH=""
declare -A COMMANDS
##
# Create a symlink for the dev script
#
function create-symlink () {
echo "The 'dev' script was not found in your \$PATH"
echo "Would you like to create a symlink? $HOME/.local/bin/dev => $CURRENT_DIR/dev ?"
echo "Press enter to continue, Ctrl-C to quit"
read
if [ ! -d "$HOME/.local/bin" ]; then
mkdir "$HOME/.local/bin"
fi
ln -s "$CURRENT_DIR/dev" "$HOME/.local/bin/dev" || exit 1
echo "Symlink created, make sure that '$HOME/.local/bin' is in your \$PATH!"
exit 0
}
which dev >> /dev/null 2>&1 || create-symlink
##
# Load the commands defined in the commands dir
# where the 'dev' script is located
#
function load-global-commands () {
while IFS= read -r -d $'\0' line; do
source "$line"
COMMANDS[$COMMAND]="$HELP_TEXT"
done < <(find "$BASE_PATH" -type f -iname "*.command" -print0)
}
##
# Load the commands defined in the 'dev-commands' folder
# which can be located in the current or any parent dir
#
function load-local-commands () {
COMMANDS_PATH=$CURRENT_DIR
while [[ "$COMMANDS_PATH" != "" && ! -e "$COMMANDS_PATH/dev-commands" ]]; do
COMMANDS_PATH=${COMMANDS_PATH%/*}
done
COMMANDS_PATH="$COMMANDS_PATH/dev-commands"
if [ -d "$COMMANDS_PATH" ]; then
while IFS= read -r -d $'\0' line; do
source "$line"
COMMANDS[$COMMAND]="$HELP_TEXT"
done < <(find "$COMMANDS_PATH" -maxdepth 1 -type f -iname "*.command" -print0)
fi
}
##
# Generate the usage screen
#
function show-usage () {
echo "Usage: dev <command>"
echo
echo "Available commands:"
echo
for i in "${!COMMANDS[@]}"
do
echo -en "\e[32m" # Green
echo "$i:"
echo -en "\e[36m" # Cyan
echo " ${COMMANDS[$i]}"
echo -en "\e[39m" # Reset to default
echo
done
}
load-global-commands
load-local-commands
if [ "$1" == "" ] || [ "$1" == "help" ]; then
show-usage
exit 0
fi
declare -f "$1" >> /dev/null 2>&1
if [ "$?" == "0" ]; then
eval "${@:1}"
else
show-usage
fi