-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·98 lines (81 loc) · 2.86 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
get_root_repo_path() {
cd "$(dirname "${BASH_SOURCE[0]}")" && pwd
}
install() {
local startup_file="$1"
local bashrc_path="${HOME}/${startup_file}"
local timestamp
local root_repo_path=""
local bashrc_line=""
local enable_verbose_output_answer
local enable_exec_complete_answer
local enable_one_tab_completion_answer
local bind_show_all_if_unmodified=":"
declare -i has_backed_up=0
timestamp=$(date +%s)
if [ -z "${timestamp}" ]; then
echo "Failed to obtain a timestamp, abort installation." >&2
exit 1
fi
root_repo_path=$(get_root_repo_path)
if [ -z "${root_repo_path}" ]; then
echo "Failed to get the repository's root path where install.sh located, abort installation." >&2
exit 1
fi
if [[ -f "${bashrc_path}" ]]; then
{ while read -r bashrc_line; do
if [[ "${bashrc_line}" =~ ^[[:space:]]*\.[[:space:]]${root_repo_path}/src/completion\.sh ]]; then
echo "Yarn-2-completion has already been installed."
exit 0
fi
done; } <"${bashrc_path}"
if ! cp "${bashrc_path}" "${bashrc_path}.${timestamp}.bak"; then
echo "Failed to backup ${bashrc_path} to ${bashrc_path}.${timestamp}.bak, abort installation." >&2
exit 1
fi
has_backed_up=1
fi
echo "Type in your answer y(yes), n(no), or leave it empty to accept the default setting"
read -r -p $'\n'"Do you want to enable the completion when only typing one tab? (y/n, default: yes): " enable_one_tab_completion_answer
if [[ $enable_one_tab_completion_answer == 'n' ]]; then
enable_one_tab_completion_answer=
else
bind_show_all_if_unmodified="bind 'set show-all-if-unmodified on'"
fi
read -r -p $'\n'"Do you want to enable verbose output? (y/n, default: no): " enable_verbose_output_answer
read -r -p $'\n'"Do you want the yarn-2-competion complete \"yarn exec\" for you? (y/n, default: yes): " enable_exec_complete_answer
if [[ $enable_verbose_output_answer == 'y' ]]; then
enable_verbose_output_answer=1
else
enable_verbose_output_answer=
fi
if [[ $enable_exec_complete_answer == 'n' ]]; then
enable_exec_complete_answer=0
else
enable_exec_complete_answer=
fi
cat <<END >>"${HOME}/${startup_file}"
# Beginning of yarn-2-completion's configurations
. ${root_repo_path}/src/completion.sh
${bind_show_all_if_unmodified} # yarn-2-completion readline settings
export Y2C_VERBOSE=${enable_verbose_output_answer:-0}
export Y2C_SYSTEM_EXECUTABLE_BY_PATH_ENV=${enable_exec_complete_answer:-1}
# End of yarn-2-completion's configurations
END
echo $'\n'"Install successfully. Open the new terminal to activate yarn-2-completion."
if [[ $has_backed_up -eq 1 ]]; then
echo "The original ${startup_file} has been backed up to ${bashrc_path}.${timestamp}.bak"
fi
}
main() {
case "${OSTYPE}" in
darwin*)
install ".bash_profile"
;;
*)
install ".bashrc"
;;
esac
}
main