-
Notifications
You must be signed in to change notification settings - Fork 12
/
gh-zsh.sh
185 lines (176 loc) · 8.01 KB
/
gh-zsh.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
179
180
181
182
183
184
185
#!/usr/bin/env bash
#--------------------------------------------------
# Shell Configurations
#--------------------------------------------------
OS="$(uname)"
if [[ "$OS" == "Linux" ]] || [[ "$OS" == "Darwin" ]] ; then
echo
if [[ "$OS" == "Linux" ]]; then
echo -e "\n→ Installing zsh, bat, and git"
sudo apt install zsh bat git -y &> /dev/null
fi
if [[ "$OS" == "Darwin" ]]; then
# Inspired from https://github.com/Homebrew/brew
version_gt() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -gt "${2#*.}" ]]
}
version_ge() {
[[ "${1%.*}" -gt "${2%.*}" ]] || [[ "${1%.*}" -eq "${2%.*}" && "${1#*.}" -ge "${2#*.}" ]]
}
major_minor() {
echo "${1%%.*}.$(x="${1#*.}"; echo "${x%%.*}")"
}
macos_version="$(major_minor "$(/usr/bin/sw_vers -productVersion)")"
should_install_command_line_tools() {
if version_gt "$macos_version" "10.13"; then
! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]]
else
! [[ -e "/Library/Developer/CommandLineTools/usr/bin/git" ]] ||
! [[ -e "/usr/include/iconv.h" ]]
fi
}
if should_install_command_line_tools && version_ge "$macos_version" "10.13"; then
echo "→ When prompted for the password, enter your Mac login password."
shell_join() {
local arg
printf "%s" "$1"
shift
for arg in "$@"; do
printf " "
printf "%s" "${arg// /\ }"
done
}
chomp() {
printf "%s" "${1/"$'\n'"/}"
}
have_sudo_access() {
local -a args
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A")
elif [[ -n "${NONINTERACTIVE-}" ]]; then
args=("-n")
fi
}
have_sudo_access() {
local -a args
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A")
elif [[ -n "${NONINTERACTIVE-}" ]]; then
args=("-n")
fi
if [[ -z "${HAVE_SUDO_ACCESS-}" ]]; then
if [[ -n "${args[*]-}" ]]; then
SUDO="/usr/bin/sudo ${args[*]}"
else
SUDO="/usr/bin/sudo"
fi
if [[ -n "${NONINTERACTIVE-}" ]]; then
${SUDO} -l mkdir &>/dev/null
else
${SUDO} -v && ${SUDO} -l mkdir &>/dev/null
fi
HAVE_SUDO_ACCESS="$?"
fi
if [[ -z "${HOMEBREW_ON_LINUX-}" ]] && [[ "$HAVE_SUDO_ACCESS" -ne 0 ]]; then
abort "Need sudo access on macOS (e.g. the user $USER needs to be an Administrator)!"
fi
return "$HAVE_SUDO_ACCESS"
}
execute() {
if ! "$@"; then
abort "$(printf "Failed during: %s" "$(shell_join "$@")")"
fi
}
execute_sudo() {
local -a args=("$@")
if have_sudo_access; then
if [[ -n "${SUDO_ASKPASS-}" ]]; then
args=("-A" "${args[@]}")
fi
execute "/usr/bin/sudo" "${args[@]}"
else
execute "${args[@]}"
fi
}
TOUCH="/usr/bin/touch"
clt_placeholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
execute_sudo "$TOUCH" "$clt_placeholder"
clt_label_command="/usr/sbin/softwareupdate -l |
grep -B 1 -E 'Command Line Tools' |
awk -F'*' '/^ *\\*/ {print \$2}' |
sed -e 's/^ *Label: //' -e 's/^ *//' |
sort -V |
tail -n1"
clt_label="$(chomp "$(/bin/bash -c "$clt_label_command")")"
if [[ -n "$clt_label" ]]; then
printf "Xcode Command Line Tools not found\nInstalling...\n"
execute_sudo "/usr/sbin/softwareupdate" "-i" "$clt_label" &> /dev/null
execute_sudo "/bin/rm" "-f" "$clt_placeholder" &> /dev/null
execute_sudo "/usr/bin/xcode-select" "--switch" "/Library/Developer/CommandLineTools" &> /dev/null
fi
fi
# Inspired from https://github.com/Homebrew/brew
fi
echo -e "\nShell Configurations"
if [[ "$OS" == "Darwin" ]]; then
chsh -s /bin/zsh &> /dev/null
fi
if [[ "$OS" == "Linux" ]]; then
sudo usermod -s /usr/bin/zsh $(whoami) &> /dev/null
sudo usermod -s /usr/bin/zsh root &> /dev/null
fi
if mv -n ~/.zshrc ~/.zshrc-backup-$(date +"%Y-%m-%d") &> /dev/null; then
echo -e "\n → Backing up the current .zshrc config to .zshrc-backup-date"
fi
(cd ~/ && curl -O https://raw.githubusercontent.com/gustavohellwig/gh-zsh/main/.zshrc) &> /dev/null
echo "source \$HOME/.zsh/powerlevel10k/powerlevel10k.zsh-theme" >>~/.zshrc
echo "source \$HOME/.zsh/fast-syntax-highlighting/fast-syntax-highlighting.plugin.zsh" >> ~/.zshrc
echo "source \$HOME/.zsh/completion.zsh" >> ~/.zshrc
echo "source \$HOME/.zsh/zsh-autosuggestions/zsh-autosuggestions.zsh" >> ~/.zshrc
echo "source \$HOME/.zsh/history.zsh" >> ~/.zshrc
echo "source \$HOME/.zsh/key-bindings.zsh" >> ~/.zshrc
#--------------------------------------------------
# Theme Installation
#--------------------------------------------------
echo -e "\nTheme Installation"
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ~/.zsh/powerlevel10k &> /dev/null
(cd ~/ && curl -O https://raw.githubusercontent.com/gustavohellwig/gh-zsh/main/.p10k.zsh) &> /dev/null
# if [[ "$OS" == "Linux" ]]; then
# sudo cp /home/"$(whoami)"/.p10k.zsh /root/
#fi
#--------------------------------------------------
# Plugins Installations
#--------------------------------------------------
echo -e "\nPlugins Installations"
git clone https://github.com/zdharma-continuum/fast-syntax-highlighting.git ~/.zsh/fast-syntax-highlighting &> /dev/null
git clone https://github.com/zsh-users/zsh-autosuggestions.git ~/.zsh/zsh-autosuggestions &> /dev/null
(cd ~/.zsh/ && curl -O https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/lib/completion.zsh) &> /dev/null
(cd ~/.zsh/ && curl -O https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/lib/history.zsh) &> /dev/null
(cd ~/.zsh/ && curl -O https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/lib/key-bindings.zsh) &> /dev/null
if [[ "$OS" == "Linux" ]]; then
sudo cp -r /home/"$(whoami)"/.zshrc /root/
sudo cp -r /home/"$(whoami)"/.zsh /root/
sudo cp -r /home/"$(whoami)"/.p10k.zsh /root
fi
echo -e "\nInstallation Finished"
echo -e "\n→ You may need to reopen the terminal if the theme doesn't load automatically.\n"
# Inspired from: https://github.com/romkatv/zsh4humans/blob/v5/sc/exec-zsh-i
try_exec_zsh() {
>'/dev/null' 2>&1 command -v "$1" || 'return' '0'
<'/dev/null' >'/dev/null' 2>&1 'command' "$1" '-fc' '
[[ $ZSH_VERSION == (5.<8->*|<6->.*) ]] || return
exe=${${(M)0:#/*}:-$commands[$0]}
zmodload -s zsh/terminfo zsh/zselect || [[ $ZSH_PATCHLEVEL == zsh-5.8-0-g77d203f && $exe == */bin/zsh && -e ${exe:h:h}/share/zsh/5.8/scripts/relocate ]]' || 'return' '0'
'exec' "$@" || 'return'
}
exec_zsh() {
'try_exec_zsh' 'zsh' "$@" || 'return'
# 'try_exec_zsh' '/usr/local/bin/zsh' "$@" || 'return'
# 'try_exec_zsh' '/bin/zsh' "$@" || 'return'
}
'exec_zsh' '-i'
# Inspired from: https://github.com/romkatv/zsh4humans/blob/v5/sc/exec-zsh-i
else
echo "This script is only supported on macOS and Linux."
exit 0
fi