This repository has been archived by the owner on Apr 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbbuild
205 lines (170 loc) · 4.19 KB
/
bbuild
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
#!/bin/bash
VERSION_NUM=6.2.2
VERSION_NOTE="See https://github.com/taikedz/bbuild"
[[ "$*" = --version ]] && {
echo "$VERSION_NUM"
echo "$VERSION_NOTE"
exit
}
### Bash Builder 6 Usage:help
#
# This project is hosted at
# https://github.com/taikedz/bbuild
#
#
# Build a script using Bash Builder
#
# bbuild [OPTIONS ...] SOURCE [DEST]
#
# SOURCE - the source script
# DEST - the destination file to build to
#
# Options
# -------
#
# -c
# --check
# use shellcheck
#
# -C
# --no-check
# don't use shellcheck
#
# --ignore-tags
# Ignore tags in sourced files
#
# --out=DIRECTORY
# Specify the default output directory
# Used if DEST is not specified.
#
#
# Syntax post-processing
# ----------------------
#
# Bash Builder 5.2+ provides a post-processing utiltiy enabling extra syntax macros that
# can help write easier-to-maintain code.
#
# Run bbuild `--help syntax` for more information
#
###/doc
#%include std/safe.sh
#%include std/autohelp.sh
#%include std/args.sh
#%include std/out.sh
#%include std/debug.sh
#%include std/includefile2.sh
#%include std/bincheck.sh
#%include std/syntax-extensions.sh
#%include syntax.sh
$%function setup_build_dir(builddir) {
mkdir -p "$builddir" || out:fail "Cannot create the output directory '$builddir'!"
}
$%function checktags(builtfile) {
local tagsfound
if [[ "${IGNORETAGS:-}" = unsafe ]]; then
return
fi
tagsfound=(: $(grep -P '^#%bbtags' "$builtfile"|sed -r 's|^#%bbtags|| ; s|\s+|\n|g')) || :
debug:print "Tags found: ${tagsfound[*]:1}"
for tag in "${tagsfound[@]:1}"; do
debug:print "Processing tag: $tag"
case "$tag" in
i:*)
out:info "TAGS: $tag"
;;
w:*)
out:warn "TAGS: $tag"
;;
e:*)
out:fail "TAGS: $tag"
;;
*)
debug:print "TAGS: $tag"
;;
esac
done
}
$%function build_script(infile ?outfile) {
local t_BBPATH="$(dirname "$infile"):$BBPATH"
local tfname="$(basename "$infile")"
local tfile="$BUILDOUTD/$tfname"
if [[ -n "$outfile" ]]; then
tfile="$outfile"
fi
setup_build_dir "$(dirname "$outfile")"
out:info "Building $infile to $tfile"
INCLUDEFILE_token="#%include"
INCLUDEFILE_paths="$t_BBPATH"
includefile:process "$infile" > "$tfile" || out:fail "Inclusion on $tfile failed : [${INCLUDEFILE_failed:-}]"
bbuild:syntax_post "$tfile" || out:fail "Syntax post-processing on $tfile failed"
do_shellcheck "$tfile"
checktags "$tfile"
chmod 755 "$tfile"
}
do_shellcheck() {
if [[ "$SHELLCHECK" = true ]]; then
if bincheck:has shellcheck ; then
shellcheck -s bash "$1"
else
out:warn "You need to install shellcheck to perform syntax check"
fi
fi
}
arg_valueof() {
echo "${1#*=}"
}
$%function parse_flag(flagstring) {
case "$flagstring" in
-C|--no-check)
SHELLCHECK=false
;;
-c|--check)
SHELLCHECK=true
;;
--out=*)
BUILDOUTD="$(arg_valueof "$flagstring")"
;;
--ignore-tags)
IGNORETAGS=unsafe
;;
--release)
DOSETRELEASE=true
;;
*)
out:fail "Unknown argument '$flagstring'"
;;
esac
}
$%function parse_options(*bsource *bdest) {
local option
while [[ -n "$*" ]]; do
if [[ ! "$1" =~ ^- ]]; then
break
fi
parse_flag "$1"
shift
done
bsource="${1:-}"; shift || out:fail "No input file specified."
([[ -e "$bsource" ]] && [[ ! -d "$bsource" ]]) || out:fail "'$bsource' is not a file."
bdest="${1:-}"
if [[ -z "$bdest" ]]; then
bdest="$BUILDOUTD/$(basename "$bsource")"
fi
}
main() {
# args: [OPTIONS ...] SOURCE [DEST]
if [[ "$*" =~ "--help syntax" ]]; then
autohelp:print syntax
exit 0
fi
autohelp:check-or-null "$@"
safe:glob on
# --- Defaults
: ${BBPATH=./}
: ${BUILDOUTD=./build-outd}
: ${SHELLCHECK=false}
local buildsource builddest
parse_options buildsource builddest "$@"
build_script "$buildsource" "$builddest"
}
main "$@"