shcompile: add options
This commit is contained in:
parent
433adb5121
commit
72a1106b4c
1 changed files with 55 additions and 20 deletions
|
|
@ -5,9 +5,28 @@ usage()
|
||||||
{
|
{
|
||||||
echo "$fname <file>
|
echo "$fname <file>
|
||||||
Compile the target shell script into a single output
|
Compile the target shell script into a single output
|
||||||
Resolves '%include' lines with shell capacity"
|
|
||||||
|
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
|
# no arg
|
||||||
unset infile
|
unset infile
|
||||||
if [ $# -lt 1 ]
|
if [ $# -lt 1 ]
|
||||||
|
|
@ -21,7 +40,7 @@ fi
|
||||||
[ -z "$TMPDIR" ] && TMPDIR=/tmp
|
[ -z "$TMPDIR" ] && TMPDIR=/tmp
|
||||||
tmpdir="$TMPDIR/shcompile_$(tr -dc '[:alnum:]' < /dev/urandom | head -c10)"
|
tmpdir="$TMPDIR/shcompile_$(tr -dc '[:alnum:]' < /dev/urandom | head -c10)"
|
||||||
mkdir -p "$tmpdir"
|
mkdir -p "$tmpdir"
|
||||||
tmpfile="$tmpdir/ret"
|
retfile="$tmpdir/ret"
|
||||||
filelist="$tmpdir/list"
|
filelist="$tmpdir/list"
|
||||||
headfile="$tmpdir/head"
|
headfile="$tmpdir/head"
|
||||||
tailfile="$tmpdir/tail"
|
tailfile="$tmpdir/tail"
|
||||||
|
|
@ -32,12 +51,22 @@ stop()
|
||||||
exit $1
|
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
|
[ -z "$infile" ] && infile=$1
|
||||||
dirname=$()
|
dirname=$()
|
||||||
[ "$infile" = '-' ] && infile=/dev/stdin
|
[ "$infile" = '-' ] && infile=/dev/stdin
|
||||||
|
|
||||||
# create copy
|
# create copy
|
||||||
cat "$infile" > "$tmpfile" 2>&1 || { echo "Error: cannot read '$infile'" >&2 && stop 2; }
|
cat "$infile" > "$retfile" 2>&1 || { echo "Error: cannot read '$infile'" >&2 && stop 2; }
|
||||||
|
|
||||||
# env when file
|
# env when file
|
||||||
[ "$infile" != "/dev/stdin" ] && {
|
[ "$infile" != "/dev/stdin" ] && {
|
||||||
|
|
@ -45,41 +74,47 @@ cat "$infile" > "$tmpfile" 2>&1 || { echo "Error: cannot read '$infile'" >&2 &&
|
||||||
cd "$(dirname "$infile")"
|
cd "$(dirname "$infile")"
|
||||||
}
|
}
|
||||||
|
|
||||||
firstline=$(head -n1 "$tmpfile" | grep '^#!/')
|
firstline=$(head -n1 "$retfile" | grep '^#!/')
|
||||||
|
|
||||||
[ -z "$firstline" ] && firstline='#!/bin/sh'
|
if [ -n "$firstline" ]
|
||||||
|
then
|
||||||
|
sed -i 1d "$retfile"
|
||||||
|
else
|
||||||
|
firstline='#!/bin/sh'
|
||||||
|
fi
|
||||||
|
|
||||||
get_include_line()
|
n=$(get_include_line "$retfile")
|
||||||
{
|
|
||||||
grep -m1 -n '^%include ' "$1" | cut -d':' -f1
|
|
||||||
}
|
|
||||||
|
|
||||||
n=$(get_include_line "$tmpfile")
|
|
||||||
while [ -n "$n" ]
|
while [ -n "$n" ]
|
||||||
do
|
do
|
||||||
pre=$(head -n $((n-1)) "$tmpfile" > "$headfile")
|
pre=$(head -n $((n-1)) "$retfile" > "$headfile")
|
||||||
post=$(tail -n +$((n+1)) "$tmpfile" > "$tailfile")
|
post=$(tail -n +$((n+1)) "$retfile" > "$tailfile")
|
||||||
incarg=$(sed "$n""q;d" "$tmpfile" | cut -d ' ' -f2-)
|
incarg=$(sed "$n""q;d" "$retfile" | cut -d ' ' -f2-)
|
||||||
inc=$(echo "for I in $incarg ; do echo \$I ; done" | sh)
|
inc=$(echo "for I in $incarg ; do echo \$I ; done" | sh)
|
||||||
|
|
||||||
cp "$headfile" "$tmpfile"
|
cp "$headfile" "$retfile"
|
||||||
echo "$inc" | while read -r file
|
echo "$inc" | while read -r file
|
||||||
do
|
do
|
||||||
cat "$file" >/dev/null 2>&1 || { echo "Error when trying to include '$incarg': '$file' could not be read" >&2 && stop 10; }
|
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"
|
if ! grep -q "^$(readlink -f "$file")\$" "$filelist"
|
||||||
then # not already included
|
then # not already included
|
||||||
cat "$file" >> "$tmpfile"
|
cat "$file" >> "$retfile"
|
||||||
echo "$(readlink -f "$file")" >> "$filelist"
|
echo "$(readlink -f "$file")" >> "$filelist"
|
||||||
fi
|
fi
|
||||||
cd "$pwd"
|
cd "$pwd"
|
||||||
done
|
done
|
||||||
cat "$tailfile" >> "$tmpfile"
|
cat "$tailfile" >> "$retfile"
|
||||||
# get next include line
|
# get next include line
|
||||||
n=$(get_include_line "$tmpfile")
|
n=$(get_include_line "$retfile")
|
||||||
done
|
done
|
||||||
|
|
||||||
which shfmt >/dev/null 2>&1 && shfmt -w -mn "$tmpfile"
|
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"
|
echo "$firstline"
|
||||||
cat "$tmpfile"
|
cat "$retfile"
|
||||||
|
|
||||||
stop 0
|
stop 0
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue