forked from CodelyTV/dotly
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinstaller
executable file
·403 lines (348 loc) · 11.9 KB
/
installer
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/usr/bin/env bash
set -euo pipefail
##? Setups the environment
##?
##? Usage:
##? install
ICLOUD_PATH="$HOME/Library/Mobile\ Documents/com~apple~CloudDocs/"
IS_ICLOUD_DOTFILES=false
DOTLY_REPOSITORY=${DOTLY_REPOSITORY:-gtrabanco/sloth}
DOTLY_BRANCH=${DOTLY_BRANCH:-main}
DOTLY_LOG_FILE=${DOTLY_LOG_FILE:-$HOME/dotly.log}
export DOTLY_ENV=${DOTLY_ENV:-PROD}
export DOTLY_INSTALLER=true
red='\033[0;31m'
green='\033[0;32m'
purple='\033[0;35m'
normal='\033[0m'
_w() {
local -r text="${1:-}"
echo -e "$text"
}
_a() { _w " > ${*}"; }
_e() { _a "${red}${*}${normal}"; }
_s() { _a "${green}${*}${normal}"; }
_q() {
if [[ "${DOTLY_ENV:-PROD}" == "CI" ]]; then
echo "🤔 ${*} : "
eval "$2='CI'"
else
echo -n "🤔 ${*}: "
read -r "$2"
fi
}
_yq() {
if [[ "${DOTLY_ENV:-PROD}" != "CI" ]]; then
_q "${*} [Y/n]"
read -r "$2"
else
echo -n "🤔 $*: "
echo y
eval "${2}=Y"
fi
}
_is_y() { [[ "${1:Y}" =~ ^[Yy] ]]; }
_log() {
log_name="$1"
current_date=$(date "+%Y-%m-%d %H:%M:%S")
touch "$DOTLY_LOG_FILE"
echo "----- $current_date - $log_name -----" >> "$DOTLY_LOG_FILE"
while IFS= read -r log_message; do
echo "$log_message" >> "$DOTLY_LOG_FILE"
done
echo "" >> "$DOTLY_LOG_FILE"
}
current_timestamp() { date +%s; }
create_dotfiles_dir() {
if [ -d "$1" ]; then
local -r backup_path="$1.$(current_timestamp).back"
_e "The path '$1' already exist"
_s "Creating a backup in '$backup_path'"
mv "$1" "$backup_path"
else
_a "Ok! dotfiles will be located in: ${purple}$DOTFILES_PATH${normal}"
fi
mkdir -p "$1"
}
command_exists() {
type "$1" > /dev/null 2>&1
}
is_macos() {
[[ "$(uname -s)" == Darwin ]]
}
is_clt_installed() {
command -vp xcode-select > /dev/null 2>&1 && xpath=$(command -p xcode-select --print-path) && test -d "${xpath}" && test -x "${xpath}"
}
install_clt() {
is_clt_installed && return
local -r placeholder="/tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress"
command -p touch "$placeholder" # Force softwareupdate to list CLT
local -r clt_label="$(command -p softwareupdate -l | grep -B 1 -E 'Command Line Tools' | awk -F '*' '/^ *\\*/ {print $2}' | sed -e 's/^ *Label: //' -e 's/^ *//' | sort -V -r |
head -n1)"
if ! command -p sudo -v -B; then
_e "Can not be installed without sudo authentication first"
return 1
fi
if [[ -n "$clt_label" ]]; then
command -p sudo "$(command -vp softwareupdate)" -i "$clt_label"
command -p sudo "$(command -vp xcode-select)" --switch "/Library/Developer/CommandLineTools"
fi
# Remove the placeholder always
/bin/rm -f "$placeholder"
# Something was terriby wrong with the CLT installation, so we need to try with another method
if ! is_clt_installed && command -p sudo -v -B; then
command -p xcode-select --install
if [[ "${DOTLY_ENV:-PROD}" != "CI" ]]; then
until command -p xcode-select --print-path > /dev/null 2>&1; do
_a "Waiting for Command Line tools to be installed... Check again in 10 secs"
sleep 10
done
fi
{
[[ -d "/Library/Developer/CommandLineTools" ]] &&
command -p sudo "$(command -vp xcode-select)" --switch /Library/Developer/CommandLineTools
} || _e "Command Line Tools could not be selected"
fi
if ! output="$(command -p xcrun clang 2>&1)" && [[ "$output" == *"license"* ]]; then
_e "Command Line Tools could not be installed because you do not have accepted the license"
return 1
fi
}
start_sudo() {
[[ "${DOTLY_ENV:-PROD}" == "CI" ]] && return
if ! has_sudo; then
command -p sudo -v -B
if has_sudo && [[ -z "${SUDO_PID:-}" ]]; then
(while true; do
command -p sudo -v
command -p sleep 30
done) &
SUDO_PID="$!"
builtin trap stop_sudo SIGINT SIGTERM
fi
fi
}
stop_sudo() {
builtin kill "$SUDO_PID" > /dev/null 2>&1
builtin trap - SIGINT SIGTERM
command -p sudo -k
}
has_sudo() {
[[ "${DOTLY_ENV:-PROD}" == "CI" ]] && return
command -p sudo -n -v > /dev/null 2>&1
}
if command -v brew > /dev/null 2>&1; then
eval "$(command brew shellenv)"
else
PATH="/usr/local/bin:/opt/homebrew/bin:/home/.linuxbrew/bin:${HOME}/.homebrew/bin:${HOME}/.brew/bin:${HOME}/.linuxbrew/bin${PATH:+:$PATH}"
fi
# Default values in CI env
if [[ "${DOTLY_ENV:-PROD}" == "CI" ]]; then
export SETUP_ZSH_AND_BASH_IN_SHELLS=false
export IGNORE_APPLY_SYMLINKS=false
export SYMLINKS_ARGS=(--ignore-backup)
export SETUP_ZSH_AS_DEFAULT_SHELL=false
export IS_ICLOUD_DOTFILES=false
export DOTFILES_PATH="${HOME}/.dotfiles"
fi
_w " ┌──────────────────────────────────────────┐"
_w "~ │ 🚀 Welcome to the ${green}sloth/dotly${normal} installer! │ ~"
_w " └──────────────────────────────────────────┘"
_w
#### Begin user prompts ####
if [[ $(uname -s) == "Darwin" ]] && ! is_clt_installed; then
_w "We will need to use user elevation with sudo to install Command Line Tools"
start_sudo
if ! has_sudo; then
_e "Elevation error: wrong or not provided password"
_a "Yow will be asked later for the password again"
fi
fi
# Adds zsh and bash in /etc/shells (only in macos, in linux is done by the package manager)
if
is_macos &&
[[ -z "${SETUP_ZSH_AND_BASH_IN_SHELLS:-}" ]]
then
SETUP_ZSH_AND_BASH_IN_SHELLS=false
if
_q "Do you want to setup more modern \`zsh\` & \`bash\` as valid shells in \`/etc/shells\` (elevation required; sudo)? [Y/n]" "PROMPT_REPLY" &&
[[ "${PROMPT_REPLY:-Y}" =~ ^[Yy] ]]
then
start_sudo
has_sudo && SETUP_ZSH_AND_BASH_IN_SHELLS=true
! has_sudo && _e "It can not be done without elevation, aborting this options (should be done later manually)" && SETUP_ZSH_AND_BASH_IN_SHELLS=false
fi
# If defined the variable, we need to cache the sudo password
elif
is_macos &&
${SETUP_ZSH_AND_BASH_IN_SHELLS:-false}
then
start_sudo
! has_sudo &&
_e "It can not be done without elevation, aborting this options (should be done later manually)" &&
SETUP_ZSH_AND_BASH_IN_SHELLS=false
fi
export SETUP_ZSH_AND_BASH_IN_SHELLS
# Backups
if
! ${IGNORE_APPLY_SYMLINKS:-false} &&
[[ -z "${SYMLINKS_ARGS[*]:-}" ]]
then
SYMLINKS_ARGS=(--interactive-backup)
if ${never_backup:-}; then
SYMLINKS_ARGS=(--ignore-backup)
elif ${always_backup:-}; then
SYMLINKS_ARGS=(--backup)
else
bk=false
output::yesno "Do you want to perform a backup of symlinks before apply them (this will include all existing files)" && bk=true
$bk && output::yesno "Do you want to be asked for every file" || SYMLINKS_ARGS=(--backup)
! $bk && SYMLINKS_ARGS=(--ignore-backup)
fi
fi
export SYMLINKS_ARGS
# ZSH as default Shell
zsh_shell_path="$(command -v zsh || true)"
if [[ -z "${SETUP_ZSH_AS_DEFAULT_SHELL:-}" ]]; then
SETUP_ZSH_AS_DEFAULT_SHELL=false
if
[[ "$SHELL" == *"zsh"* ]] &&
command_exists chsh &&
[[ -x "$zsh_shell_path" ]] &&
[[ -f "/etc/shells" ]] &&
! grep -q "^${zsh_shell_path}$" "/etc/shells"
then
_q "Do you want to setup the zsh \`$zsh_shell_path\` as default shell? [Y/n]" "PROMPT_REPLY" &&
[[ "${PROMPT_REPLY:-Y}" =~ ^[Yy] ]] &&
SETUP_ZSH_AS_DEFAULT_SHELL=true
_a "This will need a sudo authentication to add it to \`/etc/shells\` file"
start_sudo
if ! has_sudo; then
_e "Without sudo authentication this can not be done. You should do this later manually"
SETUP_ZSH_AS_DEFAULT_SHELL=false
fi
fi
elif
${SETUP_ZSH_AS_DEFAULT_SHELL:-false} &&
[[ "$SHELL" == *"zsh"* ]] &&
command_exists chsh &&
[[ -x "$zsh_shell_path" ]] &&
[[ -f "/etc/shells" ]] &&
! grep -q "^${zsh_shell_path}$" "/etc/shells"
then
start_sudo
! has_sudo &&
_e "\`SETUP_ZSH_AS_DEFAULT_SHELL\` should be aborted because sudo elevation password is not set." &&
SETUP_ZSH_AS_DEFAULT_SHELL=false
else
SETUP_ZSH_AS_DEFAULT_SHELL=${SETUP_ZSH_AS_DEFAULT_SHELL:-false}
fi
export SETUP_ZSH_AS_DEFAULT_SHELL
if
[[ -z "${IS_ICLOUD_DOTFILES:-}" ]] &&
is_macos &&
[[ -d "$ICLOUD_PATH" ]] ||
${IS_ICLOUD_DOTFILES:-false} &&
[[ -z "${DOTFILES_PATH:-}" ]]
then
_yq "Do you want to place your dotfiles in your iCloud storage" "ICLOUD_REPLY"
if _is_y "${ICLOUD_REPLY:-Y}"; then
IS_ICLOUD_DOTFILES=true
_q "What name do you want for your dotfiles folder? [.dotfiles]" "DOTFILES_PATH"
DOTFILES_PATH="${ICLOUD_PATH}/${DOTFILES_PATH:-.dotfiles}"
DOTFILES_LINK_PATH="${HOME}/$(basename "$ICLOUD_DOTFILES")"
fi
fi
if
! ${IS_ICLOUD_DOTFILES:-false} &&
[[ -z "${DOTFILES_PATH:-}" ]]
then
_q "Where do you want your dotfiles to be located? [~/.dotfiles]" "DOTFILES_PATH"
if [[ "${DOTLY_ENV:-PROD}" == "CI" ]]; then
DOTFILES_PATH="${HOME}/.dotfiles"
else
DOTFILES_PATH="${DOTFILES_PATH:-${HOME}/.dotfiles}"
fi
DOTFILES_PATH="$(eval echo "$DOTFILES_PATH")"
fi
export DOTFILES_PATH="$DOTFILES_PATH"
#### End user prompts ####
dotly_inner_path="modules/sloth"
export DOTLY_PATH="${DOTFILES_PATH}/${dotly_inner_path}"
export SLOTH_PATH="${SLOTH_PATH:-${DOTLY_PATH:-}}"
create_dotfiles_dir "$DOTFILES_PATH"
cd "$DOTFILES_PATH"
if ! command_exists git; then
_e "git not installed, trying to install"
if command_exists apt-get; then
_a "Installing using apt"
sudo apt-get -y install git 2>&1 | _log "Installing git"
elif command_exists dnf; then
_a "Installing using dnf"
sudo dnf -y install git 2>&1 | _log "Installing git"
elif command_exists yum; then
_a "Installing using yum"
yes | sudo yum install git 2>&1 | _log "Installing git"
elif command_exists brew; then
_a "Installing using brew"
yes | brew install git 2>&1 | _log "Installing git"
elif command_exists pacman; then
_a "Installing using pacman"
sudo pacman -S --noconfirm git 2>&1 | _log "Installing git"
else
if is_macos; then
_a "Checking if Command Line Tools are installed 🕵️♂️"
xcode-select --install 2>&1 | grep installed > /dev/null
if [[ $? ]]; then
_a "Installing Command Line Tools 📺"
xcode-select --install
_q "Press a key after command line tools has finished to continue...👇" "CLT_INSTALLED"
fi
else
_e "Could not install git, no package provider found"
exit 1
fi
fi
fi
if ! command_exists curl; then
_e "curl not installed, trying to install"
if command_exists apt; then
_a "Installing using apt"
sudo apt -y install curl 2>&1 | _log "Installing curl"
elif command_exists dnf; then
_a "Installing using dnf"
sudo dnf -y install curl 2>&1 | _log "Installing curl"
elif command_exists yum; then
_a "Installing using yum"
yes | sudo yum install curl 2>&1 | _log "Installing curl"
elif command_exists brew; then
_a "Installing using brew"
yes | brew install curl 2>&1 | _log "Installing curl"
elif command_exists pacman; then
_a "Installing using pacman"
sudo pacman -S --noconfirm curl 2>&1 | _log "Installing curl"
else
_e "Could not install curl, no package provider found"
exit 1
fi
fi
_a "Initializing your dotfiles git repository"
git init 2>&1 | _log "Initializing repository"
_a "Cloning .Sloth"
git submodule add -b "$DOTLY_BRANCH" "https://github.com/$DOTLY_REPOSITORY.git" "$dotly_inner_path" 2>&1 | _log "Adding sloth submodule"
git config -f .gitmodules submodule."$dotly_inner_path".ignore dirty
_a "Installing .Sloth dependencies"
git submodule update --init --recursive 2>&1 | _log "Installing dependencies"
cd "${SLOTH_PATH:-${DOTLY_PATH:-}}" || true
if [[ ${IS_ICLOUD_DOTFILES:-true} == true ]]; then
if [[ -d "$DOTFILES_LINK_PATH" ]]; then
create_dotfiles_dir "$DOTFILES_LINK_PATH"
rm -f "$DOTFILES_LINK_PATH"
ln -s "$DOTFILES_PATH" "$DOTFILES_LINK_PATH"
export DOTFILES_PATH="$DOTFILES_LINK_PATH"
fi
fi
_a "Installing .Sloth"
"${PWD}/bin/dot" core install 2>&1 | _log "Installing .Sloth"
_a "🎉 .Sloth was sucessfully installed! 🎉"
_a "Please, restart your terminal to see the changes"