install.sh os agnostic

This commit is contained in:
Harivansh Rathi 2025-12-29 22:05:54 +05:30
parent 7fdd98ee35
commit ab56d93fac
2 changed files with 222 additions and 80 deletions

View file

@ -4,5 +4,5 @@
## use it now ## use it now
```bash ```bash
bash <(curl -s https://raw.githubusercontent.com/harivansh-afk/nvim/main/install.sh) curl -fsSL https://raw.githubusercontent.com/harivansh-afk/nvim/main/install.sh | bash
``` ```

View file

@ -1,104 +1,246 @@
#!/bin/bash #!/usr/bin/env bash
set -euo pipefail
# Colors for output if [[ -t 1 ]]; then
RED='\033[0;31m' RED=$'\033[0;31m'
GREEN='\033[0;32m' GREEN=$'\033[0;32m'
YELLOW='\033[1;33m' YELLOW=$'\033[1;33m'
NC='\033[0m' # No Color NC=$'\033[0m'
else
RED=""
GREEN=""
YELLOW=""
NC=""
fi
log() { printf "%s%s%s\n" "${GREEN}" "$*" "${NC}"; }
warn() { printf "%s%s%s\n" "${YELLOW}" "$*" "${NC}" >&2; }
die() { printf "%sError:%s %s\n" "${RED}" "${NC}" "$*" >&2; exit 1; }
have() { command -v "$1" >/dev/null 2>&1; }
REPO_URL="https://github.com/harivansh-afk/nvim.git" REPO_URL="https://github.com/harivansh-afk/nvim.git"
NVIM_CONFIG_DIR="$HOME/.config/nvim" SKIP_NVIM=0
BACKUP_DIR="$HOME/.config/nvim.backup.$(date +%Y%m%d_%H%M%S)" SKIP_CONFIG=0
NO_PATH=0
echo -e "${GREEN}Installing harivansh-afk/nvim configuration...${NC}" while [[ $# -gt 0 ]]; do
case "$1" in
--repo)
[[ $# -ge 2 ]] || die "--repo requires a value"
REPO_URL="$2"
shift 2
;;
--skip-nvim) SKIP_NVIM=1; shift ;;
--skip-config) SKIP_CONFIG=1; shift ;;
--no-path) NO_PATH=1; shift ;;
-h|--help)
cat <<'EOF'
Usage: install.sh [options]
# Check if git is installed Options:
if ! command -v git &> /dev/null; then --repo URL Clone config from URL (default: repo in this script)
echo -e "${RED}Error: git is not installed. Please install git first.${NC}" --skip-nvim Do not install Neovim
exit 1 --skip-config Do not install config
fi --no-path Do not modify shell rc files
EOF
# Check if nvim is installed exit 0
if ! command -v nvim &> /dev/null; then ;;
echo -e "${YELLOW}Neovim not found. Installing via appimage (no sudo required)...${NC}" *)
die "Unknown argument: $1 (use --help)"
cd /tmp ;;
rm -f nvim.appimage esac
# Use stable release URL (more reliable than 'latest' redirect)
NVIM_URL="https://github.com/neovim/neovim/releases/download/stable/nvim-linux-x86_64.appimage"
echo "Downloading from $NVIM_URL..."
if command -v wget &> /dev/null; then
wget -q --show-progress -O nvim.appimage "$NVIM_URL"
else
curl -L -o nvim.appimage "$NVIM_URL"
fi
chmod u+x nvim.appimage
# Try to extract (works on most systems)
./nvim.appimage --appimage-extract > /dev/null 2>&1
mkdir -p "$HOME/.local/bin"
if [ -d "squashfs-root" ]; then
# Use extracted version
rm -rf "$HOME/.local/nvim"
mv squashfs-root "$HOME/.local/nvim"
ln -sf "$HOME/.local/nvim/AppRun" "$HOME/.local/bin/nvim"
rm -f nvim.appimage
else
# Use appimage directly
mv nvim.appimage "$HOME/.local/bin/nvim"
fi
# Add to PATH
export PATH="$HOME/.local/bin:$PATH"
# Add to shell rc files if not already there
for rc in "$HOME/.bashrc" "$HOME/.zshrc"; do
if [ -f "$rc" ]; then
if ! grep -q '.local/bin' "$rc" 2>/dev/null; then
echo 'export PATH="$HOME/.local/bin:$PATH"' >> "$rc"
echo -e "${GREEN}Added ~/.local/bin to PATH in $(basename $rc)${NC}"
fi
fi
done done
cd - > /dev/null OS="$(uname -s)"
ARCH_RAW="$(uname -m)"
case "$OS" in
Darwin) OS="macos" ;;
Linux) OS="linux" ;;
*) die "Unsupported OS: $OS" ;;
esac
case "$ARCH_RAW" in
x86_64|amd64) ARCH="x86_64" ;;
arm64|aarch64) ARCH="arm64" ;;
*) die "Unsupported architecture: $ARCH_RAW" ;;
esac
LOCAL_BIN="${HOME}/.local/bin"
LOCAL_NVIM_DIR="${HOME}/.local/nvim"
CONFIG_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/nvim"
BACKUP_DIR="${CONFIG_DIR}.backup.$(date +%Y%m%d_%H%M%S)"
require_downloader() {
if have curl; then
DOWNLOADER="curl"
elif have wget; then
DOWNLOADER="wget"
else
die "Missing downloader: install curl or wget"
fi
}
download() {
local url="$1"
local dest="$2"
if [[ "${DOWNLOADER}" == "curl" ]]; then
curl -fsSL "$url" -o "$dest"
else
wget -q --show-progress -O "$dest" "$url"
fi
}
ensure_local_bin_path() {
export PATH="${LOCAL_BIN}:${PATH}"
[[ "$NO_PATH" -eq 1 ]] && return 0
local export_line='export PATH="$HOME/.local/bin:$PATH"'
local rc_candidates=()
case "${SHELL:-}" in
*/zsh) rc_candidates+=("$HOME/.zshrc") ;;
*/bash) rc_candidates+=("$HOME/.bashrc") ;;
*) rc_candidates+=("$HOME/.zshrc" "$HOME/.bashrc") ;;
esac
for rc in "${rc_candidates[@]}"; do
[[ -f "$rc" ]] || continue
if ! grep -qF "$export_line" "$rc" 2>/dev/null; then
printf "\n%s\n" "$export_line" >> "$rc"
log "Added ~/.local/bin to PATH in $(basename "$rc")"
fi
done
}
install_nvim_macos_tarball() {
require_downloader
mkdir -p "$LOCAL_BIN"
local tmp
tmp="$(mktemp -d 2>/dev/null || mktemp -d -t nvim-install)"
trap 'rm -rf "$tmp"' EXIT
local tarball="${tmp}/nvim.tar.gz"
local urls=(
"https://github.com/neovim/neovim/releases/download/stable/nvim-macos-${ARCH}.tar.gz"
"https://github.com/neovim/neovim/releases/download/stable/nvim-macos.tar.gz"
)
local downloaded=0
for url in "${urls[@]}"; do
if download "$url" "$tarball" 2>/dev/null; then
downloaded=1
break
fi
done
[[ "$downloaded" -eq 1 ]] || die "Failed to download Neovim for macOS (${ARCH})"
tar -xzf "$tarball" -C "$tmp"
local extracted_dir
extracted_dir="$(find "$tmp" -maxdepth 2 -type f -path '*/bin/nvim' -print -quit | xargs -I{} dirname "{}" | xargs -I{} dirname "{}")"
[[ -n "$extracted_dir" ]] || die "Downloaded archive did not contain bin/nvim"
rm -rf "$LOCAL_NVIM_DIR"
mv "$extracted_dir" "$LOCAL_NVIM_DIR"
ln -sf "${LOCAL_NVIM_DIR}/bin/nvim" "${LOCAL_BIN}/nvim"
}
install_nvim_linux_appimage() {
require_downloader
mkdir -p "$LOCAL_BIN"
local tmp
tmp="$(mktemp -d 2>/dev/null || mktemp -d -t nvim-install)"
trap 'rm -rf "$tmp"' EXIT
local appimage="${tmp}/nvim.appimage"
local url="https://github.com/neovim/neovim/releases/download/stable/nvim-linux-${ARCH}.appimage"
download "$url" "$appimage" || die "Failed to download Neovim AppImage for Linux (${ARCH})"
chmod u+x "$appimage"
(cd "$tmp" && ./nvim.appimage --appimage-extract >/dev/null 2>&1) || true
if [[ -d "${tmp}/squashfs-root" ]]; then
rm -rf "$LOCAL_NVIM_DIR"
mv "${tmp}/squashfs-root" "$LOCAL_NVIM_DIR"
ln -sf "${LOCAL_NVIM_DIR}/AppRun" "${LOCAL_BIN}/nvim"
else
mv "$appimage" "${LOCAL_BIN}/nvim"
chmod u+x "${LOCAL_BIN}/nvim"
fi
}
install_nvim() {
if have nvim; then
log "Neovim already installed: $(nvim --version | head -1)"
return 0
fi fi
# Verify nvim installation log "Neovim not found; installing (no sudo)..."
if ! command -v nvim &> /dev/null && [ ! -x "$HOME/.local/bin/nvim" ]; then ensure_local_bin_path
echo -e "${RED}Failed to install neovim.${NC}"
exit 1 if [[ "$OS" == "macos" ]]; then
if have brew; then
if brew list neovim >/dev/null 2>&1; then
brew upgrade neovim >/dev/null || true
else
brew install neovim
fi
else
install_nvim_macos_tarball
fi
else
install_nvim_linux_appimage
fi fi
# Ensure PATH includes local bin ensure_local_bin_path
export PATH="$HOME/.local/bin:$PATH" have nvim || die "Neovim install completed but 'nvim' is not on PATH"
log "Neovim version: $(nvim --version | head -1)"
}
echo -e "${GREEN}Neovim version: $(nvim --version | head -1)${NC}" install_config() {
have git || die "git is required (install git first)"
mkdir -p "$(dirname "$CONFIG_DIR")"
# Backup existing config if it exists if [[ -d "$CONFIG_DIR" ]]; then
if [ -d "$NVIM_CONFIG_DIR" ]; then if [[ -d "${CONFIG_DIR}/.git" ]]; then
echo -e "${YELLOW}Backing up existing nvim config to $BACKUP_DIR${NC}" local remote
mv "$NVIM_CONFIG_DIR" "$BACKUP_DIR" remote="$(git -C "$CONFIG_DIR" remote get-url origin 2>/dev/null || true)"
if [[ "$remote" == "$REPO_URL" ]]; then
if git -C "$CONFIG_DIR" diff --quiet && git -C "$CONFIG_DIR" diff --cached --quiet; then
log "Updating existing config in ${CONFIG_DIR}..."
git -C "$CONFIG_DIR" pull --ff-only || true
mkdir -p "${CONFIG_DIR}/undodir"
return 0
fi
fi
fi fi
# Ensure .config directory exists warn "Backing up existing config to ${BACKUP_DIR}"
mkdir -p "$HOME/.config" mv "$CONFIG_DIR" "$BACKUP_DIR"
fi
# Clone the repository log "Cloning config into ${CONFIG_DIR}..."
echo -e "${GREEN}Cloning nvim config from $REPO_URL...${NC}" git clone "$REPO_URL" "$CONFIG_DIR"
git clone "$REPO_URL" "$NVIM_CONFIG_DIR" mkdir -p "${CONFIG_DIR}/undodir"
}
# Create undodir log "Installing nvim config from ${REPO_URL} (${OS}/${ARCH})"
mkdir -p "$NVIM_CONFIG_DIR/undodir" ensure_local_bin_path
echo -e "${GREEN}Installation complete!${NC}" if [[ "$SKIP_NVIM" -eq 0 ]]; then
echo "" install_nvim
echo -e "${YELLOW}IMPORTANT: Run this to use nvim now:${NC}" fi
echo -e " source ~/.bashrc && nvim"
echo "" if [[ "$SKIP_CONFIG" -eq 0 ]]; then
echo "Or start a new terminal session and run: nvim" install_config
fi
if have nvim; then
# Basic sanity check: confirm Neovim can start headlessly.
nvim --headless "+qall" >/dev/null 2>&1 || warn "Neovim failed to start headlessly; run 'nvim' to see errors."
fi
log "Done."
if [[ "$NO_PATH" -eq 0 ]]; then
warn "Open a new terminal (or 'source' your shell rc) if 'nvim' isn't found."
fi