-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathinstall
executable file
·224 lines (191 loc) · 5.16 KB
/
install
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash
# External Environment variables that affect behaviour:
# - DOTFILES_OVERWRITE_REPLY
# Set to one of ynq as the reply to use in case of overwriting files.
# y: yes
# n: no
# q: quit
# - DOTFILES_SKIP_VIM_PLUGINS
# Set to 1 to prevent VIM from opening and installing all plugins, as
# can be a lengthy process.
# - DOTFILES_CHANGES_REPORT
# If set, the count of changed files will be written to this file
DOTFILES_OVERWRITE_REPLY=
DOTFILES_SKIP_VIM_PLUGINS=
DOTFILES_CHANGES_REPORT=
INSTALLER_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
THIS_SCRIPT="$(basename "$0")"
declare -a CHANGED_FILES
## Download all vim plug-ins
get_vim_plugins()
{
echo "Installing Vim Plugins"
vim +PlugUpgrade +qa || { echo 'Error upgrading Plug. Will continue.'; }
if [[ $DOTFILES_SKIP_VIM_PLUGINS -eq 1 ]]
then
echo "Skipping Vim plugins installation"
return
fi
vim +PlugInstall +qa || { echo 'Error installing vim plugins'; return 1; }
}
## Run xmodmap with the customisations in ~/.Xmodmap
apply_xmodmap()
{
if [[ -e ~/.Xmodmap ]] && has_program xmodmap; then
echo "Applying custom key bindings"
xmodmap ~/.Xmodmap || true
fi
}
post_install_actions()
{
get_vim_plugins
if sys::is_linux; then
apply_xmodmap
fi
}
install_files_from()
{
local src_dir="$1"
local dest_dir="$2"
shift 2
# Include the platform directories in the excluded list, as they are
# treated separately below.
local -a excluded=("$@" _linux _macos)
replace_all=false
cd "$src_dir" || die "Could not change to $src_dir"
local file
for file in *; do
if ! element_exists "$file" "${excluded[@]}"; then
local dest_file_name
dest_file_name=$(basename "$file" .tpl)
local dest_file_path="$dest_dir/.$dest_file_name"
install_file "$file" "$dest_file_path"
fi
done
# Install platform specific files
if [[ -d "$src_dir/_linux" ]] && sys::is_linux; then
install_files_from "$src_dir/_linux" "$dest_dir" "${excluded[@]}"
fi
if [[ -d "$src_dir/_macos" ]] && sys::is_macos; then
install_files_from "$src_dir/_macos" "$dest_dir" "${excluded[@]}"
fi
}
# Install the dot files into user's home directory
install_home()
{
local -a excluded=("$THIS_SCRIPT" README.mkd README README.md LICENSE TODO.mkd)
install_files_from "$INSTALLER_DIR" "$HOME" "$THIS_SCRIPT" "${excluded[@]}"
post_install_actions
echo "Installation complete. Changed files: ${#CHANGED_FILES[@]}"
if [[ -n $DOTFILES_CHANGES_REPORT ]]; then
echo "${#CHANGED_FILES[@]}" > "$DOTFILES_CHANGES_REPORT"
fi
}
overwrite_confirmation()
{
local dest=$1
if [[ -n $DOTFILES_OVERWRITE_REPLY ]]
then
REPLY=$DOTFILES_OVERWRITE_REPLY
else
read -r -p "overwrite ${dest}? [ynaq] "
fi
}
## Install a file (with replacement confirmation)
install_file()
{
local file="$1"
local dest_file_path="$2"
if [[ -e $dest_file_path ]] || \
[[ -L $dest_file_path ]] # Can be a broken symlink
then
if [[ -e $dest_file_path ]] &&
[[ $(readlink -f "$file") == $(readlink -f "$dest_file_path") ]]
then
echo "identical $dest_file_path"
elif $replace_all
then
replace_file "$file" "$dest_file_path"
else
overwrite_confirmation "${dest_file_path}"
case $REPLY in
'y') replace_file "$file" "$dest_file_path" ;;
'q') exit 1 ;;
'a') replace_all=true
replace_file "$file" "$dest_file_path" ;;
*) echo "skipping $dest_file_path" ;;
esac
fi
else
link_file "$file" "$dest_file_path"
fi
}
replace_file()
{
local file="$1"
local dest_file_path="$2"
backup "$dest_file_path"
link_file "$file" "$dest_file_path"
}
link_file()
{
local file="$1"
local dest_file_path="$2"
if [[ $file =~ \.tpl$ ]]
then
local temp_file
temp_file=$(mktemp /tmp/dotfiles.XXXXXX)
echo "generating $dest_file_path"
if ! ( source "$file" > "$temp_file" ); then
echo "Error generating $dest_file_path"
rm "$temp_file"
return 1
fi
mv "$temp_file" "$dest_file_path"
else
echo "linking $dest_file_path"
ln -s "$(pwd)/$file" "$dest_file_path"
fi
CHANGED_FILES+=("$dest_file_path")
}
element_exists()
{
local elem="$1"
shift
local arr=( "$@" )
local i
for i in "${arr[@]}"
do
if [[ $i == "$elem" ]]
then
return 0
fi
done
return 1
}
backup()
{
local file="$1"
local backup_suffix
backup_suffix="bak.$(date "+%Y%m%d%H%M%S")"
local backup_file="${file}.${backup_suffix}"
mv "$file" "$backup_file"
echo "backed up $file to $backup_file"
}
has_program()
{
type -t "$1" > /dev/null
}
die()
{
echo "$*" >&2
exit 1
}
sys::is_linux() {
[[ $(uname -s) == Linux ]]
}
sys::is_macos() {
[[ $(uname -s) == Darwin ]]
}
# Install all configuration files
install_home