1
0
Fork 0
mirror of https://github.com/notwa/rc synced 2024-05-20 02:43:23 -07: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 "$@"