fix: Fix module stuff

This commit is contained in:
2026-06-22 22:18:20 +02:00
parent 7615da7fb2
commit c34ab49bef
+35 -17
View File
@@ -1,10 +1,10 @@
std::bootstrap() {
[ -n "${STD_VERSION:-}" ] &&
std::panic "\`std\` ($STD_VERSION) already loaded, can\'t load version $1"
[ -n "${STD_VERSION:-}" ] && [ "${STD_VERSION:-}" != "$1" ] &&
std::panic "\`std\` ($STD_VERSION) already loaded, can't load version $1"
declare -g STD_VERSION=$1
}
std::bootstrap 0.1.3
std::bootstrap 0.2.0
INTERPRETER=$(ps -p $$ | awk '$1 != "PID" {print $(NF)}')
: "${INTERPRETER:=bash}"
@@ -52,46 +52,64 @@ std::sensible() {
esac
}
# shellcheck disable=SC2016
std::mod::get-self-path() {
local interpreter
interpreter=$(std::interpreter::name)
case $interpreter in
bash)
realpath -e "$(dirname "${BASH_SOURCE[0]}")"
echo 'realpath -e "$(dirname "${BASH_SOURCE[0]}")"'
;;
zsh)
eval 'realpath -e "$(dirname "${(%):-%x}")"'
echo 'realpath -e "$(dirname "${(%):-%x}")"'
;;
ksh)
# untested, nevermind, ksh doesn't even support functions with :: in them
eval 'realpath -e "$(dirname "${.sh.file}")"'
echo 'realpath -e "$(dirname "${.sh.file}")"'
;;
*)
echo "Not sure how to get sourced path for $interpreter" >&2
exit 1
echo '
echo "Not sure how to get sourced path for $interpreter" >&2
exit 1
'
;;
esac
}
## Backtrace alternative to `${FOO?Msg}`
std::unwrap() {
if [ -z "$1" ]; then
std::panic "$2"
fi
echo "$1"
}
##
# Should be called like:
#
# `std::mod::init mylib 0.1.0 "$(eval "$(std::mod::get-self-path)")"` in a file
# immediately within `src/`
##
declare -A std__module_versions
declare -A std__module_paths
std::mod::init() {
local module_name=${1?Expected a module name}
local module_version=${2?Expected a module version}
local module_name module_version module_path
module_name=$(std::unwrap "${1:-}" "Expected a module name")
module_version=$(std::unwrap "${2:-}" "Expected a module version")
module_path=$(std::unwrap "${3:-}" "Expected a module path")
local modpath
modpath=$(std::mod::get-self-path)
if [ -n "${std__module_versions[$module_name]:-}" ]; then
if [ -n "${std__module_versions[$module_name]:-}" ] && [ "${std__module_versions[$module_name]:-}" != "$module_version" ]; then
std::panic "$module_name (${std__module_versions[$module_name]}) already initialized" \
"from $modpath, when ${std__module_paths[$module_name]} existed"
$'\n'" from $module_path ($module_version), when ${std__module_paths[$module_name]} existed"
fi
std__module_versions[$module_name]=$module_version
std__module_paths[$module_name]=$modpath
std__module_paths[$module_name]=$module_path
}
std::mod::init std "$STD_VERSION"
std::mod::init std "$STD_VERSION" \
"$(eval "$(std::mod::get-self-path)")"
std::mod::get-path() {
local module_name=${1?Expected a module name}