1+ #! /usr/bin/env bash
12_venv_dir=" /usr/local/share/venv"
23
34set -e
@@ -8,13 +9,35 @@ post() {
89 sudo rm -r " ${_venv_dir} " || true
910}
1011
12+ backup_rcfile () {
13+ local rcfile=" $1 "
14+
15+ echo -e " # This backup file was created by venv-cli during uninstall" > " ${rcfile} .old"
16+ cat " ${rcfile} " >> " ${rcfile} .old"
17+ echo " Created backup of ${rcfile} at ${rcfile} .old"
18+ }
19+
20+ remove_source_lines () {
21+ # Remove the 'source' lines and the comment above it
22+ local source_lines_to_remove=$(( "$1 " - 1 ))
23+ local rcfile=" $2 "
24+
25+ # Remove a specific number of lines from the rc file.
26+ # The result is then redirected to a .tmp rc file, which is then moved to the original rc file.
27+ local delete_pattern=" ,+${source_lines_to_remove} d"
28+ sed " \|\# Source function script for 'venv' command|${delete_pattern} " < " ${rcfile} " > " ${rcfile} .tmp"
29+ mv " ${rcfile} .tmp" " ${rcfile} "
30+ }
31+
1132uninstall_common () {
1233 local rcfile=" $1 "
1334 local completion_target=" $2 "
35+ local lines_to_remove=" $3 "
1436
15- # Remove the line from shell config that sources the script
16- sed -i " \|.*Source autocompletions for 'venv' command|d" " ${HOME} /.bashrc"
17- sed -i " \|\. ${_venv_dir} /venv|d" " ${HOME} /.bashrc"
37+ if command grep -q " ${_venv_dir} /venv" " ${rcfile} " ; then
38+ backup_rcfile " ${rcfile} "
39+ remove_source_lines " ${lines_to_remove} " " ${rcfile} "
40+ fi
1841
1942 if [ -f " ${completion_target} " ]; then
2043 sudo rm " ${completion_target} "
@@ -24,17 +47,21 @@ uninstall_common() {
2447uninstall_bash () {
2548 local rcfile=" ${HOME} /.bashrc"
2649 local completion_target=" /usr/share/bash-completion/completions/venv"
50+ local rcfile_source_lines=2
2751
28- uninstall_common " ${rcfile} " " ${completion_target} "
52+ uninstall_common " ${rcfile} " " ${completion_target} " " ${rcfile_source_lines} "
53+
54+ echo " venv command and completions removed from bash"
2955}
3056
3157uninstall_zsh () {
3258 local rcfile=" ${HOME} /.zshrc"
3359 local completion_target=" /usr/local/share/zsh/site-functions/_venv"
60+ local rcfile_source_lines=4
61+
62+ uninstall_common " ${rcfile} " " ${completion_target} " " ${rcfile_source_lines} "
3463
35- uninstall_common " ${rcfile} " " ${completion_target} "
36- sed -i " \|fpath+=( ${_install_dir} )|d" " ${HOME} /.zshrc"
37- sed -i " \|autoload -Uz venv|d" " ${HOME} /.zshrc"
64+ echo " venv command and completions removed from zsh"
3865}
3966
4067main () {
0 commit comments