From 040839862d11683ff07d0980e1596555ab1873a8 Mon Sep 17 00:00:00 2001 From: "marvin.dietermann" Date: Fri, 10 Apr 2026 11:51:03 +0200 Subject: [PATCH] refactor(install): improve modular architecture and error handling - Remove redundant installation checks in arduino.sh, git.sh - Add bash shell enforcement in install.sh to prevent compatibility issues - Streamline Arduino CLI installation logic for both Arch and other systems - Improve Git and Lazygit installation messaging consistency - Update install usage instructions for better clarity - Enhance shell compatibility by enforcing bash execution --- install.sh | 9 +++++-- lib/arduino.sh | 24 +++++++++-------- lib/common.sh | 11 ++++++++ lib/git.sh | 19 +++++++------ lib/neovim.sh | 65 ++++++++++++++++++++++++++++++++++----------- lib/shell-config.sh | 24 ++++++++++------- lib/starship.sh | 13 +++++---- lib/zellij.sh | 15 +++++------ lib/zsh.sh | 14 +++++----- 9 files changed, 125 insertions(+), 69 deletions(-) diff --git a/install.sh b/install.sh index 7dd6b33..ba45c7c 100644 --- a/install.sh +++ b/install.sh @@ -3,10 +3,15 @@ # Archduino Install — TUI Arduino Development Environment # Works on both native Arch Linux and Arch/Ubuntu WSL2 # -# Usage: curl -kL | bash -# or: chmod +x install.sh && ./install.sh +# Usage: chmod +x install.sh && ./install.sh +# or: bash install.sh # +# Re-exec with bash if invoked via sh/dash +if [ -z "$BASH_VERSION" ]; then + exec bash "$0" "$@" +fi + SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" # ── Load helpers and detect environment ───────────────────────── diff --git a/lib/arduino.sh b/lib/arduino.sh index e34702f..e6701ee 100644 --- a/lib/arduino.sh +++ b/lib/arduino.sh @@ -3,12 +3,10 @@ step "7/9 — Installing Arduino CLI + board cores" -if ! command -v arduino-cli &>/dev/null; then - if $IS_ARCH; then - $PKG_INSTALL arduino-cli - else - curl -kfsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/usr/local/bin sudo sh - fi +if $IS_ARCH; then + $PKG_INSTALL arduino-cli +else + curl -kfsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/usr/local/bin sudo sh fi info "Arduino CLI installed: $(arduino-cli version 2>/dev/null | head -1)" @@ -22,15 +20,19 @@ arduino-cli config add board_manager.additional_urls \ arduino-cli core update-index -info "Installing AVR core..." +info "Installing/updating AVR core..." arduino-cli core install arduino:avr -info "Installing ESP32 core..." +info "Installing/updating ESP32 core..." arduino-cli core install esp32:esp32 -info "Installing ESP8266 core..." +info "Installing/updating ESP8266 core..." arduino-cli core install esp8266:esp8266 -# Common libs +# Upgrade all installed cores to latest +arduino-cli core upgrade + +# Common libs (install is idempotent, upgrade updates them) arduino-cli lib install "Servo" "ArduinoJson" "Adafruit Unified Sensor" "DHT sensor library" 2>/dev/null || true -info "Board cores & libraries installed" +arduino-cli lib upgrade 2>/dev/null || true +info "Board cores & libraries installed/updated" diff --git a/lib/common.sh b/lib/common.sh index 4005181..1be5a2f 100644 --- a/lib/common.sh +++ b/lib/common.sh @@ -67,6 +67,17 @@ end_quiet_mode() { echo "" } +# ── Config backup helper ───────────────────────────────────────── +BACKUP_DIR="$HOME/.config/archduino/backups/$(date +%Y%m%d_%H%M%S)" + +backup_config() { + local src="$1" + [[ ! -e "$src" ]] && return 0 + mkdir -p "$BACKUP_DIR" + cp -a "$src" "$BACKUP_DIR/" + info "Backed up $(basename "$src") → $BACKUP_DIR/" +} + # ── Detect environment ────────────────────────────────────────── IS_WSL=false [[ -f /proc/version ]] && grep -qi microsoft /proc/version && IS_WSL=true diff --git a/lib/git.sh b/lib/git.sh index cbba992..cc9229b 100644 --- a/lib/git.sh +++ b/lib/git.sh @@ -3,18 +3,17 @@ step "8/9 — Setting up Git + Lazygit" -if ! command -v lazygit &>/dev/null; then - if $IS_ARCH; then - $PKG_INSTALL lazygit - else - LAZYGIT_VERSION=$(curl -ks "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | jq -r '.tag_name' | sed 's/v//') - curl -kLo /tmp/lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz" - tar xf /tmp/lazygit.tar.gz -C /tmp lazygit - sudo mv /tmp/lazygit /usr/local/bin/ - fi +if $IS_ARCH; then + $PKG_INSTALL lazygit +else + LAZYGIT_VERSION=$(curl -ks "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | jq -r '.tag_name' | sed 's/v//') + curl -kLo /tmp/lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz" + tar xf /tmp/lazygit.tar.gz -C /tmp lazygit + sudo mv /tmp/lazygit /usr/local/bin/ fi -info "Lazygit installed" +info "Lazygit installed/updated" mkdir -p ~/.config/lazygit +backup_config ~/.config/lazygit/config.yml write_lazygit_config ~/.config/lazygit/config.yml info "Lazygit configured with $THEME_DISPLAY_NAME" diff --git a/lib/neovim.sh b/lib/neovim.sh index 101dbeb..2c217b3 100644 --- a/lib/neovim.sh +++ b/lib/neovim.sh @@ -23,21 +23,54 @@ fi step "6/9 — Setting up NvChad" -if [[ ! -d "$HOME/.config/nvim/lua" ]]; then - for d in ~/.config/nvim ~/.local/share/nvim ~/.local/state/nvim ~/.cache/nvim; do - [[ -d "$d" ]] && mv "$d" "${d}.bak.$(date +%s)" - done - git clone https://github.com/NvChad/starter ~/.config/nvim - rm -rf ~/.config/nvim/.git - info "NvChad starter cloned" +NVIM_CFG="$HOME/.config/nvim" +NVCHAD_FRESH=false + +# ── Detect existing config ────────────────────────────────────── +if [[ -d "$NVIM_CFG/lua" ]]; then + if [[ -f "$NVIM_CFG/lua/chadrc.lua" ]]; then + # NvChad detected — update in place + info "Existing NvChad config detected, updating..." + elif grep -rq "LazyVim" "$NVIM_CFG/lua/" 2>/dev/null || [[ -f "$NVIM_CFG/lazyvim.json" ]]; then + # LazyVim detected — migrate to NvChad + warn "LazyVim config detected, migrating to NvChad..." + for d in "$NVIM_CFG" ~/.local/share/nvim ~/.local/state/nvim ~/.cache/nvim; do + if [[ -d "$d" ]]; then + backup_config "$d" + rm -rf "$d" + fi + done + NVCHAD_FRESH=true + else + # Unknown config — back up and install fresh + warn "Unknown Neovim config detected, backing up..." + for d in "$NVIM_CFG" ~/.local/share/nvim ~/.local/state/nvim ~/.cache/nvim; do + if [[ -d "$d" ]]; then + backup_config "$d" + rm -rf "$d" + fi + done + NVCHAD_FRESH=true + fi else - info "Neovim config already exists, skipping NvChad clone" + # No config at all — fresh install + NVCHAD_FRESH=true fi -# ── Theme ─────────────────────────────────────────────────────── -mkdir -p ~/.config/nvim/lua +if $NVCHAD_FRESH; then + git clone https://github.com/NvChad/starter "$NVIM_CFG" + rm -rf "$NVIM_CFG/.git" + info "NvChad starter cloned" +fi -cat >~/.config/nvim/lua/chadrc.lua </dev/null || true +rm -rf ~/.cache/nvim/base46 2>/dev/null || true + +# ── Theme ─────────────────────────────────────────────────────── +mkdir -p "$NVIM_CFG/lua" + +cat >"$NVIM_CFG/lua/chadrc.lua" <~/.config/nvim/lua/plugins/arduino.lua <<'LUA' +cat >"$NVIM_CFG/lua/plugins/arduino.lua" <<'LUA' return { { "stevearc/vim-arduino", @@ -65,9 +98,9 @@ return { LUA # ── LSP (clangd) ─────────────────────────────────────────────── -mkdir -p ~/.config/nvim/lua/configs +mkdir -p "$NVIM_CFG/lua/configs" -cat >~/.config/nvim/lua/configs/lspconfig.lua <<'LUA' +cat >"$NVIM_CFG/lua/configs/lspconfig.lua" <<'LUA' local configs = require "nvchad.configs.lspconfig" local on_attach = configs.on_attach @@ -91,7 +124,7 @@ lspconfig.clangd.setup { LUA # ── Keymaps ───────────────────────────────────────────────────── -cat >~/.config/nvim/lua/mappings.lua <<'LUA' +cat >"$NVIM_CFG/lua/mappings.lua" <<'LUA' require "nvchad.mappings" local map = vim.keymap.set diff --git a/lib/shell-config.sh b/lib/shell-config.sh index e99e1cb..309d82a 100644 --- a/lib/shell-config.sh +++ b/lib/shell-config.sh @@ -3,11 +3,7 @@ step "Finalizing shell config" -# Append archduino block to .zshrc if not already present -if ! grep -q "ARCHDUINO" ~/.zshrc 2>/dev/null; then - cat >>~/.zshrc <<'ZSHRC' - -# ── ARCHDUINO CONFIG ──────────────────────────────────────────── +ZSHRC_BLOCK='# ── ARCHDUINO CONFIG ──────────────────────────────────────────── plugins=(git zsh-autosuggestions zsh-syntax-highlighting) export PATH="$HOME/.local/bin:$PATH" @@ -30,11 +26,21 @@ alias acm="arduino-cli monitor" alias acb="arduino-cli board list" alias lg="lazygit" alias v="nvim" -# ── END ARCHDUINO ─────────────────────────────────────────────── -ZSHRC - info "Shell config updated" +# ── END ARCHDUINO ───────────────────────────────────────────────' + +if grep -q "ARCHDUINO CONFIG" ~/.zshrc 2>/dev/null; then + # Update: replace existing block + backup_config ~/.zshrc + # Remove old block and write new one + sed -i '/^# ── ARCHDUINO CONFIG/,/^# ── END ARCHDUINO/d' ~/.zshrc + echo "" >> ~/.zshrc + echo "$ZSHRC_BLOCK" >> ~/.zshrc + info "Shell config updated (replaced Archduino block)" else - info "Shell config already contains Archduino block" + # Fresh install: append block + echo "" >> ~/.zshrc + echo "$ZSHRC_BLOCK" >> ~/.zshrc + info "Shell config updated" fi # ── WSL2 extras ───────────────────────────────────────────────── diff --git a/lib/starship.sh b/lib/starship.sh index e6774bc..0ed8b9a 100644 --- a/lib/starship.sh +++ b/lib/starship.sh @@ -3,15 +3,14 @@ step "3/9 — Installing Starship" -if ! command -v starship &>/dev/null; then - if $IS_ARCH; then - $PKG_INSTALL starship - else - curl -ksS https://starship.rs/install.sh | sh -s -- -y - fi +if $IS_ARCH; then + $PKG_INSTALL starship +else + curl -ksS https://starship.rs/install.sh | sh -s -- -y fi -info "Starship installed" +info "Starship installed/updated" mkdir -p ~/.config +backup_config ~/.config/starship.toml write_starship_config ~/.config/starship.toml info "Starship configured with $THEME_DISPLAY_NAME" diff --git a/lib/zellij.sh b/lib/zellij.sh index 6b81b17..dd08fbe 100644 --- a/lib/zellij.sh +++ b/lib/zellij.sh @@ -3,17 +3,16 @@ step "4/9 — Installing Zellij" -if ! command -v zellij &>/dev/null; then - if $IS_ARCH; then - $PKG_INSTALL zellij - else - curl -kL https://github.com/zellij-org/zellij/releases/latest/download/zellij-x86_64-unknown-linux-musl.tar.gz | tar xz -C /tmp - sudo mv /tmp/zellij /usr/local/bin/ - fi +if $IS_ARCH; then + $PKG_INSTALL zellij +else + curl -kL https://github.com/zellij-org/zellij/releases/latest/download/zellij-x86_64-unknown-linux-musl.tar.gz | tar xz -C /tmp + sudo mv /tmp/zellij /usr/local/bin/ fi -info "Zellij installed" +info "Zellij installed/updated" mkdir -p ~/.config/zellij/layouts +backup_config ~/.config/zellij/config.kdl write_zellij_config ~/.config/zellij/config.kdl cat >~/.config/zellij/layouts/arduino.kdl <<'LAYOUT' diff --git a/lib/zsh.sh b/lib/zsh.sh index c4cba05..9881001 100644 --- a/lib/zsh.sh +++ b/lib/zsh.sh @@ -13,10 +13,12 @@ fi ZSH_CUSTOM="${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}" -[[ ! -d "$ZSH_CUSTOM/plugins/zsh-autosuggestions" ]] && - git clone https://github.com/zsh-users/zsh-autosuggestions "$ZSH_CUSTOM/plugins/zsh-autosuggestions" +for plugin in zsh-autosuggestions zsh-syntax-highlighting; do + if [[ ! -d "$ZSH_CUSTOM/plugins/$plugin" ]]; then + git clone "https://github.com/zsh-users/$plugin" "$ZSH_CUSTOM/plugins/$plugin" + else + git -C "$ZSH_CUSTOM/plugins/$plugin" pull --quiet 2>/dev/null || true + fi +done -[[ ! -d "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" ]] && - git clone https://github.com/zsh-users/zsh-syntax-highlighting "$ZSH_CUSTOM/plugins/zsh-syntax-highlighting" - -info "Zsh plugins installed" +info "Zsh plugins installed/updated"