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
/
bbrun
100 lines (75 loc) · 2.17 KB
/
bbrun
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
#!/usr/bin/env bash
set -euo pipefail
### Bash Builder Runner Usage:help
#
# Run a Bash Builder script directly without pre-compiling.
#
# Suitable for use with a shebang line.
#
# bbrun SCRIPT ARGS ...
#
# bbrun uses a temp file in ~/.local/tmp with the same name as
# the original script.
#
# When the compiled script is run, an environment variable
# `BBRUN_SCRIPT` contains the path of the original script (use this
# instead of $0).
#
# Set environment variable `DEBUG_mode=true` to see the BBPATH and build target
#
###/doc
BBRUN_VERSION=1.2
#%include std/syntax-extensions.sh
#%include std/abspath.sh
#%include std/debug.sh
#%include std/colours.sh
#%include std/autohelp.sh
removebuilt() {
rm "$BUILTSCR" "$BUILDMESSAGES"
}
rewrite_shebang() {
sed -r -e '1 s|^#!.+$|#!/usr/bin/env bash|g' -i "$BUILTSCR"
debug:print "Shebang: $(head -n 1 "$BUILTSCR")"
}
check_build_failure() {
if [[ "$1" != 0 ]]; then
echo -n "$CRED"
cat "$BUILDMESSAGES"
echo -n "$CDEF"
exit 1
fi
}
make_private_script() {
touch "$BUILTSCR"
chmod 700 "$BUILTSCR"
}
$%function version_check(flag) {
if [[ "$flag" =~ ^--version$ ]]; then
echo "$BBRUN_VERSION"
exit 0
fi
}
$%function bbrun:main(bbscript) {
local tempscr build_result BBRUN_SCRIPT
build_result=0
version_check "$bbscript"
tmpdir="$HOME/.local/tmp"
[[ -e "$tmpdir" ]] ||
mkdir -p "$tmpdir"
# Use absolute path so that built script is callable predictably
export BBRUN_SCRIPT="$(abspath:path "$bbscript")"
BUILTSCR="$tmpdir/$(basename "$bbscript")"
([[ -e "$BBRUN_SCRIPT" ]] && [[ ! -d "$BBRUN_SCRIPT" ]]) || out:fail "'$bbscript' is not a file."
make_private_script
BUILDMESSAGES="$(mktemp)"
debug:print "Building dynamically | BBPATH => $BBPATH"
debug:print "Building to [$BUILTSCR]"
bbuild "$bbscript" "$BUILTSCR" &> "$BUILDMESSAGES" || build_result=$?
chmod 700 "$BUILTSCR" # re-apply the perms, because bbuild will set 755 as part of the build
trap removebuilt exit
check_build_failure "$build_result"
rewrite_shebang
"$BUILTSCR" "$@"
}
autohelp:check-or-null "$@"
bbrun:main "$@"