Skip to content

Commit

Permalink
Update shell config file after installation (bash & zsh)
Browse files Browse the repository at this point in the history
Fix #112
  • Loading branch information
ianchen-tw committed Dec 25, 2022
1 parent 49fba59 commit 6cad90a
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 2 deletions.
5 changes: 3 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ Uninstall:
.. code:: bash
rm -fr ~/.pyenv
then remove these three lines from ``.bashrc``:
then remove these three lines from your shell config files
(``.bashrc``, ``.zshrc``, ``.profile``...):

.. code:: bash
Expand Down
61 changes: 61 additions & 0 deletions bin/pyenv-installer
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ if [ -z "$PYENV_ROOT" ]; then
export PYENV_ROOT="${HOME}/.pyenv"
fi

if [ -z "$shell" ]; then
shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
shell="${shell%% *}"
shell="${shell##-}"
shell="${shell:-$SHELL}"
shell="${shell##*/}"
shell="${shell%%-*}"
fi

USER_PATH_ADDED=false

colorize() {
if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
else echo -n "$2"
Expand Down Expand Up @@ -50,7 +61,57 @@ checkout "${GITHUB}/pyenv/pyenv-update.git" "${PYENV_ROOT}/plugins/pyenv-upd
checkout "${GITHUB}/pyenv/pyenv-virtualenv.git" "${PYENV_ROOT}/plugins/pyenv-virtualenv" "master"
checkout "${GITHUB}/pyenv/pyenv-which-ext.git" "${PYENV_ROOT}/plugins/pyenv-which-ext" "master"

write_source() {
# expand ~ to user's home
local target="${1/#\~/$HOME}"

echo "Update file: $1" >&2

echo "" >>$target
echo '# pyenv' >>$target
echo 'export PATH="'"$PYENV_ROOT/bin:"'$PATH"' >>$target
echo 'eval "$(pyenv init -)"' >>$target
echo 'eval "$(pyenv virtualenv-init -)"' >>$target

USER_PATH_ADDED=true
}

add_userpath() {
# Will detect rc files by setting variables below:
# PYENV_SHELL_DETECT
# PYENV_PROFILE_DETECT
# PYENV_RC_DETECT
eval "$(${PYENV_ROOT}/bin/pyenv init --detect-shell $shell)"

case "$PYENV_SHELL_DETECT" in
bash )
write_source $PYENV_PROFILE_DETECT
write_source $PYENV_RC_DETECT
;;
zsh )
write_source $PYENV_PROFILE_DETECT
write_source $PYENV_RC_DETECT
;;
ksh )
write_source $PYENV_PROFILE_DETECT
;;
* )
{
echo "Add userpath for $PYENV_SHELL_DETECT is not currently supported"
echo "Please set up manually"
} >&2
;;
esac
}

if ! command -v pyenv 1>/dev/null; then

add_userpath
if ${USER_PATH_ADDED}; then
echo "Restart your shell to start using pyenv" >&2
exit 0
fi

{ echo
colorize 1 "WARNING"
echo ": seems you still have not added 'pyenv' to the load path."
Expand Down
59 changes: 59 additions & 0 deletions bin/pyenv-offline-installer
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ if [ -z "$PYENV_ROOT" ]; then
PYENV_ROOT="${HOME}/.pyenv"
fi

if [ -z "$shell" ]; then
shell="$(ps -p "$PPID" -o 'args=' 2>/dev/null || true)"
shell="${shell%% *}"
shell="${shell##-}"
shell="${shell:-$SHELL}"
shell="${shell##*/}"
shell="${shell%%-*}"
fi

USER_PATH_ADDED=false

colorize() {
if [ -t 1 ]; then printf "\e[%sm%s\e[m" "$1" "$2"
else echo -n "$2"
Expand Down Expand Up @@ -60,8 +71,56 @@ conditional_mv "$TMP_DIR/pyenv-which-ext" "${PYENV_ROOT}/plugins/pyenv-which-ex

rm -rf $TMP_DIR

write_source() {
# expand ~ to user's home
local target="${1/#\~/$HOME}"

echo "Update file: $1" >&2

echo "" >>$target
echo '# pyenv' >>$target
echo 'export PATH="'"$PYENV_ROOT/bin:"'$PATH"' >>$target
echo 'eval "$(pyenv init -)"' >>$target
echo 'eval "$(pyenv virtualenv-init -)"' >>$target

USER_PATH_ADDED=true
}

add_userpath() {
# Will detect rc files by setting variables below:
# PYENV_SHELL_DETECT
# PYENV_PROFILE_DETECT
# PYENV_RC_DETECT
eval "$(${PYENV_ROOT}/bin/pyenv init --detect-shell $shell)"

case "$PYENV_SHELL_DETECT" in
bash )
write_source $PYENV_PROFILE_DETECT
write_source $PYENV_RC_DETECT
;;
zsh )
write_source $PYENV_PROFILE_DETECT
write_source $PYENV_RC_DETECT
;;
ksh )
write_source $PYENV_PROFILE_DETECT
;;
* )
{
echo "Add userpath for $PYENV_SHELL_DETECT is not currently supported"
echo "Please set up manually"
} >&2
;;
esac
}

if ! command -v pyenv 1>/dev/null; then
add_userpath
if ${USER_PATH_ADDED}; then
echo "Restart your shell to start using pyenv" >&2
exit 0
fi

{ echo
colorize 1 "WARNING"
echo ": seems you still have not added 'pyenv' to the load path."
Expand Down

0 comments on commit 6cad90a

Please sign in to comment.