add install script and g_aliases source-able file

This commit is contained in:
Emile Clark-Boman 2025-07-25 19:47:12 +10:00
parent def2126eeb
commit 766f1845cb
2 changed files with 135 additions and 0 deletions

31
bin/g_aliases Normal file
View file

@ -0,0 +1,31 @@
#!/usr/bin/env bash
# NOTE: This file is "incomplete"!
# NOTE: You have two options:
# NOTE: 1. run `./configure`
# NOTE: 2. manually set `G_BIN_DIR`
# WARNING: Variables patterned "AUTO_*" are automatically
# WARNING: replaced during `install_g` by `./configure`.
# WARNING: :: If you're setting them manually then make sure
# WARNING: :: they expand to absolute paths (ie ~/.local/bin or /usr/local/bin)
# === Configure g_aliases === #
G_BIN_DIR="AUTO_G_BIN_DIR" # g binary directory
# =========================== #
# Add binary directory to PATH (if not already)
if [ -d "$G_BIN_DIR" ] && [[ ":$PATH:" != *":$G_BIN_DIR:"* ]]; then
PATH="${PATH:+"$PATH:"}$G_BIN_DIR"
fi
# === g_aliases === #
G="$G_BIN_DIR/g"
alias g="$G"
alias g!="$G !"
alias g#="$G '#'" # avoid comment interpretation
alias g+="$G +"
alias g@="$G @"
alias g^="$G ^"
alias g^^="$G ^^"
alias gdiff="$G diff"
# ================= #

104
configure vendored Executable file
View file

@ -0,0 +1,104 @@
#!/usr/bin/env bash
# He wrote this in BASH because he hates himself, apparently :(
USAGE="Usage: ./configure [-g|--global] [-u|--uninstall] [-f|--force]"
set -ue
# === Configure Install === #
INSTALL_PATH_HOME="$HOME/.local/bin"
INSTALL_PATH_GLOBAL="/usr/local/bin"
SCRIPT_PERM=555
ALIASES_PERM=444
# ========================= #
# NOTE: since this last changed in 2015 its a really good unique identifier...
# NOTE: (to avoid uninstalling other programs called g)
G_IDENTIFIER="I'm writing this in BASH because I hate myself, apparently."
# Filters filenames based on if the file contains the G_IDENTIFIER
is_this_g() {
while read -r LOC; do
if [[ -d "$LOC" ]]; then
LOC="$LOC/g"
fi
grep -lF "$G_IDENTIFIER" "$LOC" 2>/dev/null
done <&0
}
# Finds absolute paths to all installed g binaries
# (or the extremely rare case there are multiple)
find_installed() {
(echo -e "$INSTALL_PATH_HOME\n$INSTALL_PATH_GLOBAL" && which -a g 2>/dev/null) \
| is_this_g
exit 0
}
# Escapes all / \ & characters in sed patterns
escape_pattern() {
<<< "$1" sed -e 's/[\/&]/\\&/g'
}
install_g() {
local LOC=$1
local FORCE=$2
mkdir -p "$LOC"
FOUND=$(find_installed)
if [[ -n "$FOUND" && -z "$FORCE" ]]; then
echo "Failed: g already exists at:" $FOUND >&2
echo " 1. Run \`./configure --uninstall\` to remove, OR" >&2
echo " 2. Use \`--force\` to overwrite" >&2
exit 2
fi
install -m "$SCRIPT_PERM" bin/g "$LOC/g"
install -m "$ALIASES_PERM" bin/g_aliases "$LOC/g_aliases"
# Format config strings for `g_aliases`
local G_BIN_DIR=$(escape_pattern "$LOC")
sed -i -e "s/AUTO_G_BIN_DIR/$G_BIN_DIR/g" "$LOC/g_aliases"
echo "Installed bin/g -> $LOC/g"
echo "Installed bin/g_aliases -> $LOC/g_aliases"
echo -e "\n[+] To use the aliases, add this to your ~/.bashrc or ~/.bash_aliases:"
echo "### source $LOC/g_aliases"
}
uninstall_g() {
local FOUND=""
while read -r LOC; do
local FOUND="1"
LOC=$(dirname "$LOC")
rm "$LOC/g" "$LOC/g_aliases"
rmdir "$LOC"
echo "Removed: $LOC/g"
echo "Removed: $LOC/g_aliases"
done < <(find_installed | uniq -u)
if [[ -z "$FOUND" ]]; then
echo "Failed: g was not found to be installed" >&2
exit 2
fi
echo -e "\nAliases are still bound!\nUse \`source ~/.bashrc\` or \`exec \`$SHELL\`"
}
INSTALL_PATH="$INSTALL_PATH_HOME"
FORCE=
for OPT in "$@"; do
case $OPT in
-g|--global)
INSTALL_PATH="$INSTALL_PATH_GLOBAL"
;;
-u|--uninstall)
uninstall_g
exit 0 # non-zero will already propagate from `set -e`
;;
-f|--force)
FORCE="1"
;;
*)
echo -e "Unknown option $OPT\n$USAGE" >&2
exit 1
;;
esac
done
install_g "$INSTALL_PATH" "$FORCE"