Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pip Auto Reshim Support #136

Merged
merged 2 commits into from
Jul 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions shims/pip
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#! /usr/bin/env bash

# This script wraps pip to run `asdf reshim` after installs and uninstalls.
# Any other cases are passed-through to pip.
#
# Inspired by the npm shim: https://github.com/asdf-vm/asdf-nodejs/blob/b2d06a768d9a14186db72/shims/npm

set -euo pipefail

this_dir=$(dirname "${BASH_SOURCE[0]}")
this_dir=$(cd "$this_dir" && pwd -P) # Normalizes the directory; see https://stackoverflow.com/a/7666/2308068
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() {
local pip_location="${ASDF_PYTHON_CANON_PIP_PATH:-$(search_pip_bin)}"

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"
}

search_pip_bin() {
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 "$@"