forked from crosswalk-project/crosswalk-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
site.sh
executable file
·100 lines (87 loc) · 1.95 KB
/
site.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
#!/bin/bash
desc="Site management script"
app=$0
cmd=$0
if [[ ! -e "$cmd" && "$cmd" =~ .*\/.* ]]; then
cmd=$(which "$cmd")
fi
dir=$(dirname "$cmd")/scripts
. ${dir}/common.inc
debug=${debug:=0}
declare names=()
declare descs=()
declare valid=()
declare max_len=0
function scope_source () {
. $1
echo $desc
}
for script in "${dir}"/*.sh; do
names[$i]=$(basename ${script/.sh})
# Verify the script starts with an inclusion of common.inc
first_line=$(grep -m 1 -v -E '^((#.*)|([[:blank:]]*))$' ${script})
desc=$(scope_source "$script")
if [[ "$desc" == "" ]]; then
valid[$i]="0"
else
valid[$i]="1"
fi
descs[$i]=$desc
(( ${#names[$i]}>max_len )) && max_len=${#names[$i]}
i=$((i+1))
done
function usage () {
cat << EOF
usage: $app [--help]
<command> [<args>]
EOF
# Loop through all of the defined scripts and display them
for (( j=0; j<i; j++ )); do
printf " %-*s %-.*s\n" ${max_len} "${names[$j]}" $((73-max_len)) "${descs[$j]}"
done
cat << EOF
NOTE: Most commands require that there are no changed files on disk.
EOF
exit
}
function execute_script () {
local site_script=$1
local site_cmd=$2
shift 2
unset -f ${site_cmd}
. "${dir}/${site_script}.sh"
${site_cmd} $*
exit
}
# If --help was passed, then show the sub-command usage
if [[ "$1" = "--help" ]]; then
mode="usage"
shift
else
mode="run"
fi
if [[ "$1" = "" ]]; then
usage
fi
j=0
while [[ "${names[$j]}" != "" ]]; do
if [[ "${names[$j]}" = "$1" ]]; then
if [[ "${valid[$j]}" == "1" ]]; then
shift
for v in $@; do
if [[ "$v" == "--help" ]]; then
mode="usage"
fi
done
execute_script "${names[$j]}" $mode $*
else
echo "$1 is an invalid script."
fi
exit
fi
j=$((j+1))
done
cat << EOF
$app: '$1' is not a site command. See '$app --help'.
EOF
exit