chore(bootstrap): optimize shell-scripts for robustness

This commit is contained in:
js0ny 2025-02-27 03:02:55 +00:00
parent 42b83d4696
commit 2c014e281f
13 changed files with 441 additions and 184 deletions

View file

@ -0,0 +1,232 @@
#!/usr/bin/env bash
# Adds better error handling and safety features
# shellcheck disable=SC2016
# TODO: UNTESTED
set -e # Exit immediately if a command exits with a non-zero status
set -u # Treat unset variables as an error
# Create temporary working directory
TEMP_DIR="${HOME}/.tmp/catppuccin-install"
mkdir -p "${TEMP_DIR}"
# Create config directories
mkdir -p "${HOME}/.config/tmux/plugins"
mkdir -p "${HOME}/.config/yazi"
mkdir -p "${HOME}/.config/btop/themes"
mkdir -p "${HOME}/.config/git"
mkdir -p "${HOME}/.config/lsd"
mkdir -p "${HOME}/.local/share/mc/skins"
mkdir -p "${HOME}/.config/fish/themes"
mkdir -p "${HOME}/.local/share/fcitx5/themes"
# Function for downloading files
download_file() {
local url="$1"
local output_path="$2"
local output_dir
# Extract directory from output path
output_dir=$(dirname "$output_path")
# Create directory if it doesn't exist
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
fi
# Print status message
echo "Downloading: $url"
echo " to: $output_path"
# Download with curl (using -L to follow redirects)
if ! curl -L -s --fail "$url" -o "$output_path"; then
echo "Error: Failed to download $url" >&2
return 1
fi
# Verify file was downloaded
if [ ! -f "$output_path" ]; then
echo "Error: File was not created at $output_path" >&2
return 1
fi
echo "Download successful."
return 0
}
# Function to check if command exists
command_exists() {
command -v "$1" >/dev/null 2>&1
}
# Install Bat themes
if command_exists bat; then
BAT_CONFIG_DIR=$(bat --config-dir 2>/dev/null || echo "${HOME}/.config/bat")
mkdir -p "${BAT_CONFIG_DIR}/themes"
echo "Installing Bat themes..."
THEMES=("Latte" "Frappe" "Macchiato" "Mocha")
for theme in "${THEMES[@]}"; do
download_file "https://github.com/catppuccin/bat/raw/main/themes/Catppuccin%20${theme}.tmTheme" \
"${BAT_CONFIG_DIR}/themes/Catppuccin ${theme}.tmTheme"
done
echo "Rebuilding Bat cache..."
bat cache --build || echo "WARNING: Failed to rebuild Bat cache" >&2
else
echo "Bat not found, skipping Bat themes installation" >&2
fi
# Install Tmux plugin
if command_exists tmux; then
echo "Installing Tmux plugin..."
TMUX_PLUGIN_DIR="${HOME}/.config/tmux/plugins/catppuccin"
rm -rf "${TMUX_PLUGIN_DIR}"
mkdir -p "${TMUX_PLUGIN_DIR}"
if command_exists git; then
if ! git clone -b v2.1.2 --depth=1 https://github.com/catppuccin/tmux.git "${TMUX_PLUGIN_DIR}/tmux" 2>/dev/null; then
echo "WARNING: Failed to clone Tmux plugin repository" >&2
fi
else
echo "Git not found, skipping Tmux plugin installation" >&2
fi
else
echo "Tmux not found, skipping Tmux plugin installation" >&2
fi
# Install Yazi theme
if command_exists yazi; then
echo "Installing Yazi theme..."
download_file "https://raw.githubusercontent.com/catppuccin/yazi/main/themes/mocha/catppuccin-mocha-lavender.toml" \
"${HOME}/.config/yazi/theme.toml"
else
echo "Yazi not found, skipping Yazi theme installation" >&2
fi
# Install BTopa themes
if command_exists btop; then
echo "Installing BTopa themes..."
download_file "https://raw.githubusercontent.com/catppuccin/btop/main/themes/catppuccin_mocha.theme" \
"${HOME}/.config/btop/themes/catppuccin_mocha.theme"
download_file "https://raw.githubusercontent.com/catppuccin/btop/main/themes/catppuccin_latte.theme" \
"${HOME}/.config/btop/themes/catppuccin_latte.theme"
else
echo "BTopa not found, skipping BTopa themes installation" >&2
fi
# Install Delta config
echo "Installing Delta config..."
download_file "https://raw.githubusercontent.com/catppuccin/delta/main/catppuccin.gitconfig" \
"${HOME}/.config/git/catppuccin-delta.gitconfig"
# Configure FZF
if command_exists fzf; then
echo "Configuring FZF..."
# Check shell type to determine how to set environment variables
if [ -n "${FISH_VERSION:-}" ] || command_exists fish; then
# For Fish shell
fish -c 'set -U FZF_DEFAULT_OPTS "--color=bg+:#313244,bg:#1e1e2e,spinner:#f5e0dc,hl:#f38ba8 --color=fg:#cdd6f4,header:#f38ba8,info:#cba6f7,pointer:#f5e0dc --color=marker:#b4befe,fg+:#cdd6f4,prompt:#cba6f7,hl+:#f38ba8 --color=selected-bg:#45475a --multi"' ||
echo "WARNING: Failed to set FZF options for Fish shell" >&2
else
# For Bash/Zsh - add to both .bashrc and .zshrc if they exist
FZF_CONFIG="export FZF_DEFAULT_OPTS=\"--color=bg+:#313244,bg:#1e1e2e,spinner:#f5e0dc,hl:#f38ba8 --color=fg:#cdd6f4,header:#f38ba8,info:#cba6f7,pointer:#f5e0dc --color=marker:#b4befe,fg+:#cdd6f4,prompt:#cba6f7,hl+:#f38ba8 --color=selected-bg:#45475a --multi\""
# Add to .bashrc if it exists
if [ -f "${HOME}/.bashrc" ]; then
if ! grep -q "FZF_DEFAULT_OPTS.*catppuccin" "${HOME}/.bashrc"; then
echo "${FZF_CONFIG}" >>"${HOME}/.bashrc"
echo "Added FZF configuration to .bashrc"
fi
fi
# Add to .zshrc if it exists
if [ -f "${HOME}/.zshrc" ]; then
if ! grep -q "FZF_DEFAULT_OPTS.*catppuccin" "${HOME}/.zshrc"; then
echo "${FZF_CONFIG}" >>"${HOME}/.zshrc"
echo "Added FZF configuration to .zshrc"
fi
fi
fi
else
echo "FZF not found, skipping FZF configuration" >&2
fi
# Install LSD themes
if command_exists lsd; then
echo "Installing LSD themes..."
download_file "https://raw.githubusercontent.com/catppuccin/lsd/main/themes/catppuccin-mocha/colors.yaml" \
"${HOME}/.config/lsd/colors.yaml"
download_file "https://raw.githubusercontent.com/catppuccin/lsd/main/themes/catppuccin-latte/colors.yaml" \
"${HOME}/.config/lsd/colors-light.yaml"
else
echo "LSD not found, skipping LSD themes installation" >&2
fi
# Install Midnight Commander skin
echo "Installing Midnight Commander skin..."
download_file "https://raw.githubusercontent.com/catppuccin/mc/main/catppuccin.ini" \
"${HOME}/.local/share/mc/skins/catppuccin.ini"
# Print MC configuration hint
echo "Note: To use the Midnight Commander theme:"
echo " - Change or add skin=catppuccin in the [Midnight-Commander] section inside ~/.config/mc/ini"
echo " - OR choose the skin inside Midnight Commander through F9 > Options > Appearance"
echo " - Save setup through F9 > Options > Save setup"
# Install PowerShell module
if command_exists pwsh; then
echo "Installing PowerShell module..."
PS_MODULE_PATH=$(pwsh -C 'Write-Output $Env:PSModulePath.split(":")[0]' 2>/dev/null)
if [ -n "${PS_MODULE_PATH}" ]; then
if command_exists git; then
if ! git clone --depth=1 https://github.com/catppuccin/powershell.git "${PS_MODULE_PATH}/Catppuccin" 2>/dev/null; then
echo "WARNING: Failed to clone PowerShell module repository" >&2
fi
else
echo "Git not found, skipping PowerShell module installation" >&2
fi
else
echo "PowerShell module path not found, skipping PowerShell module installation" >&2
fi
else
echo "PowerShell not found, skipping PowerShell module installation" >&2
fi
# Install Fish themes
if command_exists fish; then
echo "Installing Fish themes..."
download_file "https://raw.githubusercontent.com/catppuccin/fish/main/themes/Catppuccin%20Mocha.theme" \
"${HOME}/.config/fish/themes/Catppuccin Mocha.theme"
download_file "https://raw.githubusercontent.com/catppuccin/fish/main/themes/Catppuccin%20Latte.theme" \
"${HOME}/.config/fish/themes/Catppuccin Latte.theme"
else
echo "Fish not found, skipping Fish themes installation" >&2
fi
# Install Fcitx5 themes
if command_exists fcitx5; then
echo "Installing Fcitx5 themes..."
rm -rf "${TEMP_DIR}/fcitx5"
if command_exists git; then
if git clone --depth=1 https://github.com/catppuccin/fcitx5.git "${TEMP_DIR}/fcitx5" 2>/dev/null; then
cp -r "${TEMP_DIR}/fcitx5/src/"* "${HOME}/.local/share/fcitx5/themes/"
else
echo "WARNING: Failed to clone Fcitx5 repository" >&2
fi
else
echo "Git not found, skipping Fcitx5 themes installation" >&2
fi
else
echo "Fcitx5 not found, skipping Fcitx5 themes installation" >&2
fi
# Clean up temporary directory
rm -rf "${TEMP_DIR}"
echo "Catppuccin themes installation completed!"

