120 lines
2.3 KiB
Bash
Executable file
120 lines
2.3 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
fname=$(basename "$0")
|
|
usage()
|
|
{
|
|
echo "$fname <file>
|
|
Compile the target shell script into a single output
|
|
|
|
Functionality:
|
|
- resolve '%include' lines with shell capacity
|
|
- minimize code (shfmt required)
|
|
|
|
Options:
|
|
-I Don't resolve includes
|
|
-M Don't minimize code"
|
|
}
|
|
|
|
unset opt_m opt_I
|
|
while getopts ":hIM" opt;
|
|
do
|
|
case $opt in
|
|
h) usage && exit 1 ;;
|
|
I) opt_I=y ;;
|
|
M) opt_M=y ;;
|
|
esac
|
|
done
|
|
|
|
shift $((OPTIND-1))
|
|
|
|
# no arg
|
|
unset infile
|
|
if [ $# -lt 1 ]
|
|
then
|
|
if ! [ -t 0 ]
|
|
then infile=/dev/stdin
|
|
else usage && exit 1
|
|
fi
|
|
fi
|
|
|
|
[ -z "$TMPDIR" ] && TMPDIR=/tmp
|
|
tmpdir="$TMPDIR/shcompile_$(tr -dc '[:alnum:]' < /dev/urandom | head -c10)"
|
|
mkdir -p "$tmpdir"
|
|
retfile="$tmpdir/ret"
|
|
filelist="$tmpdir/list"
|
|
headfile="$tmpdir/head"
|
|
tailfile="$tmpdir/tail"
|
|
|
|
stop()
|
|
{
|
|
rm -rf "$tmpdir"
|
|
exit $1
|
|
}
|
|
|
|
warn()
|
|
{
|
|
printf "\033[33m%s\033[0m\n" "$*"
|
|
}
|
|
|
|
get_include_line()
|
|
{
|
|
[ -z "$opt_I" ] && grep -m1 -n '^%include ' "$1" | cut -d':' -f1
|
|
}
|
|
|
|
[ -z "$infile" ] && infile=$1
|
|
dirname=$()
|
|
[ "$infile" = '-' ] && infile=/dev/stdin
|
|
|
|
# create copy
|
|
cat "$infile" > "$retfile" 2>&1 || { echo "Error: cannot read '$infile'" >&2 && stop 2; }
|
|
|
|
# env when file
|
|
[ "$infile" != "/dev/stdin" ] && {
|
|
echo "$(readlink -f "$infile")" > "$filelist"
|
|
cd "$(dirname "$infile")"
|
|
}
|
|
|
|
firstline=$(head -n1 "$retfile" | grep '^#!/')
|
|
|
|
if [ -n "$firstline" ]
|
|
then
|
|
sed -i 1d "$retfile"
|
|
else
|
|
firstline='#!/bin/sh'
|
|
fi
|
|
|
|
n=$(get_include_line "$retfile")
|
|
while [ -n "$n" ]
|
|
do
|
|
pre=$(head -n $((n-1)) "$retfile" > "$headfile")
|
|
post=$(tail -n +$((n+1)) "$retfile" > "$tailfile")
|
|
incarg=$(sed "$n""q;d" "$retfile" | cut -d ' ' -f2-)
|
|
inc=$(echo "for I in $incarg ; do echo \$I ; done" | sh)
|
|
|
|
cp "$headfile" "$retfile"
|
|
echo "$inc" | while read -r file
|
|
do
|
|
cat "$file" >/dev/null 2>&1 || { echo "Error when trying to include '$incarg': '$file' could not be read" >&2 && stop 10; }
|
|
if ! grep -q "^$(readlink -f "$file")\$" "$filelist"
|
|
then # not already included
|
|
cat "$file" >> "$retfile"
|
|
echo "$(readlink -f "$file")" >> "$filelist"
|
|
fi
|
|
cd "$pwd"
|
|
done
|
|
cat "$tailfile" >> "$retfile"
|
|
# get next include line
|
|
n=$(get_include_line "$retfile")
|
|
done
|
|
|
|
if [ -z "$opt_M" ]
|
|
then
|
|
if which shfmt >/dev/null 2>&1
|
|
then shfmt -w -mn "$retfile"
|
|
else warn "warn: sfmt not installed, code cannot be minimized"
|
|
fi
|
|
fi
|
|
echo "$firstline"
|
|
cat "$retfile"
|
|
|
|
stop 0
|