asdf-python/shims/pip

67 lines
1.3 KiB
Text
Raw Permalink Normal View History

2022-05-02 14:49:16 +01:00
#! /usr/bin/env bash
2022-05-02 14:50:08 +01:00
2022-05-02 14:49:16 +01:00
# This script wraps pip to run `asdf reshim` after installs and uninstalls.
# Any other cases are passed-through to pip.
#
2022-05-02 14:50:08 +01:00
# Inspired by the npm shim: https://github.com/asdf-vm/asdf-nodejs/blob/b2d06a768d9a14186db72/shims/npm
2022-05-02 14:49:16 +01:00
set -euo pipefail
this_dir=$(dirname "${BASH_SOURCE[0]}")
2022-05-02 14:50:08 +01:00
this_dir=$(cd "$this_dir" && pwd -P) # Normalizes the directory; see https://stackoverflow.com/a/7666/2308068
2022-05-02 14:49:16 +01:00
plugin_name=$(basename "$(dirname "$this_dir")")
should_reshim() {
if [ "${ASDF_PYTHON_SKIP_RESHIM:-}" ]; then
return 1
fi
for arg; do
case "$arg" in
install|uninstall)
return 0
;;
esac
done
return 1
}
resolve_pip() {
2022-05-02 14:50:08 +01:00
local pip_location="${ASDF_PYTHON_CANON_PIP_PATH:-$(search_pip_bin)}"
2022-05-02 14:49:16 +01:00
if ! [ "$pip_location" ]; then
echo "asdf-python couldn't find a suitable pip executable"
echo "This is probably a problem with the plugin, please report this issue"
exit 1
fi
echo "$pip_location"
}
2022-05-02 14:50:08 +01:00
search_pip_bin() {
2022-05-02 14:49:16 +01:00
local probably_pip="$(asdf where python)/bin/pip"
if [ -x "$probably_pip" ]; then
echo "$probably_pip"
return 0
fi
return 1
}
wrap_pip() {
local pip=$(resolve_pip)
if should_reshim "$@"; then
"$pip" "$@"
echo "Reshimming asdf $plugin_name..."
asdf reshim "$plugin_name"
else
exec "$pip" "$@"
fi
}
wrap_pip "$@"
2022-05-02 14:50:08 +01:00