Skip to content

Commit d4e30e3

Browse files
committed
Fix #27: Running uninstall.sh does not remove comment line from rc files
1 parent cea2875 commit d4e30e3

File tree

1 file changed

+33
-7
lines changed

1 file changed

+33
-7
lines changed

src/venv-cli/uninstall.sh

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
1232
uninstall_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() {
2547
uninstall_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

3257
uninstall_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

4167
main() {

0 commit comments

Comments
 (0)