-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbs
More file actions
executable file
·193 lines (158 loc) · 4.37 KB
/
bs
File metadata and controls
executable file
·193 lines (158 loc) · 4.37 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
#!/usr/bin/env bash
set -e
export BS_VERSION="1.2.3"
error() {
echo -e "\e[1m\e[31m$1\e[0m"
}
bold() {
echo -e "\e[36m$1\e[0m"
}
verbose() {
echo -e "\e[1m\e[30m$1\e[0m"
}
# Print help for bs itself
show-bs-help() {
echo ""
echo "bs: Bash build system"
echo ""
echo "usage: bs <command> [<args>]"
echo "usage: bs [<default-commmand-args>]"
echo "usage: bs ls"
echo ""
echo "bs allows you to create build scripts using simple shell scripting:"
echo ""
echo "1. Create a ./bs directory in your repository root"
echo ""
echo "2. Implement commands by adding *.sh scripts to ./bs with"
echo " the name of the command. For example:"
echo ""
bold " bs publish"
echo ""
echo " will call:"
echo ""
bold " ./bs/publish.sh"
echo ""
echo "3. Additional arguments can be passed, and will be received by"
echo " command scripts as \$1, \$2, etc. For example:"
echo ""
bold " bs publish --mode Release -v"
echo ""
echo " will call:"
echo ""
bold " ./bs/publish.sh --mode Release -v"
echo ""
echo "4. You can define a default script at ./bs/default.sh, which will"
echo " be called when the argument passed for <command> doesn't match"
echo " any script in ./bs."
echo ""
bold " bs restore --all --no-cache"
echo ""
echo " will call:"
echo ""
bold " ./bs/default.sh restore --all --no-cache"
echo ""
echo "5. That's actually it, its pretty simple."
echo ""
exit 0
}
# Converts the specified command to an executable script path
get-script-path() {
local command="$1"
local script_path="./bs/${command}.sh"
if [ -f "$script_path" ]; then
echo "$script_path"
return
fi
script_path=$(check-subdirs "$command")
if [ ! "$script_path" = "" ]; then
echo "$script_path"
elif [ -f "./bs/default.sh" ]; then
echo "./bs/default.sh"
fi
}
bullet() {
while read line; do
echo "- $line"
done
}
list-dir() {
local dir="$1"
(
cd "$dir"
find . -maxdepth 1 -name "*.sh" -type f -print | \
sed 's|^./||' | \
grep -v "^_" | \
grep -v "^default$" |
sed 's/.sh$//' | \
bullet
)
}
# Prints a list of all available commands
list() {
# List files in the bs root dir
list-dir "./bs"
# List aliases in the alias file
if [ -f ./bs/.bsconfig ]; then
cat ./bs/.bsconfig | \
while read line || [ -n "$line" ]; do
echo
echo "Commands in ${line}:"
echo
list-dir "$line"
done
fi
}
# Checks the specified command name against subdirectories in the .bsconfig file
check-subdirs() {
local command="$1"
if [ ! -f ./bs/.bsconfig ]; then
return
fi
local config_file=$(cat ./bs/.bsconfig)
cat ./bs/.bsconfig |
while read line || [ -n "$line" ]; do
local script_path="${line}/${command}.sh"
if [ -f "$script_path" ]; then
echo "$script_path"
return 0
fi
done
}
main() {
local command="$1"
local error_banner="bs ERROR:"
if [ "$command" = "help" ] || [ "$command" = "--help" ] || [ "$command" = "-h" ]; then
show-bs-help
exit 0
fi
if [ "$command" = "--version" ] || [ "$command" = "-v" ]; then
echo "bs: version $BS_VERSION"
exit 0
fi
if [ ! -d ./bs ]; then
error "$error_banner Expected a build commands directory at ./bs, but none was found"
exit 1
fi
if [ "$command" = "ls" ]; then
echo "Available bs commands for this project:"
echo ""
list
echo ""
exit 0
fi
local script_path=$(get-script-path "$command")
if [ "$script_path" = "" ]; then
error "$error_banner"
if [ "$command" = "" ]; then
error " * No command was specified"
error " * No default command was found at ./bs/default.sh"
else
error " * No file was found for ./bs/${command}.sh"
error " * No file was found for ${command} in any directory in ./bs/.bsconfig"
fi
exit 1
fi
# Slice out first two arguments (bash, script path), and pass to the command script
bash "$script_path" "${@:2}"
}
main "$@"