installs/neovim.sh
2025-05-24 00:42:56 +02:00

260 lines
8 KiB
Bash

{
#!/usr/bin/env bash
# ─< Check if the given command exists silently >─────────────────────────────────────────
command_exists() {
command -v "$@" >/dev/null 2>&1
}
source-script() {
local url="$1"
local import="$(mktemp)"
# ─< if $1 is a local file, source this one instead >─────────────────────────────────────
if [ -f "$url" ]; then
source "$url"
sleep 0.1
return 0
else
# ─< if $1 is a url, grab it and source it, also deletes afterwards >─────────────────────
if command_exists curl; then
curl -fsSL $url -o $import
elif command_exists wget; then
wget -o $import $url
else
echo "curl/wget is required, but missing.."
exit 69
fi
source "$import"
echo "${BLUE}Sourcing external script:${NC} $url"
sleep 0.1
rm -f "$import"
fi
}
getDependencies() {
local err
local depsDebian=(git ninja-build gettext cmake curl build-essential)
local depsFedora=(git ninja-build cmake gcc make gettext curl glibc-gconv-extra)
local depsOpensuse=(git ninja cmake gcc-c++ gettext-tools curl)
local depsArch=(git base-devel cmake ninja curl)
local depsAlpine=(git build-base cmake coreutils curl gettext-tiny-dev)
declare -A deps=(
[debian]="depsDebian"
[ubuntu]="depsDebian"
[opensuse]="depsOpensuse"
[fedora]="depsFedora"
[arch]="depsArch"
[alpine]="depsAlpine"
)
declare -n pkgArray="${deps[$distro]}"
case "$distro" in
debian | ubuntu | arch | fedora | alpine | opensuse)
check-and-install ${pkgArray[@]}
;;
*)
echo-error "Cannot install for $distro"
exit 69
;;
esac
}
cloneSources() {
local err
cloneDir="$(mktemp -d)"
cd $cloneDir || mkdir $cloneDir && cd $cloneDir
line
# spin yellow "Cloning $(pen bold red $PACKAGE) $(pen yellow 'sources at') $(pen red $cloneDir/neovim).."
if command_exists git; then
if run --err err git clone --depth=1 https://github.com/neovim/neovim.git; then
check green "Cloned $PACKAGE sources"
cd neovim
else
throw "Error cloning neovim"
pen bold red "The error: ${err:-}"
fi
# minifyed version..
# run --err err git clone --depth=1 https://github.com/neovim/neovim.git && check green "Cloned $PACKAGE sources" && cd neovim || throw "Error cloning neovim" && pen bold red "The error: ${err:-}"
else
echo-error "Git was required, but is missing.. exiting now"
return 69
fi
}
makeInstall() {
local err
spin green bold "Compiling $PACKAGE from source"
if run --err err make CMAKE_BUILD_TYPE=RelWithDebInfo; then
check "Compiled $PACKAGE from source"
line
# echo_pkg install
spin bold "Installing $PACKAGE"
if run --err err $_sudo make install; then
check "$PACKAGE installed!"
else
thorw "Installation of $PACKAGE has failed!"
echo-error "${err:-}"
fi
else
echo-error "${err:-}"
exit 69
fi
}
checkAndInitConfig() {
local err
if [ -d "$HOME/.config/nvim/" ]; then
# echo_pkg qol "Prefetching neovim setup configuration.."
spin "Prefetching neovim setup configuration.."
if run --err err nvim --headless +q; then
check "Prefetched config"
else
throw "Prefetching config went terribly wrong!"
echo-error "${err:-}"
fi
else
# pen bold yellow "You don't have a neovim config installed. Do you want to clone one now? [Y]es (standard), [m]inimal, [n]o"
# read -r askNvim
choose askNvim "You don't have a neovim config installed. Do you want to clone one now?" "pika's standard config" "pika's minimal config" own none </dev/tty
case "$askNvim" in
"pika's standard config")
# git clone --recursive --depth=1 https://git.k4li.de/dotfiles/nvim.git "$HOME/.config/nvim"
# sleep 0.2
local err
local url="https://git.k4li.de/dotfiles/nvim.git"
spin yellow "Cloning pika's standard config"
# if run install-stuff; then
if run --err err git clone --recursive --depth=1 $url "$HOME/.config/nvim"; then
line
check "Cloned neovim config"
else
throw "Clone failed!"
pen "${err:-}"
fi
;;
"pika's minimal config")
local url="https://git.k4li.de/dotfiles/nvim-mini.git"
local err
spin yellow "Cloning pika's mini config"
# if run install-stuff; then
if run --err err git clone --recursive --depth=1 $url "$HOME/.config/nvim"; then
line
check "Cloned neovim config"
else
throw "Clone failed!"
pen "${err:-}"
fi
;;
none) exit 0 ;;
own)
local url err
seek url "What is the url for your neovim config?" </dev/tty
spin yellow "Cloning your own config!"
if run --err err git clone --recursive --depth=1 $url "$HOME/.config/nvim"; then
check "Cloned neovim config"
else
throw "Clone failed!"
pen "${err:-}"
fi
;;
*)
echo-error "Something failed HARD!"
exit 69
;;
esac
fi
if [ -d "$HOME/.config/nvim/" ]; then
spin bold yellow "Also sourcing config!"
if run --err err nvim --headless +q; then
check "Prefetching config was successfull"
else
throw "Prefetching has failed hard!"
pen "${err:-}"
fi
fi
}
main() {
if $silent; then
pen bold yellow "Executing script silently!"
fi
if ! getDependencies; then
echo-error "Error when installing dependencies.."
fi
cloneSources
makeInstall
checkAndInitConfig </dev/tty
}
# WHY:
# This import will give you the following variables:
# _sudo="sudo -E" <- only if non root user
# distro = <distro name, like 'arch', 'debian', 'fedora'..>
# arch = bool
# fedora = bool
# opensuse = bool....
# You can then use it for, `if $arch; then`
# Also this gives you the pkg-install command, which installs a package pased on the packagemanager/distro used.
# CAUTION:
# This only wokrs for generic package names, like neovim, or vim, or tmux etc..
# not every package packagemanager has the same packagenames for their packages..
setup-env() {
# local beddu=https://git.k4li.de/scripts/beddu/raw/branch/main/dist/beddu.sh
# local pika=https://git.k4li.de/scripts/imports/raw/branch/main/distros.sh
local dream=https://git.k4li.de/scripts/imports/raw/branch/main/dream.sh
if ! command_exists pkg-install && ! command_exists check-and-install && ! command_exists spin; then
source-script $dream
fi
}
if setup-env; then
# ─< package variable >───────────────────────────────────────────────────────────────────
unset PACKAGE
# ─< argument list variables >────────────────────────────────────────────────────────────
silent=false
sleep 0.1
PACKAGE=neovim
if command_exists "$PACKAGE"; then
pen bold yellow "$PACKAGE is already installed!"
pen bold yellow "Exiting now!"
exit 69
fi
# ─< parse arguments and get variable contents >──────────────────────────────────────────
for arg in "$@"; do
case "$arg" in
--silent | -s)
export silent=true
;;
esac
done
main </dev/tty
fi
pen grey bold "Cleaning up old $cloneDir directory.."
rm -rf "$cloneDir"
}