@@ -9,13 +9,35 @@ post() {
99 sudo rm -r " ${_venv_dir} " || true
1010}
1111
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+
1232uninstall_common () {
1333 local rcfile=" $1 "
1434 local completion_target=" $2 "
35+ local lines_to_remove=" $3 "
1536
16- # Remove the line from shell config that sources the script
17- sed -i " \|.*Source autocompletions for 'venv' command|d" " ${rcfile} "
18- sed -i " \|\. ${_venv_dir} /venv|d" " ${rcfile} "
37+ if command grep -q " ${_venv_dir} /venv" " ${rcfile} " ; then
38+ backup_rcfile " ${rcfile} "
39+ remove_source_lines " ${lines_to_remove} " " ${rcfile} "
40+ fi
1941
2042 if [ -f " ${completion_target} " ]; then
2143 sudo rm " ${completion_target} "
@@ -25,17 +47,21 @@ uninstall_common() {
2547uninstall_bash () {
2648 local rcfile=" ${HOME} /.bashrc"
2749 local completion_target=" /usr/share/bash-completion/completions/venv"
50+ local rcfile_source_lines=2
2851
29- uninstall_common " ${rcfile} " " ${completion_target} "
52+ uninstall_common " ${rcfile} " " ${completion_target} " " ${rcfile_source_lines} "
53+
54+ echo " venv command and completions removed from bash"
3055}
3156
3257uninstall_zsh () {
3358 local rcfile=" ${HOME} /.zshrc"
3459 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} "
3563
36- uninstall_common " ${rcfile} " " ${completion_target} "
37- sed -i " \|fpath+=( ${_install_dir} )|d" " ${rcfile} "
38- sed -i " \|autoload -Uz venv|d" " ${rcfile} "
64+ echo " venv command and completions removed from zsh"
3965}
4066
4167main () {
0 commit comments