View file

@ -1,7 +1,7 @@
echo "[INFO] Installing Rime"
git clone --depth 1 https://github.com/js0ny/rime_wanxiang_pro.git ~/Library/Rime
cd ~/Library/Rime
cd ~/Library/Rime || exit
just init
# just install_rime

View file

@ -16,9 +16,9 @@ XDG_DATA_HOME="${XDG_DATA_HOME:-$HOME/.local/share}"
XDG_STATE_HOME="${XDG_STATE_HOME:-$HOME/.local/state}"
echo "[INFO] Setting up some local directories"
test -d $XDG_CACHE_HOME || mkdir -p $XDG_CACHE_HOME
test -d $XDG_DATA_HOME || mkdir -p $XDG_DATA_HOME
test -d $XDG_STATE_HOME || mkdir -p $XDG_STATE_HOME
test -d "$XDG_CACHE_HOME" || mkdir -p "$XDG_CACHE_HOME"
test -d "$XDG_DATA_HOME" || mkdir -p "$XDG_DATA_HOME"
test -d "$XDG_STATE_HOME" || mkdir -p "$XDG_STATE_HOME"
test -d ~/.local/state/zsh || mkdir -p ~/.local/state/zsh
# echo "[INFO] Setting up system-wide zsh configuration"
@ -88,7 +88,7 @@ else
["$DOTFILES/tools/sioyek"]="$HOME/.config/sioyek"
)
for kde in "$DOTFILES/platforms/linux/kde/"*; do
linkDots+=["$kde"]="$HOME/.config/kde/$(basename $kde)"
linkDots+=["$kde"]="$HOME/.config/kde/$(basename "$kde")"
# echo "Linking $kde to $HOME/.config/kde/$(basename $kde)"
done
if [ "$WHEEL" -eq 1 ]; then

