-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·182 lines (159 loc) · 5.09 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
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
#!/usr/bin/env bash
# Configuration script to symlink the dotfiles or clean up the symlinks.
# The script should take a target flag stating whether "build" or "clean". The
# first option will symlink all of the dotfiles and attempt to install
# oh-my-zsh. Otherwise, the script will simply remove all symlinks.
usage="Usage: $0 [-h] [-t <build|clean|shellcheck>]"
BUILD=true
# Run the symlinking from the repo root.
cd "$(dirname "$0")" || exit
while getopts :ht: option; do
case "${option}" in
h)
echo "${usage}"
echo
echo "OPTIONS"
echo "-h Output verbose usage message"
echo "-t build Set up dotfile symlinks and configure oh-my-zsh"
echo "-t clean Remove all existing dotfiles symlinks"
echo "-t shellcheck Lint all shell scripts"
exit;;
t)
if [[ "build" =~ ^${OPTARG} ]]; then
BUILD=true
elif [[ "clean" =~ ^${OPTARG} ]]; then
BUILD=
elif [[ "shellcheck" =~ ^${OPTARG} ]]; then
shopt -s globstar
shellcheck -x -- **/*.sh
exit $?
else
echo "${usage}" >&2
exit 1
fi;;
\?)
echo "Unknown option: -${OPTARG}" >&2
exit 1;;
:)
echo "Missing argument for -${OPTARG}" >&2
exit 1;;
esac
done
execute() {
cmd=$1
msg=$2
eval "${cmd}"
if [[ $? ]]; then
# Print output in green.
printf "\e[0;32m [✔] %s\e[0m\n" "${msg}"
else
# Print output in red.
printf "\e[0;31m [✖] %s\e[0m\n" "${msg}"
fi
}
install_omz() {
ZSH=${HOME}/.oh-my-zsh
ZSH_CUSTOM="${ZSH_CUSTOM:-${ZSH}/custom}"
# Clone or update Oh My Zsh.
if [[ ! -d "${ZSH}" ]]; then
git clone --quiet --filter=blob:none https://github.com/robbyrussell/oh-my-zsh "${ZSH}"
else
git -C "${ZSH}" pull --quiet
fi
# Clone or update Powerlevel10k.
THEME_REPO_URL="https://github.com/romkatv/powerlevel10k"
THEME_PATH="${ZSH_CUSTOM}/themes/${THEME_REPO_URL##*/}"
THEME_VERSION_TAG="master"
if [[ -x "$(command -v jq)" ]]; then
THEME_VERSION_TAG=$(curl -s https://api.github.com/repos/romkatv/powerlevel10k/releases/latest | jq -r .tag_name)
fi
if [[ ! -d "${THEME_PATH}" ]]; then
git clone --quiet --filter=blob:none --branch "${THEME_VERSION_TAG}" "${THEME_REPO_URL}" "${THEME_PATH}"
else
git -C "${THEME_PATH}" fetch --quiet
git -C "${THEME_PATH}" checkout "${THEME_VERSION_TAG}" --quiet
fi
# Install or update custom oh-my-zsh plugins.
CUSTOM_PLUGIN_REPOS=(
"https://github.com/Aloxaf/fzf-tab"
"https://github.com/zdharma-continuum/fast-syntax-highlighting"
"https://github.com/zsh-users/zsh-autosuggestions"
)
for REPO_URL in "${CUSTOM_PLUGIN_REPOS[@]}"; do
PLUGIN_PATH="${ZSH_CUSTOM}/plugins/${REPO_URL##*/}"
if [[ ! -d "${PLUGIN_PATH}" ]]; then
git clone --quiet --filter=blob:none "${REPO_URL}" "${PLUGIN_PATH}"
else
git -C "${PLUGIN_PATH}" pull --quiet
fi
done
}
link_file() {
local source=$1
local target=$2
# We've already symlinked, do nothing.
if [[ "$(readlink "${target}")" == "${source}" ]]; then
return
fi
# If the target location exists and it's not our target symlink, we create a backup.
if [[ -e "${target}" ]]; then
epoch=$(date +%s)
execute "mv ${target} ${target}.${epoch}.bak" "Backing up ${target} → ${target}.${epoch}.bak"
fi
# Symlink the dotfile.
execute "ln -fs ${source} ${target}" "Linking ${target} → ${source}"
}
unlink_file() {
local source=$1
local target=$2
if [ "$(readlink "${target}")" == "${source}" ]; then
execute "unlink ${target}" "Unlinking ${target} → ${source}"
fi
}
main() {
# Symlink (or unlink) the dotfiles.
# Technically, this won't work for odd filenames, e.g. those with spaces or
# newlines. However, we don't care in this case and would rather have broader
# compatibility.
#
# shellcheck disable=SC2207
FILES_TO_SYMLINK=($(find home -mindepth 1 -maxdepth 1 -type f))
for dotfile in "${FILES_TO_SYMLINK[@]}"; do
sourceFile="$(pwd)/${dotfile}"
targetFile="${HOME}/.$(basename "${dotfile}")"
if [[ $BUILD ]]; then
link_file "$sourceFile" "$targetFile"
else
unlink_file "$sourceFile" "$targetFile"
fi
done
# Symlink (or unlink) folders in the ~/.config directory.
mkdir -p "${HOME}/.config"
# shellcheck disable=SC2207
FOLDERS_TO_SYMLINK=($(find config -mindepth 1 -maxdepth 1 -type d))
for configFolder in "${FOLDERS_TO_SYMLINK[@]}"; do
sourceFolder="$(pwd)/$configFolder"
targetFolder="${HOME}/.config/$(basename "${configFolder}")"
if [[ $BUILD ]]; then
link_file "$sourceFolder" "$targetFolder"
else
unlink_file "$sourceFolder" "$targetFolder"
fi
done
if [[ $BUILD ]]; then
# Link gitconfig.
touch ~/.gitconfig
# Add a single empty line to the file, because the sed command below expects at least
# one line to be present
echo > ~/.gitconfig
sed -i '1i\
[include]\n\
path=~/.main.gitconfig\n' ~/.gitconfig
# Install oh-my-zsh and its custom plugins/themes.
install_omz
else
# Unlink gitconfig.
git config --global --unset include.path
fi
}
main "$@"