1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-11-13 20:29:02 -08:00

overhaul script sourcing and dependency system

This commit is contained in:
Connor Olding 2021-08-02 13:48:46 -07:00
parent 752dc2a605
commit 7ae8c5681d
68 changed files with 115 additions and 88 deletions

View file

@ -37,13 +37,14 @@ ADDPATH "$HOME/sh"
(
cd ~/sh \
&& printf "%s\n" '#!/usr/bin/env false' '[ "${SOURCING:-0}" -gt 0 ] || exit 1' '' \
&& printf "%s\n" '#!/usr/bin/env false' '[ -n "$preload" ] || exit 1' '' \
| cat - $(grep -lF 'YES_BASH' $(find ~/sh -maxdepth 1 -type f)) \
> ~/.sh-bash
)
: $((SOURCING+=1))
dummy() { :; } ### @- return 0, ignoring arguments.
preload=dummy
. ~/.sh-bash
: $((SOURCING-=1))
unset preload
alias reload='cd; exec bash' ### @- **TODO:** respect initctl like in `.zshrc`.

View file

@ -51,7 +51,7 @@ function {
[ -s ~/.sh.sha1 ] && read -r b _ < ~/.sh.sha1
( cd ~/sh \
&& print -l '#!/usr/bin/env false' '[ "${SOURCING:-0}" -gt 0 ] || exit 1' '' \
&& print -l '#!/usr/bin/env false' '[ -n "$preload" ] || exit 1' '' \
| cat - $(grep -lF 'YES_ZSH' *(.)) \
| tee ~/.sh \
) | sha1sum - \
@ -61,9 +61,10 @@ function {
[ "$a" = "$b" ] && touch ~/.sh.zwc || zrecompile -p ~/.sh
}
: $((SOURCING+=1))
dummy() { :; } ### @- return 0, ignoring arguments.
preload=dummy
. ~/.sh
: $((SOURCING-=1))
unset preload
local fuzzy=`has fzy || print`

View file

@ -59,4 +59,4 @@ argc() { ### @- validate the number of arguments in a function.
return 0
}
[ "${SOURCING:-0}" -gt 0 ] || argc "$@"
[ -n "${preload+-}" ] || argc "$@"

View file

@ -20,4 +20,4 @@ arith() { ### @-
printf "%s\n" "$(($@))"
}
[ "${SOURCING:-0}" -gt 0 ] || arith "$@"
[ -n "${preload+-}" ] || arith "$@"

2
sh/aur
View file

@ -136,4 +136,4 @@ aur() { ### @-
[ "$fail" -eq 0 ] && return 0 || return 1
}
[ "${SOURCING:-0}" -gt 0 ] || aur "$@"
[ -n "${preload+-}" ] || aur "$@"

View file

@ -17,4 +17,4 @@ autosync() { ### @-
done
}
[ "${SOURCING:-0}" -gt 0 ] || autosync "$@"
[ -n "${preload+-}" ] || autosync "$@"

2
sh/bak
View file

@ -30,4 +30,4 @@ bak() { ### @-
return $ret
}
[ "${SOURCING:-0}" -gt 0 ] || bak "$@"
[ -n "${preload+-}" ] || bak "$@"

View file

@ -19,4 +19,4 @@ baknow() { ### @-
cp -ip "${1:?missing file argument}" "$1.$(now "$1").bak"
}
[ "${SOURCING:-0}" -gt 0 ] || baknow "$@"
[ -n "${preload+-}" ] || baknow "$@"

View file

@ -4,10 +4,6 @@
# YES_BASH
# YES_DASH
#: $((SOURCING+=1))
#. ~/sh/note # FIXME: don't do this?
#: $((SOURCING-=1))
baks() { ### @-
### backup files by copying each and appending *the current* date-time,
### irrespective of when the files were modified or created.
@ -36,4 +32,6 @@ baks() { ### @-
return $ret
}
[ "${SOURCING:-0}" -gt 0 ] || baks "$@"
preload() { local preload='set -- $@'; while [ $# -gt 0 ]; do . ~/sh/$1 || exit 2; shift; done; }
eval ${preload:-preload} note
[ -n "${preload+-}" ] || baks "$@"

2
sh/bin
View file

@ -20,4 +20,4 @@ bin() { ### @-
printf "%08X\n" "$a"
}
[ "${SOURCING:-0}" -gt 0 ] || bin "$@"
[ -n "${preload+-}" ] || bin "$@"

View file

@ -17,4 +17,4 @@ cdbusiest() { ### @-
cd $d
}
[ "${SOURCING:-0}" -gt 0 ] || cdbusiest "$@"
[ -n "${preload+-}" ] || cdbusiest "$@"

View file

@ -13,4 +13,4 @@ colors() { ### @-
printf "\e[%dm$(printf " \e[%dm mV \e[40m" {4,10}{0..7})\e[0m\n" {3,9}{0..7}
}
[ "${SOURCING:-0}" -gt 0 ] || colors "$@"
[ -n "${preload+-}" ] || colors "$@"

View file

@ -22,4 +22,4 @@ compandy() { ### @-
$atk $atk $rel $rel $thresh $thresh $end $knee $gain $start $delay
}
[ "${SOURCING:-0}" -gt 0 ] || compandy "$@"
[ -n "${preload+-}" ] || compandy "$@"

View file

@ -359,4 +359,4 @@ compile() { ### @-
$compiler $std ${final_flags[@]} $file ${libraries[@]} ${warnings[@]} $outflag $out
}
[ "${SOURCING:-0}" -gt 0 ] || compile "$@"
[ -n "${preload+-}" ] || compile "$@"

View file

@ -45,4 +45,4 @@ else
}
fi
[ "${SOURCING:-0}" -gt 0 ] || confirm "$@"
[ -n "${preload+-}" ] || confirm "$@"

View file

@ -19,4 +19,4 @@ countdiff() { ### @-
| awk '/changed/{print $4+$6;a=1}END{if(!a)print 0}'
}
[ "${SOURCING:-0}" -gt 0 ] || countdiff "$@"
[ -n "${preload+-}" ] || countdiff "$@"

View file

@ -27,4 +27,4 @@ cutv() { ### @-
# -maxrate 9600k -bufsize 12000k -vcodec libx264 -preset medium -crf 22 -vf scale=1280:720
}
[ "${SOURCING:-0}" -gt 0 ] || cutv "$@"
[ -n "${preload+-}" ] || cutv "$@"

View file

@ -14,4 +14,4 @@ days() { ### @-
echo $(( ($(date +%s) - $(date -d "$1" +%s)) / 60 / 60 / 24 ))
}
[ "${SOURCING:-0}" -gt 0 ] || days "$@"
[ -n "${preload+-}" ] || days "$@"

View file

@ -24,4 +24,4 @@ dbusiest() { ### @-
[ -z $d ] && return 1
}
[ "${SOURCING:-0}" -gt 0 ] || dbusiest
[ -n "${preload+-}" ] || dbusiest "$@"

2
sh/dfu
View file

@ -19,4 +19,4 @@ dfu() { ### @-
NR>1{printf"%-20s %7.2f %7.2f %7.2f %7.2f\n",$6,$3/2^20,($3+$4)/2^20,($4)/2^20,($2-$4-$3)/2^20}'
}
[ "${SOURCING:-0}" -gt 0 ] || dfu
[ -n "${preload+-}" ] || dfu "$@"

View file

@ -6,7 +6,7 @@
# TODO: actually test that this works with dash.
function disf() { ### @-
disf() { ### @-
### disassemble a single function from an unstripped executable, unreliably.
[ $# -le 2 ] || { printf "%s\n" "$0: too many arguments" >&2; return 1; }
local exe="${1:?first argument should be an unstripped executable}"
@ -15,4 +15,4 @@ function disf() { ### @-
objdump -d -C --no-show-raw-insn "$exe" | sed '/<'"$symbol"'>:/,/^$/!d;//d'
}
[ "${SOURCING:-0}" -gt 0 ] || disf "$@"
[ -n "${preload+-}" ] || disf "$@"

View file

@ -164,4 +164,4 @@ document() { ### @-
esac
}
[ "${SOURCING:-0}" -gt 0 ] || document "$@"
[ -n "${preload+-}" ] || document "$@"

2
sh/e
View file

@ -61,4 +61,4 @@ e() { ### @-
fi
}
[ "${SOURCING:-0}" -gt 0 ] || e "$@"
[ -n "${preload+-}" ] || e "$@"

2
sh/ea
View file

@ -46,4 +46,4 @@ ea() { ### @- **TODO:** document.
esac
}
[ "${SOURCING:-0}" -gt 0 ] || ea "$@"
[ -n "${preload+-}" ] || ea "$@"

View file

@ -17,4 +17,4 @@ echo2() { ### @-
printf "%s\n" "$*" >&2
}
[ "${SOURCING:-0}" -gt 0 ] || echo2 "$@"
[ -n "${preload+-}" ] || echo2 "$@"

View file

@ -9,4 +9,4 @@ explore() { ### @- open a single directory in `explorer.exe`, defaulting to `$PW
explorer "$(cygpath -w "${1:-.}")"
}
[ "${SOURCING:-0}" -gt 0 ] || explore "$@"
[ -n "${preload+-}" ] || explore "$@"

2
sh/ff
View file

@ -10,4 +10,4 @@ ff() { ### @-
find "${1:-.}" -type f | fzy
}
[ "${SOURCING:-0}" -gt 0 ] || ff "$@"
[ -n "${preload+-}" ] || ff "$@"

View file

@ -28,4 +28,4 @@ ghmd() { ### @-
return $ret
}
[ "${SOURCING:-0}" -gt 0 ] || ghmd "$@"
[ -n "${preload+-}" ] || ghmd "$@"

2
sh/has
View file

@ -16,4 +16,4 @@ has() { ### @-
fi
}
[ "${SOURCING:-0}" -gt 0 ] || has "$@"
[ -n "${preload+-}" ] || has "$@"

2
sh/hex
View file

@ -21,4 +21,4 @@ hex() { ### @-
printf "%08X\n" "$(($@))"
}
[ "${SOURCING:-0}" -gt 0 ] || hex "$@"
[ -n "${preload+-}" ] || hex "$@"

2
sh/ify
View file

@ -19,4 +19,4 @@ ify() { ### @-
"$@" | "$ex"
}
[ "${SOURCING:-0}" -gt 0 ] || ify "$@"
[ -n "${preload+-}" ] || ify "$@"

View file

@ -13,4 +13,4 @@ is_empty() { ### @-
return 0
}
[ "${SOURCING:-0}" -gt 0 ] || is_empty "$@"
[ -n "${preload+-}" ] || is_empty "$@"

View file

@ -16,4 +16,4 @@ isup() { ### @-
[ -z "${c#2[0-9][0-9]}" ]
}
[ "${SOURCING:-0}" -gt 0 ] || isup "$@"
[ -n "${preload+-}" ] || isup "$@"

View file

@ -136,4 +136,4 @@ maybesudo_() { ### @-
# don't put any code here or you'll clobber $?.
}
[ "${SOURCING:-0}" -gt 0 ] || maybesudo_ "$@"
[ -n "${preload+-}" ] || maybesudo_ "$@"

View file

@ -40,4 +40,4 @@ minutemaid() { ### @-
fi
}
[ "${SOURCING:-0}" -gt 0 ] || minutemaid "$@"
[ -n "${preload+-}" ] || minutemaid "$@"

View file

@ -47,4 +47,4 @@ monitor() { ### @-
done
}
[ "${SOURCING:-0}" -gt 0 ] || monitor "$@"
[ -n "${preload+-}" ] || monitor "$@"

View file

@ -53,4 +53,4 @@ EOF
perl ~/opt/local/bin/noccom "$@"
}
[ "${SOURCING:-0}" -gt 0 ] || noccom "$@"
[ -n "${preload+-}" ] || noccom "$@"

View file

@ -12,4 +12,4 @@ note() { ### @-
printf "\033[1m%s\033[0m\n" "$*" >&2
}
[ "${SOURCING:-0}" -gt 0 ] || note "$@"
[ -n "${preload+-}" ] || note "$@"

2
sh/now
View file

@ -53,4 +53,4 @@ now() { ### @-
echo "${F}_${ms}"
}
[ "${SOURCING:-0}" -gt 0 ] || now "$@"
[ -n "${preload+-}" ] || now "$@"

View file

@ -18,4 +18,4 @@ pacbm() { ### @-
' | sort -n
}
[ "${SOURCING:-0}" -gt 0 ] || pacbm "$@"
[ -n "${preload+-}" ] || pacbm "$@"

View file

@ -17,4 +17,4 @@ pause() { ### @-
'
}
[ "${SOURCING:-0}" -gt 0 ] || pause "$@"
[ -n "${preload+-}" ] || pause "$@"

View file

@ -19,4 +19,4 @@ pegg() { ### @-
pippy "https://github.com/$1/$2/tarball/${3:-master}#egg=$2"
}
[ "${SOURCING:-0}" -gt 0 ] || pegg "$@"
[ -n "${preload+-}" ] || pegg "$@"

View file

@ -12,4 +12,4 @@ pippy() { ### @-
"$sudo" py -m pip install --upgrade --upgrade-strategy only-if-needed "$@"
}
[ "${SOURCING:-0}" -gt 0 ] || pippy "$@"
[ -n "${preload+-}" ] || pippy "$@"

2
sh/pre
View file

@ -26,4 +26,4 @@ pre() { ### @-
fi
}
[ "${SOURCING:-0}" -gt 0 ] || pre "$@"
[ -n "${preload+-}" ] || pre "$@"

32
sh/preload Normal file
View file

@ -0,0 +1,32 @@
#!/usr/bin/env false
# NO_ZSH
# NO_BASH
# NO_DASH
# NO_ASH
# NOTE: this is actually compatible with all of the above shells;
# i just really don't want it being sourced by accident.
# this file really only exists for documentation purposes.
preload() { ### @- handle dependencies within the [`~/sh/`](/sh) directory.
### this function contains more comments than code, so you should read it.
# all sourced files from within the preload function will instead
# append to the list of files to preload (i.e. dependencies).
# dash and ash have no arrays besides $@, so we must use it.
local preload='set -- $@'
# we want to iterate over $@ like a queue; pushing and popping.
# `for` would create a local copy and leave $@ unmodified, so it's no good.
while [ $# -gt 0 ]; do
# NOTE: there's no quotes here since the surrounding method uses eval,
# so paths-with-spaces is already doomed from the start.
# however, $HOME can still safely contain spaces.
# NOTE: it's important to use an absolute path here.
. ~/sh/$1 # yes, you can source files from within functions,
# and they even respect your locals.
[ $? -eq 0 ] \
|| exit 2 # this `exit 2` is just for zsh and bash;
# dash and ash do it automatically.
# zsh actually uses some other return value,
# but we use 2 anyway since everything else uses 2.
shift # pop the queue
done
}

View file

@ -18,4 +18,4 @@ psbm() { ### @-
' | sort -n
}
[ "${SOURCING:-0}" -gt 0 ] || psbm "$@"
[ -n "${preload+-}" ] || psbm "$@"

View file

@ -13,4 +13,4 @@ randir() { ### @-
find -maxdepth 1 -type d \( -path '/root' -prune -o -print \) | tail -n+2 | shuf | head -n1
}
[ "${SOURCING:-0}" -gt 0 ] || randir "$@"
[ -n "${preload+-}" ] || randir "$@"

View file

@ -7,4 +7,4 @@ refresh() { ### @- invoke `hash -r`.
hash -r
}
[ "${SOURCING:-0}" -gt 0 ] || refresh "$@"
[ -n "${preload+-}" ] || refresh "$@"

View file

@ -16,4 +16,4 @@ rot13() { ### @-
tr -- A-Za-z0-9 N-ZA-Mn-za-m5-90-4
}
[ "${SOURCING:-0}" -gt 0 ] || rot13 "$@"
[ -n "${preload+-}" ] || rot13 "$@"

2
sh/rs
View file

@ -72,4 +72,4 @@ rs() { ### @-
$o_overwrite "$extless.enc.$ext"
}
[ "${SOURCING:-0}" -gt 0 ] || rs "$@"
[ -n "${preload+-}" ] || rs "$@"

8
sh/sc
View file

@ -1,9 +1,5 @@
#!/usr/bin/env bash
: $((SOURCING+=1))
. ~/sh/ea # FIXME: don't do this? somehow?
: $((SOURCING-=1))
sc_shorten() {
REPLY="${1:2:2}${1:5:2}${1:8:2}${1:11:12}"
}
@ -66,4 +62,6 @@ sc() { ### @-
return 0
}
[ "${SOURCING:-0}" -gt 0 ] || sc "$@"
preload() { local preload='set -- $@'; while [ $# -gt 0 ]; do . ~/sh/$1 || exit 2; shift; done; }
eval ${preload:-preload} ea
[ -n "${preload+-}" ] || sc "$@"

View file

@ -18,4 +18,4 @@ scramble() { ### @-
-e 's/@//g'
}
[ "${SOURCING:-0}" -gt 0 ] || scramble "$@"
[ -n "${preload+-}" ] || scramble "$@"

View file

@ -17,4 +17,4 @@ screeny() { ### @-
return 0
}
[ "${SOURCING:-0}" -gt 0 ] || screeny "$@"
[ -n "${preload+-}" ] || screeny "$@"

View file

@ -1,10 +1,6 @@
#!/usr/bin/env bash
# YES_ZSH
#: $((SOURCING+=1))
#. ~/sh/now # FIXME: don't do this?
#: $((SOURCING-=1))
scropt() { ### @-
### run `scrot` through `optipng` and save the result to `~/play/$(now).png`.
###
@ -18,4 +14,6 @@ scropt() { ### @-
[ $? -eq 0 ] && echo "$fn" || return $?
}
[ "${SOURCING:-0}" -gt 0 ] || scropt "$@"
preload() { local preload='set -- $@'; while [ $# -gt 0 ]; do . ~/sh/$1 || exit 2; shift; done; }
eval ${preload:-preload} now
[ -n "${preload+-}" ] || scropt "$@"

View file

@ -20,4 +20,4 @@ similar() { ### @-
}'
}
[ "${SOURCING:-0}" -gt 0 ] || similar "$@"
[ -n "${preload+-}" ] || similar "$@"

View file

@ -12,4 +12,4 @@ slit() { ### @-
#awk "{ print ${(j:,:):-\$${^@}} }"
}
[ "${SOURCING:-0}" -gt 0 ] || slit "$@"
[ -n "${preload+-}" ] || slit "$@"

View file

@ -15,4 +15,4 @@ slitt() { ### @-
#awk "BEGIN { FS=\"\\t\"; OFS=\"\\t\" } { print ${(j:,:):-\$${^@}} }"
}
[ "${SOURCING:-0}" -gt 0 ] || slitt "$@"
[ -n "${preload+-}" ] || slitt "$@"

View file

@ -49,4 +49,4 @@ sram() { ### @-
revend "$out"
}
[ "${SOURCING:-0}" -gt 0 ] || sram "$@"
[ -n "${preload+-}" ] || sram "$@"

View file

@ -1,9 +1,6 @@
#!/usr/bin/env sh
# YES_ZSH
#. ~/sh/echo2 # FIXME
#. ~/sh/note # FIXME
stfu() { ### @-
### invoke a command, silencing stdout and stderr *unless* the command fails.
###
@ -44,8 +41,8 @@ stfu() { ### @-
### WARNING: The system cannot find the file specified.
### asses
### ```
[ $# -gt 0 ] || { printf "%s\n" "$0: too few arguments" >&2; return 1; }
local temp="${TMP:-/tmp}/stfu"
local time="$(date -u '+%s')"
@ -77,4 +74,6 @@ stfu() { ### @-
return $ret
}
[ "${SOURCING:-0}" -gt 0 ] || stfu "$@"
preload() { local preload='set -- $@'; while [ $# -gt 0 ]; do . ~/sh/$1 || exit 2; shift; done; }
eval ${preload:-preload} echo2 note
[ -n "${preload+-}" ] || stfu "$@"

2
sh/sum
View file

@ -20,4 +20,4 @@ sum() { ### @-
echo "$sum"
}
[ "${SOURCING:-0}" -gt 0 ] || sum "$@"
[ -n "${preload+-}" ] || sum "$@"

2
sh/sv
View file

@ -36,4 +36,4 @@ sv() { ### @-
'
}
[ "${SOURCING:-0}" -gt 0 ] || sv "$@"
[ -n "${preload+-}" ] || sv "$@"

View file

@ -11,4 +11,4 @@ tpad() { ### @-
done
}
[ "${SOURCING:-0}" -gt 0 ] || tpad "$@"
[ -n "${preload+-}" ] || tpad "$@"

View file

@ -14,4 +14,4 @@ trash() { ### @-
dd status=none if=/dev/random bs=1 count="${1:?missing count argument}"
}
[ "${SOURCING:-0}" -gt 0 ] || trash "$@"
[ -n "${preload+-}" ] || trash "$@"

View file

@ -18,4 +18,4 @@ trunc() { ### @-
awk -vL=${1:-$COLUMNS} '{e=length>L?"…":"";print substr($0,0,L-(e?1:0)) e}'
}
[ "${SOURCING:-0}" -gt 0 ] || trunc "$@"
[ -n "${preload+-}" ] || trunc "$@"

View file

@ -14,4 +14,4 @@ unscreen() { ### @-
return 0
}
[ "${SOURCING:-0}" -gt 0 ] || unscreen "$@"
[ -n "${preload+-}" ] || unscreen "$@"

2
sh/wat
View file

@ -37,4 +37,4 @@ wat() { ### @-
)
}
[ "${SOURCING:-0}" -gt 0 ] || wat "$@"
[ -n "${preload+-}" ] || wat "$@"

View file

@ -21,4 +21,4 @@ wipe() { ### @-
echo $'\033[0m\n' | tr -d '$'
}
[ "${SOURCING:-0}" -gt 0 ] || wipe "$@"
[ -n "${preload+-}" ] || wipe "$@"

2
sh/xxp
View file

@ -20,4 +20,4 @@ xxp() { ### @-
hexdump -e '15 1 "%02X " 1 1 " %02X\n"' -- "$@"
}
[ "${SOURCING:-0}" -gt 0 ] || xxp "$@"
[ -n "${preload+-}" ] || xxp "$@"