fix: Fix module stuff
This commit is contained in:
+35
-17
@@ -1,10 +1,10 @@
|
|||||||
std::bootstrap() {
|
std::bootstrap() {
|
||||||
[ -n "${STD_VERSION:-}" ] &&
|
[ -n "${STD_VERSION:-}" ] && [ "${STD_VERSION:-}" != "$1" ] &&
|
||||||
std::panic "\`std\` ($STD_VERSION) already loaded, can\'t load version $1"
|
std::panic "\`std\` ($STD_VERSION) already loaded, can't load version $1"
|
||||||
|
|
||||||
declare -g STD_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=$(ps -p $$ | awk '$1 != "PID" {print $(NF)}')
|
||||||
: "${INTERPRETER:=bash}"
|
: "${INTERPRETER:=bash}"
|
||||||
@@ -52,46 +52,64 @@ std::sensible() {
|
|||||||
esac
|
esac
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# shellcheck disable=SC2016
|
||||||
std::mod::get-self-path() {
|
std::mod::get-self-path() {
|
||||||
local interpreter
|
local interpreter
|
||||||
interpreter=$(std::interpreter::name)
|
interpreter=$(std::interpreter::name)
|
||||||
case $interpreter in
|
case $interpreter in
|
||||||
bash)
|
bash)
|
||||||
realpath -e "$(dirname "${BASH_SOURCE[0]}")"
|
echo 'realpath -e "$(dirname "${BASH_SOURCE[0]}")"'
|
||||||
;;
|
;;
|
||||||
zsh)
|
zsh)
|
||||||
eval 'realpath -e "$(dirname "${(%):-%x}")"'
|
echo 'realpath -e "$(dirname "${(%):-%x}")"'
|
||||||
;;
|
;;
|
||||||
ksh)
|
ksh)
|
||||||
# untested, nevermind, ksh doesn't even support functions with :: in them
|
# 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
|
echo '
|
||||||
exit 1
|
echo "Not sure how to get sourced path for $interpreter" >&2
|
||||||
|
exit 1
|
||||||
|
'
|
||||||
;;
|
;;
|
||||||
esac
|
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_versions
|
||||||
declare -A std__module_paths
|
declare -A std__module_paths
|
||||||
std::mod::init() {
|
std::mod::init() {
|
||||||
local module_name=${1?Expected a module name}
|
local module_name module_version module_path
|
||||||
local module_version=${2?Expected a module version}
|
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
|
if [ -n "${std__module_versions[$module_name]:-}" ] && [ "${std__module_versions[$module_name]:-}" != "$module_version" ]; then
|
||||||
modpath=$(std::mod::get-self-path)
|
|
||||||
|
|
||||||
if [ -n "${std__module_versions[$module_name]:-}" ]; then
|
|
||||||
std::panic "$module_name (${std__module_versions[$module_name]}) already initialized" \
|
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
|
fi
|
||||||
|
|
||||||
std__module_versions[$module_name]=$module_version
|
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() {
|
std::mod::get-path() {
|
||||||
local module_name=${1?Expected a module name}
|
local module_name=${1?Expected a module name}
|
||||||
|
|||||||
Reference in New Issue
Block a user