-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathinstall.sh
198 lines (169 loc) · 6.28 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
# Dotfile setup script
# Fetches latest changes, symlinks files, and installs dependencies
# For docs and more info, see: /~https://github.com/lissy93/dotfiles
# Licensed under MIT - (C) Alicia Sykes, 2022 <https://aliciasykes.com>
# IMPORTANT: Before running, read through everything, and confirm it's what you want!
# Configuration Params
REPO_NAME="Lissy93/Dotfiles"
REPO_PATH="/~https://github.com/${REPO_NAME}.git"
CONFIG=".install.conf.yaml"
DOTBOT_DIR="dotbot"
DOTBOT_BIN="bin/dotbot"
CURRENT_DIR=$(cd "$(dirname ${BASH_SOURCE[0]})" && pwd)
DOTFILES_DIR="${DOTFILES_DIR:-$HOME/Documents/config/dotfiles}"
TITLE='🧰 Lissy93/Dotfiles Setup'
# Color Variables
CYAN_B='\033[1;96m'
YELLOW_B='\033[1;93m'
RED_B='\033[1;31m'
GREEN_B='\033[1;32m'
PLAIN_B='\033[1;37m'
RESET='\033[0m'
PURPLE='\033[0;35m'
# Other params
PROMPT_TIMEOUT=15 # When user is prompted for input, skip after x seconds
# Start timer
start_time=`date +%s`
# Get system type - Linux / MacOS (Darwin)
system_type=$(uname -s)
# Function that prints important text in a banner with colored border
# First param is the text to output, then optional color and padding
make_banner () {
bannerText=$1
lineColor="${2:-$CYAN_B}"
padding="${3:-0}"
titleLen=$(expr ${#bannerText} + 2 + $padding);
lineChar="─"; line=""
for (( i = 0; i < "$titleLen"; ++i )); do line="${line}${lineChar}"; done
banner="${lineColor}╭${line}╮\n│ ${PLAIN_B}${bannerText}${lineColor} │\n╰${line}╯"
echo -e "\n${banner}\n${RESET}"
}
# Checks if a given package is installed
command_exists () {
hash "$1" 2> /dev/null
}
# On error, displays death banner, and terminates app with exit code 1
terminate () {
make_banner "Installation failed. Terminating..." ${RED_B}
exit 1
}
# Checks if command / package (in $1) exists and then shows
# warning or error + terminates depending if required ($2)
system_verify () {
if ! command_exists $1; then
if $2; then
echo -e "🚫 ${RED_B}Error:${PLAIN_B} $1 is not installed${RESET}"
terminate
else
echo -e "⚠️ ${YELLOW_B}Warning:${PLAIN_B} $1 is not installed${RESET}"
fi
fi
}
# Prints welcome banner, verifies that requirements are met
function pre_setup_tasks () {
# Show starting banner
make_banner "${TITLE}" "${CYAN_B}" 1
# Verify required packages are installed
system_verify "git" true
system_verify "zsh" false
system_verify "vim" false
system_verify "nvim" false
system_verify "tmux" false
}
# Downloads / updates dotfiles and symlinks them
function setup_dot_files () {
# Download / update dotfiles repo with git
if [[ ! -d "$DOTFILES_DIR" ]]
then
echo -e "${PURPLE}Dotfiles not yet present. Will download ${REPO_NAME} into ${DOTFILES_DIR}${RESET}"
mkdir -p "${DOTFILES_DIR}"
git clone --recursive ${REPO_PATH} ${DOTFILES_DIR}
else
echo -e "${PURPLE}Pulling changes from ${REPO_NAME} into ${DOTFILES_DIR}${RESET}"
cd "${DOTFILES_DIR}" && git pull origin master && git submodule update --recursive
fi
# If git clone / pull failed, then exit with error
ret=$?
if ! test "$ret" -eq 0
then
echo >&2 "${RED_B}Failed to fetch dotfiels $ret${RESET}"
terminate
fi
# Set up symlinks with dotbot
echo -e "${PURPLE}Setting up Symlinks${RESET}"
cd "${DOTFILES_DIR}"
git -C "${DOTBOT_DIR}" submodule sync --quiet --recursive
git submodule update --init --recursive "${DOTBOT_DIR}"
chmod +x dotbot/bin/dotbot
"${DOTFILES_DIR}/${DOTBOT_DIR}/${DOTBOT_BIN}" -d "${DOTFILES_DIR}" -c "${CONFIG}" "${@}"
}
# Applies application-specific preferences, and runs some setup tasks
function apply_preferences () {
# If ZSH not the default shell, ask user if they'd like to set it
if [[ $SHELL != *"zsh"* ]] && command_exists zsh; then
read -t $PROMPT_TIMEOUT -p "$(echo -e $CYAN_B)Would you like to set ZSH as your default shell? (y/N)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -e "${PURPLE}Setting ZSH as default shell${RESET}"
chsh -s $(which zsh) $USER
fi
fi
# Install / update vim plugins with Plug
echo -e "${PURPLE}Installing Vim Plugins${RESET}"
vim +PlugInstall +qall
# Install / update Tmux plugins with TPM
echo -e "${PURPLE}Installing TMUX Plugins${RESET}"
chmod ug+x "${XDG_DATA_HOME}/tmux/tpm"
sh "${XDG_DATA_HOME}/tmux/plugins/tpm/bin/install_plugins"
# Install / update ZSH plugins with Antigen
echo -e "${PURPLE}Installing ZSH Plugins${RESET}"
/bin/zsh -i -c "antigen update && antigen-apply"
}
# Based on system type, uses appropriate package manager to install / updates apps
function install_packages () {
read -t $PROMPT_TIMEOUT -p "$(echo -e $CYAN_B)Would you like to install / update system packages? (y/N) " -n 1 -r
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
echo -e "\n${PURPLE}Skipping package installs${RESET}"
return
fi
# Mac OS
if [ "$system_type" = "Darwin" ]; then
# Homebrew not installed, ask user if they'd like to download it now
if ! command_exists brew; then
read -t $PROMPT_TIMEOUT -p "$(echo -e $CYAN_B)Would you like to install Homebrew? (y/N)" -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
echo -en "🍺 ${YELLOW_B}Installing Homebrew...${RESET}\n"
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
export PATH=/opt/homebrew/bin:$PATH
fi
fi
# Update / Install the Homebrew packages in ~/.Brewfile
if command_exists brew && [ -f "$HOME/.Brewfile" ]
then
echo -e "${PURPLE}Updating homebrew and packages...${RESET}"
brew update
brew upgrade
BREW_PREFIX=$(brew --prefix)
brew bundle --global --file $HOME/.Brewfile
brew cleanup
fi
fi
}
# Updates current session, and outputs summary
function finishing_up () {
# Update source to ZSH entry point
source "${HOME}/.zshenv"
# Print success message, and time taken
total_time=$((`date +%s`-start_time))
make_banner "✨ Dotfiles configured succesfully in $total_time seconds" ${GREEN_B} 1
exit 0
}
# Begin!
pre_setup_tasks # Print start message, and check requirements are met
setup_dot_files # Clone / updatae dotfiles, and create the symlinks
apply_preferences # Set settings for individual applications
install_packages # Prompt to install / update OS-specific packages
finishing_up # Re-source .zshenv, and print summary
# All done!