zsh/install.bash

254 lines
6.5 KiB
Bash
Raw Normal View History

2022-07-30 20:11:35 -05:00
#!/usr/bin/env bash
set -eo pipefail
2022-07-30 21:13:25 -05:00
DEPS_PATH="${HOME}/.local/share"
2022-07-30 22:24:52 -05:00
SKIP_PKG_INSTALL=false
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
PKG_INSTALL_CMD=""
2022-07-30 21:13:25 -05:00
BOLD=$(tput bold)
CYAN=$(tput setaf 6)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
2022-07-30 21:19:56 -05:00
RED=$(tput setaf 1)
2022-07-30 21:13:25 -05:00
RESET=$(tput sgr0)
2022-07-30 22:24:52 -05:00
RESET_BOLD="${RESET}${BOLD}"
2022-07-30 21:13:25 -05:00
log() {
# Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc.
#
# Arguments:
# level <type: string> <position: 1> <required: true>
# - The log level, defined within a case check in this function
# message <type: string> <position: 2> <required: true>
# - The info message
#
# Usage:
# log "info" "Could not find that directory"
#
# POSIX Compliant:
# Yes
#
# Convert the level to uppercase
local level
2022-07-30 22:24:52 -05:00
level=$(printf "%s" "${1}" | tr '[:lower:]' '[:upper:]')
2022-07-30 21:13:25 -05:00
local message
2022-07-30 22:24:52 -05:00
message="${2}"
2022-07-30 21:13:25 -05:00
case "${level}" in
INFO)
printf "%sINFO:%s %s%s%s\n" \
"${CYAN}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
WARN*)
printf "%sWARN:%s %s%s%s\n" \
"${YELLOW}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
ERROR)
printf "%sERROR:%s %s%s%s\n" \
"${RED}" \
"${RESET}" \
"${BOLD}" \
"${message}" \
"${RESET}" >&2
return 0
;;
2022-07-30 22:24:52 -05:00
# Further log levels can be added by extending this switch statement with more comparisons
2022-07-30 21:13:25 -05:00
esac
}
print-break() {
printf "${GREEN}%.s─${RESET}" $(seq 1 "$(tput cols)")
}
log "${@}"
2022-07-30 20:11:35 -05:00
install-fzf() {
2022-07-30 22:24:52 -05:00
local install_path="${1}/fzf"
if ! [[ -e "${install_path}" ]]; then
git clone --depth 1 https://github.com/junegunn/fzf.git "${install_path}"
"${install_path}/install" \
--key-bindings \
--completion \
--no-update-rc \
--xdg >/dev/null
else
log "info" "${GREEN}FZF${RESET_BOLD} already installed, skipping"
fi
2022-07-30 21:13:25 -05:00
}
install-rust() {
local install_path="${1}"
export CARGO_HOME="${install_path}/cargo"
export RUSTUP_HOME="${install_path}/rustup"
2022-07-30 22:24:52 -05:00
if ! [[ -e "${CARGO_HOME}" ]] || ! [[ -e "${RUSTUP_HOME}" ]]; then
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | ${RUSTUP_HOME} sh -s -- -y --quiet
2022-07-30 22:24:52 -05:00
else
log "info" "${GREEN}Rust${RESET_BOLD} already installed, skipping"
fi
2022-07-30 21:13:25 -05:00
export PATH="${PATH}:${install_path}/cargo/bin"
2022-07-30 22:30:20 -05:00
2022-07-30 22:36:41 -05:00
if [[ -z "$(ls "${RUSTUP_HOME}/toolchains")" ]]; then
log "info" "No toolchain found, installing a ${GREEN}Rust${RESET_BOLD} toolchain"
log "info" "Setting ${GREEN}Rust{$RESET_BOLD}'s toolchain to ${GREEN}stable${RESET_BOLD}"
(
printf "%s" "rustup default stable" | zsh
)
2022-07-30 22:36:41 -05:00
else
log "info" "${GREEN}Rust${RESET_BOLD} toolchain found, skipping toolchain setup"
fi
2022-07-30 21:13:25 -05:00
}
install-cargo-binary() {
local binary="${1}"
cargo install "${binary}"
2022-07-30 20:11:35 -05:00
}
check-script-deps() {
2022-07-30 21:13:25 -05:00
local commands_to_check=(
2022-07-30 20:11:35 -05:00
git
2022-07-30 21:13:25 -05:00
gcc
2022-07-30 20:11:35 -05:00
)
2022-07-30 21:13:25 -05:00
local ret_code=0
2022-07-30 20:11:35 -05:00
for cmd in "${commands_to_check[@]}"; do
2022-07-30 22:24:52 -05:00
if ! command -v "${cmd}" >/dev/null; then
log "warning" "Could not find command: ${GREEN}${cmd}${RESET_BOLD}, attempting automatic installation"
install-from-pkg-mngr "${cmd}"
2022-07-30 21:13:25 -05:00
ret_code=1
2022-07-30 22:24:52 -05:00
else
log "info" "Found dependency ${GREEN}${cmd}${RESET_BOLD}"
2022-07-30 20:11:35 -05:00
fi
done
2022-07-30 21:13:25 -05:00
2022-07-30 21:18:28 -05:00
return "${ret_code}"
2022-07-30 20:11:35 -05:00
}
2022-07-30 22:24:52 -05:00
install-from-pkg-mngr() {
if [[ "${SKIP_PKG_INSTALL}" = true ]]; then
return
fi
local pkg_to_install="${*}"
log "info" "Installing ${GREEN}${pkg_to_install}${RESET_BOLD}"
if ! "${PKG_INSTALL_CMD} ${pkg_to_install}"; then
log "error" "Failed to install ${GREEN}${pkg_to_install}${RESET_BOLD}"
fi
log "info" "Successfully installed ${GREEN}${pkg_to_install}${RESET_BOLD}"
}
deploy-config() {
local install_paths
declare -A install_paths=(
["zshrc"]=".zshrc"
["ZSH-Config"]=".config/zsh"
)
for install_key in "${!install_paths[@]}"; do
local src_path="${SCRIPT_DIR}/${install_paths[${install_key}]}"
local dest_path="${HOME}/${install_paths[${install_key}]}"
if [[ -e "${dest_path}" ]]; then
log "info" "${GREEN}${install_key}${RESET_BOLD} already exists at ${GREEN}${dest_path}${RESET_BOLD}, skipping"
continue
fi
2022-07-30 22:27:28 -05:00
log "info" "Linking ${GREEN}${install_key}${RESET_BOLD}, ${GREEN}${src_path}${RESET_BOLD} to ${GREEN}${dest_path}${RESET_BOLD}"
if ! ln -s "${src_path}" "${dest_path}"; then
log "error" "Failed linking ${GREEN}${install_key}${RESET_BOLD}, ${GREEN}${src_path}${RESET_BOLD} to ${GREEN}${dest_path}${RESET_BOLD}"
2022-07-30 22:24:52 -05:00
return 1
fi
done
}
2022-07-30 20:11:35 -05:00
main() {
local tasks_done=()
2022-07-30 22:24:52 -05:00
PKG_INSTALL_CMD="${*}"
if [[ -z "${PKG_INSTALL_CMD}" ]]; then
log "error" "A package installer must be passed to install missing packages, or ${GREEN}SKIP_PKG_INSTALL=TRUE${RESET_BOLD} must be set."
return 1
fi
log "info" "Dependencies directory set to ${GREEN}${DEPS_PATH}${RESET_BOLD}"
log "info" "Packager install command set to ${GREEN}${PKG_INSTALL_CMD}${RESET_BOLD}"
2022-07-30 20:11:35 -05:00
mkdir -p "${DEPS_PATH}"
2022-07-30 21:13:25 -05:00
print-break
2022-07-30 20:11:35 -05:00
2022-07-30 21:13:25 -05:00
log "info" "Checking script dependencies..."
2022-07-30 20:11:35 -05:00
if ! check-script-deps; then
2022-07-30 21:13:25 -05:00
log "error" "Script dependencies failed, install missing dependencies and try again\n"
fi
log "info" "Script dependencies good"
print-break
2022-07-30 22:24:52 -05:00
log "info" "Installing ${GREEN}FZF${RESET_BOLD}"
2022-07-30 21:13:25 -05:00
if ! install-fzf "${DEPS_PATH}"; then
2022-07-30 22:24:52 -05:00
log "error" "Failed to install ${GREEN}FZF${RESET_BOLD}"
2022-07-30 21:13:25 -05:00
exit 1
fi
2022-07-30 22:24:52 -05:00
log "info" "Successfully installed ${GREEN}FZF${RESET_BOLD}"
tasks_done+=("Install FZF")
2022-07-30 21:13:25 -05:00
print-break
log "info" "Installing Rust"
if ! install-rust "${DEPS_PATH}"; then
2022-07-30 22:24:52 -05:00
log "error" "Failed to install ${GREEN}Rust${RESET_BOLD}"
2022-07-30 21:13:25 -05:00
exit 1
2022-07-30 20:11:35 -05:00
fi
2022-07-30 22:24:52 -05:00
log "info" "Successfully installed ${GREEN}Rust${RESET_BOLD}"
tasks_done+=("Install Rust")
2022-07-30 21:13:25 -05:00
print-break
2022-07-30 20:11:35 -05:00
2022-07-30 21:13:25 -05:00
log "info" "Installing Cargo programs"
local cargo_binaries=(
ripgrep
exa
)
for pkg in "${cargo_binaries[@]}"; do
2022-07-30 22:24:52 -05:00
log "info" "Attempting install of ${GREEN}${pkg}${RESET_BOLD}"
2022-07-30 21:13:25 -05:00
if ! install-cargo-binary "${pkg}"; then
2022-07-30 22:24:52 -05:00
log "error" "Failed installation of ${GREEN}${pkg}${RESET_BOLD}"
2022-07-30 21:13:25 -05:00
exit 1
fi
done
log "info" "Finished installing Cargo programs"
2022-07-30 22:24:52 -05:00
print-break
log "info" "Installing ${GREEN}ZSH${RESET_BOLD}"
if ! command -v "zsh" >/dev/null; then
if ! install-from-pkg-mngr "zsh"; then
log "error" "Failed to install ${GREEN}ZSH${RESET_BOLD}"
exit 1
fi
else
log "info" "${GREEN}ZSH${RESET_BOLD} already installed, skipping"
fi
log "info" "Successfully installed ${GREEN}ZSH${RESET_BOLD}"
log "info" "Deploying ${GREEN}ZSH${RESET_BOLD} configuration files"
if ! deploy-config; then
log "error" "Failed to deploy configuration files"
exit 1
fi
log "info" "Successfully installed ${GREEN}ZSH${RESET_BOLD} configuration files"
log "info" "Finished installing ${GREEN}ZSH${RESET_BOLD} configurations and dependencies"
2022-07-30 20:11:35 -05:00
}
2022-07-30 21:16:11 -05:00
2022-07-30 22:24:52 -05:00
main "${@}"