Skip to content

Commit 2438f17

Browse files
authored
Merge branch 'release/v2.0.0' into feature/remove-file-stem-constraints
2 parents 0624caa + a23dd79 commit 2438f17

File tree

2 files changed

+65
-21
lines changed

2 files changed

+65
-21
lines changed

install.sh

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
#!/usr/bin/env bash
12
_src_dir="${PWD}/src/venv-cli"
23
_install_dir="/usr/local/share/venv"
4+
_source_comment="# Source function script for 'venv' command"
35

46
set -e
57

@@ -17,9 +19,13 @@ install_common() {
1719
sudo cp "${completion_file}" "${completion_target}"
1820
fi
1921

20-
# Append line to the shell config file to source the script
21-
echo -e "\n# Source function script for 'venv' command" >> "${rcfile}"
22-
echo ". ${_install_dir}/venv" >> "${rcfile}"
22+
# If line does not already exist in the rc file,
23+
# append line to the shell config file to source the venv-cli script
24+
if ! command grep -q "${_source_comment}" "${rcfile}"; then
25+
echo "${_source_comment}" >> "${rcfile}"
26+
echo "source ${_install_dir}/venv" >> "${rcfile}"
27+
fi
28+
2329
{ set +x; } 2>/dev/null
2430
}
2531

@@ -39,21 +45,32 @@ install_zsh() {
3945
echo "Command completions currently not supported for zsh"
4046

4147
install_common "${rcfile}"
42-
echo "fpath+=( ${_install_dir} )" >> "${rcfile}"
43-
echo "autoload -Uz venv" >> "${rcfile}"
48+
if ! command grep -q "${_source_comment}" "${rcfile}"; then
49+
echo "fpath+=( ${_install_dir} )" >> "${rcfile}"
50+
echo "autoload -Uz venv" >> "${rcfile}"
51+
fi
4452
}
4553

4654
main() {
47-
if [ -n "${ZSH_VERSION}" ]; then
48-
install_zsh
49-
elif [ "${SHELL}" = "/bin/bash" ]; then
50-
install_bash
51-
else
52-
echo "Current shell is not supported, aborting..."
53-
return 1
54-
fi
55+
local shell="$1"
56+
57+
case "${shell}" in
58+
"" | "bash")
59+
install_bash
60+
;;
61+
62+
"zsh")
63+
install_zsh
64+
;;
65+
66+
*)
67+
echo "No install script available for '${shell}'."
68+
echo "Use a different shell or install manually by adding 'source venv-cli/src/venv-cli/venv-cli.sh' to your shell's RC-file"
69+
return 1
70+
;;
71+
esac
5572

5673
echo "venv installed"
5774
}
5875

59-
main
76+
main "$@"

src/venv-cli/uninstall.sh

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#!/usr/bin/env bash
12
_venv_dir="/usr/local/share/venv"
23

34
set -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+
1132
uninstall_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() {
2447
uninstall_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

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

4067
main() {

0 commit comments

Comments
 (0)