Add support for building with alternate executable names

This commit is contained in:
Ethan P 2019-09-08 17:43:29 -07:00
parent 5749d9ea2d
commit 3d4a0b8381
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC
6 changed files with 43 additions and 15 deletions

View File

@ -48,4 +48,12 @@ This uses [shfmt](https://github.com/mvdan/sh) to perform minification.
**Installation:**
You can also specify `--install` and `--prefix=PATH` to have the build script automatically install the scripts for all users on the system. You may need to run the build script as root.
You can also specify `--install` and `--prefix=PATH` to have the build script automatically install the scripts for all users on the system. You may need to run the build script as root.
**Alternate Executable:**
Depending on the distribution, bat may have been renamed to avoid package conflicts.
If you wish to use these scripts on a distribution where this is the case, there is an `--alternate-executable=NAME` option which will build the scripts to use an alternate executable name.

View File

@ -55,7 +55,7 @@ step_read() {
# Build step: preprocess
# Preprocesses the script.
#
# This will embed library scripts.
# This will embed library scripts and replace the BAT variable.
#
# Input:
# The original file contents.
@ -69,6 +69,12 @@ step_preprocess() {
while IFS='' read -r line; do
# Skip certain lines.
[[ "$line" =~ ^LIB=.*$ ]] && continue
# Replace the BAT variable with the build option.
if [[ "$line" =~ ^BAT=.*$ ]]; then
printf "BAT=%q\n" "$OPT_BAT"
continue
fi
# Embed library scripts.
if [[ "$line" =~ ^[[:space:]]*source[[:space:]]+[\"\']\$\{?LIB\}/([a-z-]+\.sh)[\"\'] ]]; then
@ -169,18 +175,29 @@ pp_minify() {
OPT_INSTALL=false
OPT_MINIFY="lib"
OPT_PREFIX="/usr/local"
OPT_BAT="bat"
while shiftopt; do
case "$OPT" in
--install) OPT_INSTALL=true;;
--prefix) shiftval; OPT_PREFIX="$OPT_VAL";;
--minify) shiftval; OPT_MINIFY="$OPT_VAL";;
--install) OPT_INSTALL=true;;
--prefix) shiftval; OPT_PREFIX="$OPT_VAL";;
--alternate-executable) shiftval; OPT_BAT="$OPT_VAL";;
--minify) shiftval; OPT_MINIFY="$OPT_VAL";;
*) printc "%{RED}%s: unknown option '%s'%{CLEAR}" "$PROGRAM" "$OPT";
exit 1;;
esac
done
if [[ "$OPT_BAT" != "bat" ]]; then
printc "%{YELLOW}Building executable scripts with an alternate bat executable at %{CLEAR}%s%{YELLOW}.%{CLEAR}\n" "$OPT_BAT" 1>&2
if ! command -v "$OPT_BAT"; then
printc "%{YELLOW}WARNING: Bash cannot execute the specified file.\n" 1>&2
printc "%{YELLOW} The finished scripts may not run properly.%{CLEAR}\n" 1>&2
fi
printc "\n" 1>&2
fi
if [[ "$OPT_INSTALL" = true ]]; then
printc "%{YELLOW}Installing to %{MAGENTA}%s%{YELLOW}.%{CLEAR}\n" "$OPT_PREFIX" 1>&2
else

View File

@ -8,7 +8,7 @@
# Gets the current bat version.
bat_version() {
bat --version | cut -d ' ' -f 2
"$BAT" --version | cut -d ' ' -f 2
return
}

View File

@ -6,6 +6,7 @@
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
BAT="bat"
source "${LIB}/print.sh"
source "${LIB}/opt.sh"
source "${LIB}/version.sh"
@ -103,12 +104,12 @@ do_print() {
FIRST_PRINT=false
# Print the file.
bat "${BAT_ARGS[@]}" \
"${LAST_LR[@]}" \
"${LAST_LH[@]}" \
--style="${BAT_STYLE}" \
--paging=never \
"$LAST_FILE"
"$BAT" "${BAT_ARGS[@]}" \
"${LAST_LR[@]}" \
"${LAST_LH[@]}" \
--style="${BAT_STYLE}" \
--paging=never \
"$LAST_FILE"
# Print the separator.
echo "$SEP"

View File

@ -6,12 +6,13 @@
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
BAT="bat"
source "${LIB}/print.sh"
source "${LIB}/opt.sh"
source "${LIB}/version.sh"
# -----------------------------------------------------------------------------
export MANPAGER='sh -c "col -bx | bat --language man --style grid"'
export MANPAGER='sh -c "col -bx | '"$(printf "%q" "$BAT")"' --language man --style grid"'
export MANROFFOPT='-c'
export BAT_PAGER="$PAGER"

View File

@ -6,6 +6,7 @@
# Issues: https://github.com/eth-p/bat-extras/issues
# -----------------------------------------------------------------------------
LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/../lib"
BAT="bat"
source "${LIB}/opt.sh"
source "${LIB}/str.sh"
source "${LIB}/print.sh"
@ -117,10 +118,10 @@ extname() {
print_file() {
if [[ "${#BAT_ARGS[@]}" -eq 0 ]]; then
bat "$@"
"$BAT" "$@"
return $?
else
bat "${BAT_ARGS[@]}" "$@"
"$BAT" "${BAT_ARGS[@]}" "$@"
return $?
fi
}