View file

@ -12,7 +12,7 @@ if [ "$WHEEL" -eq 1 ]; then
echo "[ACTION] Elevation required"
test -f "/etc/zsh/zshenv" && sudo cp "$DOTFILES/tools/zsh/global.zshenv" "/etc/zsh/zshenv"
test -f "/etc/zshenv" && sudo cp "$DOTFILES/tools/zsh/global.zshenv" "/etc/zshenv"
if [ $(uname) = "Darwin" ]; then
if [ "$(uname)" = "Darwin" ]; then
sudo cp "$DOTFILES/tools/zsh/global.zshenv" "/etc/zshenv"
fi
else
@ -22,7 +22,7 @@ else
fi
echo "[INFO] Installing zsh plugins"
test -d $ZDOTDIR/plugins/zsh-autosuggestions || git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions.git $ZDOTDIR/plugins/zsh-autosuggestions
test -d $ZDOTDIR/plugins/zsh-syntax-highlighting || git clone --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting.git $ZDOTDIR/plugins/zsh-syntax-highlighting
test -d $ZDOTDIR/plugins/zsh-history-substring-search || git clone --depth 1 https://github.com/zsh-users/zsh-history-substring-search.git $ZDOTDIR/plugins/zsh-history-substring-search
test -d $ZDOTDIR/plugins/zsh-completions || git clone --depth 1 https://github.com/zsh-users/zsh-completions.git $ZDOTDIR/plugins/zsh-completions
test -d "$ZDOTDIR"/plugins/zsh-autosuggestions || git clone --depth 1 https://github.com/zsh-users/zsh-autosuggestions.git "$ZDOTDIR"/plugins/zsh-autosuggestions
test -d "$ZDOTDIR"/plugins/zsh-syntax-highlighting || git clone --depth 1 https://github.com/zsh-users/zsh-syntax-highlighting.git "$ZDOTDIR"/plugins/zsh-syntax-highlighting
test -d "$ZDOTDIR"/plugins/zsh-history-substring-search || git clone --depth 1 https://github.com/zsh-users/zsh-history-substring-search.git "$ZDOTDIR"/plugins/zsh-history-substring-search
test -d "$ZDOTDIR"/plugins/zsh-completions || git clone --depth 1 https://github.com/zsh-users/zsh-completions.git "$ZDOTDIR"/plugins/zsh-completions

