Skip to content

Commit

Permalink
cleanup script
Browse files Browse the repository at this point in the history
  • Loading branch information
timmermansjoy committed Nov 27, 2024
1 parent 91e6469 commit 35366e1
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 133 deletions.
69 changes: 54 additions & 15 deletions scripts/dependencies-install-linux.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,65 @@
#!/bin/bash
#
# Run all dotfiles installers and related setup

sudo apt update
set -e # Exit immediately if a command exits with a non-zero status
set -o pipefail # Exit if any command in a pipeline fails

# run all dotfiles installers
export DOTFILES="$HOME/.dotfiles"
# Utility functions for consistent output
info() {
printf "\r [ \033[00;34m..\033[0m ] %s\n" "$1"
}

success() {
printf "\r\033[2K [ \033[00;32mOK\033[0m ] %s\n" "$1"
}

cd "$(dirname "$0")"/.. || exit
find -H "$DOTFILES" -maxdepth 4 -name 'install.sh' -not -path '*.git*' -exec sh -c 'FILE="$1"; "$FILE"' _ {} \;
fail() {
printf "\r\033[2K [ \033[0;31mFAIL\033[0m ] %s\n" "$1"
echo ''
exit 1
}

export DOTFILES="$HOME/.dotfiles"

info "adding zsh extensions"
git clone https://github.com/zsh-users/zsh-autosuggestions.git ~/.oh-my-zsh/custom/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ~/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting
info "Updating package lists"
sudo apt update || fail "Failed to update package lists"

info "adding vscode repo"
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
info "Running all dotfiles installers"
cd "$(dirname "$0")/.." || fail "Failed to change directory"
find -H "$DOTFILES" -maxdepth 4 -name 'install.sh' -not -path '*.git*' -exec sh -c 'FILE="$1"; "$FILE"' _ {} \; || fail "Failed to run dotfiles installers"

info "install the packages inside specific/linux/packages.txt"
xargs sudo apt -y -qq install < ~/.dotfiles/specific/linux/packages.txt
if [[ "$(uname)" == "Linux" ]]; then
info "Installing Zsh Autosuggestions"
ZSH_PLUGIN_DIR="$HOME/.oh-my-zsh/custom/plugins/zsh-autosuggestions"
if [[ ! -d "$ZSH_PLUGIN_DIR" ]]; then
git clone https://github.com/zsh-users/zsh-autosuggestions "$ZSH_PLUGIN_DIR" || fail "Failed to clone zsh-autosuggestions"
success "Zsh Autosuggestions installed"
else
success "Zsh Autosuggestions already installed"
fi

info "installing lambda stack"
wget -nv -O- https://lambdalabs.com/install-lambda-stack.sh | sh -
info "Installing Zsh Syntax Highlighting"
ZSH_HIGHLIGHT_DIR="$HOME/.oh-my-zsh/custom/plugins/zsh-syntax-highlighting"
if [[ ! -d "$ZSH_HIGHLIGHT_DIR" ]]; then
git clone https://github.com/zsh-users/zsh-syntax-highlighting "$ZSH_HIGHLIGHT_DIR" || fail "Failed to clone zsh-syntax-highlighting"
success "Zsh Syntax Highlighting installed"
else
success "Zsh Syntax Highlighting already installed"
fi

info "Installing packages from specific/linux/packages.txt"
PACKAGE_FILE="$DOTFILES/specific/linux/packages.txt"
if [[ -f "$PACKAGE_FILE" ]]; then
xargs sudo apt -y -qq install <"$PACKAGE_FILE" || fail "Failed to install packages from $PACKAGE_FILE"
success "Packages installed from $PACKAGE_FILE"
else
fail "Package list not found at $PACKAGE_FILE"
fi

info "Installing Lambda Stack"
wget -nv -O- https://lambdalabs.com/install-lambda-stack.sh | sh - || fail "Failed to install Lambda Stack"
success "Lambda Stack installed"
fi

success "Dotfiles setup completed successfully!"
158 changes: 69 additions & 89 deletions scripts/dotfiles-install.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# hooks the dotfiles project to your home and shell config
# Hooks the dotfiles project to your home and shell config
export DOTFILES="$HOME/.dotfiles"

echo ''
Expand All @@ -20,144 +20,124 @@ success() {
fail() {
printf "\r\033[2K [\033[0;31mFAIL\033[0m] %s\n" "$1"
echo ''
exit
exit 1
}

setup_gitconfig() {
if ! [ -f ~/.gitconfig ]; then
info 'setup gitconfig'
if ! [ -f "$HOME/.gitconfig" ]; then
info 'Setting up gitconfig'

user ' - What is your github author name?'
user ' - What is your GitHub author name?'
read -r -e git_authorname
user ' - What is your github author email?'
user ' - What is your GitHub author email?'
read -r -e git_authoremail

sed \
-e "s/AUTHORNAME/$git_authorname/g" \
-e "s/AUTHOREMAIL/$git_authoremail/g" \
~/.dotfiles/specific/git/gitconfig-local.symlink.example >~/.dotfiles/specific/git/gitconfig-local.symlink
"$DOTFILES/specific/git/gitconfig-local.symlink.example" >"$DOTFILES/specific/git/gitconfig-local.symlink" || fail "Error creating gitconfig"

success 'gitconfig'
success 'Gitconfig setup completed'
fi
}

link_file() {
local SRC=$1 DST=$2
local SRC="$1" DST="$2"

local overwrite='' backup='' skip=''
local action=

if [ -f "$DST" ] || [ -d "$DST" ] || [ -L "$DST" ]; then
if [ "$overwrite_all" == "false" ] && [ "$backup_all" == "false" ] && [ "$skip_all" == "false" ]; then
local currentSrc
currentSrc="$(readlink "$DST")"

if [ "$currentSrc" == "$SRC" ]; then
skip=true
else
user "File already exists: $DST ($(basename "$SRC")), what do you want to do?\n\
[s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all?"
read -r -n 1 action

case "$action" in
o)
overwrite=true
;;
O)
overwrite_all=true
;;
b)
backup=true
;;
B)
backup_all=true
;;
s)
skip=true
;;
S)
skip_all=true
;;
*) ;;

