forked from MrPickles/dotfiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure.sh
executable file
·203 lines (172 loc) · 4.7 KB
/
configure.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/bin/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>]"
if [[ "$#" -lt 1 ]]; then
echo "$usage"
exit
fi
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"
exit;;
t)
if [[ "build" =~ ^${OPTARG} ]]; then
BUILD=true
elif [[ "clean" =~ ^${OPTARG} ]]; then
BUILD=
else
echo "$usage" >&2
exit 1
fi;;
\?)
echo "Unknown option: -$OPTARG" >&2
exit 1;;
:)
echo "Missing argument for -$OPTARG" >&2
exit 1;;
esac
done
declare -a FILES_TO_SYMLINK=(
'editor/vim'
'editor/vimrc'
'git/gitattributes'
'git/gitconfig'
'git/gitignore'
'shell/ignore'
'shell/tmux.conf'
'shell/zshrc'
'shell/antigen.zsh'
'shell/ripgreprc'
'third_party/Gdbinit/gdbinit'
'third_party/zsh-interactive-cd/zsh-interactive-cd.plugin.zsh'
)
declare -a FULL_PATH_FILES_TO_SYMLINK=(
'config/nvim/init.vim'
)
print_success() {
if [[ $BUILD ]]; then
# Print output in green
printf "\e[0;32m [✔] %s\e[0m\n" "$1"
else
# Print output in cyan
printf "\e[0;36m [✔] Unlinked %s\e[0m\n" "$1"
fi
}
print_error() {
if [[ $BUILD ]]; then
# Print output in red
printf "\e[0;31m [✖] %s %s\e[0m\n" "$1" "$2"
else
# Print output in red
printf "\e[0;31m [✖] Failed to unlink %s %s\e[0m\n" "$1" "$2"
fi
}
print_question() {
# Print output in yellow
printf "\e[0;33m [?] %s\e[0m" "$1"
}
execute() {
$1 &> /dev/null
print_result $? "${2:-$1}"
}
print_result() {
if [ "$1" -eq 0 ]; then
print_success "$2"
else
print_error "$2"
fi
[ "$3" == "true" ] && [ "$1" -ne 0 ] && exit
}
ask_for_confirmation() {
print_question "$1 [y/N] "
read -r -n 1
printf "\n"
}
answer_is_yes() {
[[ "$REPLY" =~ ^[Yy]$ ]] \
&& return 0 \
|| return 1
}
link_file() {
local sourceFile=$1
local targetFile=$2
if [ ! -e "$targetFile" ]; then
execute "ln -fs $sourceFile $targetFile" "$targetFile → $sourceFile"
elif [ "$(readlink "$targetFile")" == "$sourceFile" ]; then
print_success "$targetFile → $sourceFile"
else
ask_for_confirmation "'$targetFile' already exists, do you want to overwrite it?"
if answer_is_yes; then
rm -rf "$targetFile"
execute "ln -fs $sourceFile $targetFile" "$targetFile → $sourceFile"
else
print_error "$targetFile → $sourceFile"
fi
fi
}
unlink_file() {
local sourceFile=$1
local targetFile=$2
if [ "$(readlink "$targetFile")" == "$sourceFile" ]; then
execute "unlink $targetFile" "$targetFile"
fi
}
ensure_homebrew() {
which -s brew
if [[ $? != 0 ]] ; then
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
else
brew update
fi
}
# Install basics using Brewfile
ensure_homebrew
brew bundle install
# Symlink (or unlink) the dotfiles.
for i in "${FILES_TO_SYMLINK[@]}"; do
sourceFile="$(pwd)/$i"
targetFile="$HOME/.$(printf "%s" "$i" | sed "s/.*\/\(.*\)/\1/g")"
if [[ $BUILD ]]; then
link_file $sourceFile $targetFile
else
unlink_file $sourceFile $targetFile
fi
done
for i in "${FULL_PATH_FILES_TO_SYMLINK[@]}"; do
sourceFile="$(pwd)/$i"
targetFile="$HOME/.$i"
if [[ $BUILD ]]; then
mkdir -p $(dirname $targetFile)
link_file $sourceFile $targetFile
else
unlink_file $sourceFile $targetFile
fi
done
if [[ $BUILD ]]; then
# Set up diff-so-fancy.
if [[ "$(command -v diff-so-fancy)" ]]; then
git config --global pager.diff "diff-so-fancy | less --tabs=4 -RFX"
git config --global pager.show "diff-so-fancy | less --tabs=4 -RFX"
git config --global color.ui true
git config --global color.diff-highlight.oldNormal "red"
git config --global color.diff-highlight.oldHighlight "red 52"
git config --global color.diff-highlight.newNormal "green"
git config --global color.diff-highlight.newHighlight "green 22"
git config --global color.diff.meta "yellow"
git config --global color.diff.frag "magenta"
git config --global color.diff.commit "yellow"
git config --global color.diff.old "red"
git config --global color.diff.new "green"
git config --global color.diff.whitespace "red reverse"
fi
fi