-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.sh
executable file
·178 lines (160 loc) · 5.45 KB
/
setup.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#!/usr/bin/env bash
# Exit on any error
set -e
if [[ -n "${MYENV_DEBUG}" ]]; then
set -x
fi
# Use this session to url when using curl wget to avoid caching
MYENV_SESSION_TIME="$(date +%s)"
# ENV: fileDirUrl
if [ -z "${fileDirUrl}" ]; then
fileDirUrl="https://raw.githubusercontent.com/vleedev/myenv/main"
fi
# ENV: gitRepoUrl
if [ -z "${gitRepoUrl}" ]; then
gitRepoUrl="https://github.com/vleedev/myenv.git"
fi
# The method to download and echo to current screen
myenv_setup_663358564_load_script_url(){
echo "Please install curl or wget first"
return 1
}
# Detect download tool
for downloadTool in "curl" "wget"; do
if command -v "${downloadTool}" >/dev/null 2>&1; then
# Override the myenv_setup_663358564_load_script_url method
myenv_setup_663358564_load_script_url(){
local url="${1-}"
# The option --fail or --no-clobber will treat error if a request is a http error code
local options="--fail -sSL" # Default option for curl
if [[ "${downloadTool}" == "wget" ]]; then
options="--no-clobber -O-"
fi
eval "${downloadTool} ${options} ${url}"
}
break
fi
done
# Detect package manager first
# Source the stdout, ignore stderr
# shellcheck source=detection_scripts/package_manager.sh
source <(myenv_setup_663358564_load_script_url "${fileDirUrl}/detection_scripts/package_manager.sh?${MYENV_SESSION_TIME}" 2>/dev/null)
if [[ -z "${MYENV_PACKAGE_MANAGER}" ]]; then
echo "Package manager is not found"
exit 1
fi
# Load utils
# Source the stdout, ignore stderr
# shellcheck source=package_managers/[package_manager].sh
source <(myenv_setup_663358564_load_script_url "${fileDirUrl}/package_managers/${MYENV_PACKAGE_MANAGER}.sh?${MYENV_SESSION_TIME}" 2>/dev/null)
# Install git
myenv_package_managers_632264331_install git
# Install zsh
myenv_package_managers_632264331_install zsh
# Install python3
myenv_package_managers_632264331_install python3
# Install python3-pip
myenv_package_managers_632264331_install python3-pip
# Install pipx
if [ "$(command -v pip)" ]; then
if ! pip install --user pipx; then
if ! python3 -m pip install --user pipx; then
myenv_package_managers_632264331_install pipx
fi
fi
fi
# Install ansible
if [ "$(command -v pipx)" ]; then
install_ansible(){
local install_loop_count
install_loop_count=1
while true; do
if pipx install --include-deps ansible; then
break
fi
if [[ ${install_loop_count} -gt 2 ]]; then
break
fi
# Increase install_loop_count
((install_loop_count++))
done
}
install_ansible
fi
USER_DIR="$(echo ~)"
# make zsh as default shell for the user
if [ "${SHELL}" != "$(which zsh)" ]; then
zsh_path=$(which zsh)
# Check /etc/shells list
if ! grep "${zsh_path}" < "/etc/shells"; then
echo "${zsh_path}" | sudo tee -a "/etc/shells" > /dev/null
fi
# Set default
if command -v "chsh" >/dev/null 2>&1;
then
sudo chsh -s "${zsh_path}" ${USER}
elif command -v "usermod" >/dev/null 2>&1;
then
sudo usermod -s "${zsh_path}" ${USER}
else
echo "your current system is not supported to set zsh as default shell"
exit 2
fi
# Check user shell
if ! grep "^${USER}:" < "/etc/passwd" | grep "$(which zsh)"; then
echo "your shell is not changed"
exit 2
fi
fi
# Install oh-my-zsh
if [ -d "${USER_DIR}/.oh-my-zsh" ]; then
rm -rf "${USER_DIR}/.oh-my-zsh"
fi
yes Y | bash -c "$(myenv_setup_663358564_load_script_url https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
# Install plugin zsh-autosuggestions
zsh_autosuggestions_dir="${USER_DIR}/.oh-my-zsh/custom/plugins/zsh-autosuggestions"
if [ -d "${zsh_autosuggestions_dir}" ]; then
rm -rf "${zsh_autosuggestions_dir}"
fi
# Clone zsh-autosuggestions git repo
git clone https://github.com/zsh-users/zsh-autosuggestions "${zsh_autosuggestions_dir}"
# Check zsh-autosuggestions configuration in zshrc
ZSHRC_PATH="${USER_DIR}/.zshrc"
if ! grep "zsh-autosuggestions" "${ZSHRC_PATH}" >/dev/null 2>&1; then
# Add to plugins before loading oh-my-zsh
line_num=$(grep -nE '^source .+oh-my-zsh\.sh' "${ZSHRC_PATH}" | sed 's/[^0-9]//g')
sed -i "${line_num} i plugins+=(zsh-autosuggestions)" "${ZSHRC_PATH}"
fi
# Set ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE='fg=22'
sed -i 's/fg=[0-9]*/fg=22/g' "${zsh_autosuggestions_dir}/src/config.zsh"
# Clone myenv
# Default dir will be ${USER_DIR}/.myenv
# If you want to change it, set MYENV_DIR before running this script
if [[ -z "${MYENV_DIR}" ]]; then
MYENV_DIR="${USER_DIR}/.myenv"
fi
if ! git clone "${gitRepoUrl}" "${MYENV_DIR}"; then
echo "Can not clone ${gitRepoUrl} to ${MYENV_DIR}"
exit 1
else
echo "Clone ${gitRepoUrl} to ${MYENV_DIR}: ok"
fi
if ! grep "${MYENV_DIR}/import.sh" "${ZSHRC_PATH}" >/dev/null 2>&1; then
# Add to plugins before loading oh-my-zsh
line_num=$(grep -nE '^source .+oh-my-zsh\.sh' "${ZSHRC_PATH}" | sed 's/[^0-9]//g')
sed -i "${line_num} i plugins+=(zsh-autosuggestions)" "${ZSHRC_PATH}"
fi
# Add myenv to zsh
grep "${MYENV_DIR}/import.sh" "${ZSHRC_PATH}" >/dev/null 2>&1 || tee -a "${ZSHRC_PATH}" >/dev/null 2>&1 <<EOF
# Load customize configuration myenv
source "${MYENV_DIR}/import.sh"
EOF
# Restart shell session when the first input is not "test"
if [[ "${1}" != "test" ]]; then
# Replace the current shell session by creating the new shell session
exec "$(which zsh)"
else
# Simulate the way when zsh creates a new shell session
# It should be gone with option: -c with simple action
grep -qE 'Current process ID:.+[0-9]+' <(zsh -i -c 'echo ok')
fi