esac
fi
local action

if [ -e "$DST" ]; then
local currentSrc
currentSrc=$(readlink "$DST" 2>/dev/null)

if [ "$currentSrc" == "$SRC" ]; then
skip=true
else
user "File already exists: $DST ($(basename "$SRC")), what do you want to do?\n\
[s]kip, [S]kip all, [o]verwrite, [O]verwrite all, [b]ackup, [B]ackup all?"
read -r -n 1 action
echo ""

case "$action" in
o) overwrite=true ;;
O) overwrite_all=true ;;
b) backup=true ;;
B) backup_all=true ;;
s) skip=true ;;
S) skip_all=true ;;
*) ;;
esac
fi
fi

overwrite=${overwrite:-$overwrite_all}
backup=${backup:-$backup_all}
skip=${skip:-$skip_all}

if [ "$overwrite" == "true" ]; then
rm -rf "$DST"
success "removed $DST"
fi
overwrite=${overwrite:-$overwrite_all}
backup=${backup:-$backup_all}
skip=${skip:-$skip_all}

if [ "$backup" == "true" ]; then
mv "$DST" "${DST}.backup"
success "moved $DST to ${DST}.backup"
fi
if [ "$overwrite" == "true" ]; then
rm -rf "$DST" || fail "Failed to remove $DST"
success "Removed $DST"
fi

if [ "$skip" == "true" ]; then
success "skipped $SRC"
fi
if [ "$backup" == "true" ]; then
mv "$DST" "${DST}.backup" || fail "Failed to backup $DST"
success "Moved $DST to ${DST}.backup"
fi

# "false" or empty
if [ "$skip" != "true" ]; then
ln -s "$1" "$2"
success "linked $1 to $2"
if [ "$skip" == "true" ]; then
success "Skipped $SRC"
return
fi

ln -s "$SRC" "$DST" || fail "Failed to link $SRC to $DST"
success "Linked $SRC to $DST"
}

install_dotfiles() {
info 'linking dotfiles'
info 'Linking dotfiles'

local overwrite_all=false backup_all=false skip_all=false

# shellcheck disable=SC2044
for SRC in $(find -H "$DOTFILES" -maxdepth 4 -name '*.symlink' -not -path '*.git*'); do
DST="$HOME/.$(basename "${SRC%.*}")"
find -H "$DOTFILES" -maxdepth 4 -name '*.symlink' -not -path '*.git*' | while read -r SRC; do
local DST="$HOME/.$(basename "${SRC%.*}")"
link_file "$SRC" "$DST"
done
}

setup_gitconfig
install_dotfiles

# check if M1 mac
if [[ "$(uname -m)" == "arm64" ]]; then
info "Linking /opt/homebrew/bin to /usr/local/bin for M1 macs compatibility"
sudo ln -s /opt/homebrew/bin /usr/local/bin
info "Linking /opt/homebrew/bin to /usr/local/bin for M1 Macs compatibility"
sudo ln -s /opt/homebrew/bin /usr/local/bin || fail "Failed to link /opt/homebrew/bin to /usr/local/bin"
fi

info "installing dependencies"
if source ~/.dotfiles/scripts/dependencies-install.sh; then
success "dependencies installed"
info "Installing dependencies"
if source "$DOTFILES/scripts/dependencies-install.sh"; then
success "Dependencies installed"
else
fail "error installing dependencies"
fail "Error installing dependencies"
fi

install packages on linux
if [[ "$(uname)" == "Linux" ]]; then
info "installing packages"
if source ~/.dotfiles/scripts/dependencies-install-linux.sh
then
success "packages installed"
info "setting zsh as default shell"
chsh -s "$(command -v zsh)"
info "Installing Linux packages"
if source "$DOTFILES/scripts/dependencies-install-linux.sh"; then
success "Linux packages installed"
info "Setting Zsh as the default shell"
chsh -s "$(command -v zsh)" || fail "Failed to set Zsh as default shell"
else
fail "error installing dependencies"
fail "Error installing Linux packages"
fi
fi

info "setting up ssh"
if [[ ! -f ~/.ssh/id_rsa ]]; then
ssh-keygen -t rsa -b 4096 -C " $(git config --global user.email) " -f ~/.ssh/id_rsa -N ""
info "Setting up SSH"
if [[ ! -f "$HOME/.ssh/id_rsa" ]]; then
ssh-keygen -t rsa -b 4096 -C "$(git config --global user.email)" -f "$HOME/.ssh/id_rsa" -N "" || fail "SSH key generation failed"
eval "$(ssh-agent -s)"
success "ssh setup"
success "SSH setup completed"
else
success "ssh already setup"
success "SSH already set up"
fi

echo ''
Expand Down
27 changes: 0 additions & 27 deletions specific/linux/install.sh

This file was deleted.

1 change: 0 additions & 1 deletion specific/linux/packages.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ vim
tmux
htop
neofetch
code
2 changes: 1 addition & 1 deletion specific/oh-my-zsh/index.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ export ZSH=~/.oh-my-zsh

ZSH_THEME="powerlevel10k/powerlevel10k"

plugins=(docker python git z brew virtualenv pyenv tmux)
plugins=(docker python git z virtualenv tmux)

source $ZSH/oh-my-zsh.sh

Expand Down

0 comments on commit 35366e1

Please sign in to comment.