View file

@ -6,7 +6,7 @@ wget -q https://raw.githubusercontent.com/ivan-hc/AM/main/AM-INSTALLER && chmod
APPMAN_PATH="$HOME/.local/bin/appman"
if command -v $APPMAN_PATH >/dev/null 2>&1; then
if command -v "$APPMAN_PATH" >/dev/null 2>&1; then
$APPMAN_PATH install wezterm
$APPMAN_PATH install nvim
fi

View file

@ -32,7 +32,7 @@ for path in "${browser_flags_path[@]}"; do
echo "[INFO] Found Browser Flags: $path"
else
echo "[INFO] Creating Browser Flags: $path"
ln -s $BROWSER_FLAG $path
ln -s "$BROWSER_FLAG" "$path"
fi
done
@ -41,6 +41,6 @@ for path in "${electron_flags_path[@]}"; do
echo "[INFO] Found Electron Flags: $path"
else
echo "[INFO] Creating Electron Flags: $path"
ln -s $ELECTRON_FLAG $path
ln -s "$ELECTRON_FLAG" "$path"
fi
done

View file

@ -23,11 +23,17 @@ export WHEEL
GUI_SETUP=0
IS_WSL=0
WINDOWS_USER=""
PACKAGE_MANAGER=""
# NOTE: This is NOT a POSIX-compliant way, for POSIX-compliant way, use case/esac
if [[ "$(uname -r)" = *Microsoft* ]]; then
echo "[INFO] Running on WSL1 Skipping GUI setup"
IS_WSL=1
WINDOWS_USER="$(cmd.exe /c "echo %USERNAME%" | tr -d '\r')"
else
read -p "[ACTION] Do you want to setup Linux GUI? (y/N) " choice
read -p "[ACTION] Do you want to setup Linux GUI? (y/N) " -r choice
case "$choice" in
y | Y)
GUI_SETUP=1
@ -47,9 +53,11 @@ if [ "$WHEEL" -eq 1 ]; then
if command -v apt >/dev/null 2>&1; then
sudo apt update
sudo apt install -y build-essential
PACKAGE_MANAGER="apt"
elif command -v pacman >/dev/null 2>&1; then
sudo pacman -Syu --noconfirm
sudo pacman -S --noconfirm base-devel
PACKAGE_MANAGER="pacman"
else
echo "[ERROR] Unsupported package manager"
exit 1
@ -60,6 +68,8 @@ echo "[INFO] Cloning Dotfiles"
if [ -d "$DOTFILES" ]; then
echo "[INFO] Dotfiles already cloned"
elif [ "$IS_WSL" -eq 1 ]; then
ln -sf "/mnt/c/Users/$WINDOWS_USER/.dotfiles" "$DOTFILES"
elif command -v git >/dev/null 2>&1; then
git clone https://github.com/js0ny/dotfiles.git "$DOTFILES" --depth 1
else
@ -68,7 +78,7 @@ else
fi
if [ "$WHEEL" -eq 0 ]; then
read -p "[ACTION] Do you want to install some AppImages (by AppMan)? (y/N) " choice
read -p "[ACTION] Do you want to install some AppImages (by AppMan)? (y/N) " -r choice
case "$choice" in
y | Y)
source "$DOTFILES/bootstrap/linux/appman.bash"
@ -82,25 +92,25 @@ fi
echo "[INFO] Setting up symbolic links"
source $DOTFILES/bootstrap/linux/symlinks.bash
source "$DOTFILES"/bootstrap/linux/symlinks.bash
if [ "$WHEEL" -eq 1 ]; then
source $DOTFILES/tools/bash/xdg-compact.sh
source $DOTFILES/tools/bash/global.bashrc
source "$DOTFILES"/tools/bash/xdg-compact.sh
source "$DOTFILES"/tools/bash/global.bashrc
fi
source $DOTFILES/tools/bash/profile
source $DOTFILES/tools/bash/bashrc
source $DOTFILES/tools/bash/bash_aliases
source "$DOTFILES"/tools/bash/profile
source "$DOTFILES"/tools/bash/bashrc
source "$DOTFILES"/tools/bash/bash_aliases
if command -v zsh >/dev/null 2>&1; then
read -p "[ACTION] Do you want to setup zsh? (Y/n) " choice
read -p "[ACTION] Do you want to setup zsh? (Y/n) " -r choice
case "$choice" in
n | N)
:
;;
*)
source $DOTFILES/bootstrap/components/zsh.sh
source "$DOTFILES"/bootstrap/components/zsh.sh
;;
esac
fi
@ -110,19 +120,31 @@ if [ "$WHEEL" -eq 0 ]; then
exit 0
fi
read -p "[ACTION] Do you want to use other package managers? (y/N) " choice
# TODO: Add this part
read -p "[ACTION] Do you want to use other package managers? (y/N) " -r choice
case "$choice" in
y | Y)
y | Y)
:
;;
*)
*)
:
;;
esac
echo "[INFO] Installing Doom Emacs"
source $DOTFILES/bootstrap/components/emacs.sh
source "$DOTFILES"/bootstrap/components/emacs.sh
# TODO: Untestest
if [ "$IS_WSL" -eq 1 ]; then
ln -sf "/mnt/c/Users/$WINDOWS_USER" "$HOME/winhome"
ln -sf "/mnt/c/Users/$WINDOWS_USER/Downloads" "$HOME/Downloads"
ln -sf "/mnt/c/Users/$WINDOWS_USER/Documents" "$HOME/Documents"
ln -sf "/mnt/c/Users/$WINDOWS_USER/Desktop" "$HOME/Desktop"
ln -sf "/mnt/c/Users/$WINDOWS_USER/Pictures" "$HOME/Pictures"
ln -sf "/mnt/c/Users/$WINDOWS_USER/Music" "$HOME/Music"
ln -sf "/mnt/c/Users/$WINDOWS_USER/Videos" "$HOME/Videos"
fi
if [ "$GUI_SETUP" -eq 0 ]; then
echo "Done!"
@ -131,15 +153,17 @@ fi
# Wayland
source $DOTFILES/bootstrap/linux/chromium-flags.sh
source "$DOTFILES"/bootstrap/linux/chromium-flags.bash
# Rime
source $DOTFILES/bootstrap/components/rime.sh
source "$DOTFILES"/bootstrap/components/rime.sh
# TODO: Rewrite the script(mocha_port.fish) in bash/zsh or POSIX-compliant shell
echo "[INFO] Installing Color Scheme (Catppuccin Mocha)"
fish $DOTFILES/bootstrap/temp/mocha_port.fish
# fish $DOTFILES/bootstrap/temp/mocha_port.fish
source "$DOTFILES"/bootstrap/components/mocha-port.bash
echo "Done!"

View file

@ -9,7 +9,7 @@ source "$DOTFILES/bootstrap/components/symlinks.bash"
echo "[INFO] DOTFILES = $DOTFILES"
echo "[ACTION] Press any key to proceed"
read -n 1
read -r -n1 -s
echo "[INFO] Setting up symbolic links"

0
bootstrap/linux/wsl.bash Normal file
View file

View file

@ -9,7 +9,7 @@ source "$DOTFILES/bootstrap/components/symlinks.bash"
echo "[INFO] DOTFILES = $DOTFILES"
echo "[ACTION] Press any key to proceed"
read -n 1
read -r -n1
echo "[INFO] Setting up symbolic links"
# 使用 zsh 的关联数组语法

View file

@ -1 +1,4 @@
editMode: Vi
bind: n down
bind: e up
bind: l vi-insert

View file

@ -6,28 +6,26 @@
export DOTFILES=$HOME/.dotfiles
if [ -n "$WAYLAND_DISPLAY" ]; then
if [ -n "$WSL_DISTRO_NAME" ]; then
alias clip="clip.exe"
alias paste="pwsh.exe -NoProfile -Command 'Get-Clipboard'"
elif [ -n "$WAYLAND_DISPLAY" ]; then
alias clip="wl-copy"
alias paste="wl-paste"
elif [ -n "$DISPLAY" ]; then
alias clip="xclip"
elif [ -n "$WSL_DISTRO_NAME" ]; then
alias clip="clip.exe"
alias paste="pwsh.exe -NoProfile -Command 'Get-Clipboard'"
fi
source $DOTFILES/tools/bash/bash_aliases # For compatibility
source "$DOTFILES"/tools/bash/bash_aliases # For compatibility
export IPYTHONDIR="$XDG_CONFIG_HOME"/ipython
if command -v fzf > /dev/null; then
if command -v fzf >/dev/null; then
eval "$(fzf --bash)"
fi
if command -v starship > /dev/null; then
if command -v starship >/dev/null; then
eval "$(starship init bash)"
fi
#
# bind 'set show-mode-in-prompt off'
# bind 'set show-mode-in-prompt off'