diff --git a/install.bash b/install.bash index 95296da..2fbab4e 100644 --- a/install.bash +++ b/install.bash @@ -2,6 +2,9 @@ set -eo pipefail DEPS_PATH="${HOME}/.local/share" +SKIP_PKG_INSTALL=false +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +PKG_INSTALL_CMD="" BOLD=$(tput bold) CYAN=$(tput setaf 6) @@ -9,6 +12,7 @@ GREEN=$(tput setaf 2) YELLOW=$(tput setaf 3) RED=$(tput setaf 1) RESET=$(tput sgr0) +RESET_BOLD="${RESET}${BOLD}" log() { # Print a message and send it to stdout or stderr depending upon log level, also configurable with debug etc. @@ -18,8 +22,6 @@ log() { # - The log level, defined within a case check in this function # message # - The info message - # line_number - # - The line number of the calling function (${LINNO}) # # Usage: # log "info" "Could not find that directory" @@ -30,11 +32,10 @@ log() { # Convert the level to uppercase local level - level=$(echo "${1}" | tr '[:lower:]' '[:upper:]') - shift + level=$(printf "%s" "${1}" | tr '[:lower:]' '[:upper:]') local message - message="${*}" + message="${2}" case "${level}" in INFO) @@ -64,13 +65,8 @@ log() { "${RESET}" >&2 return 0 ;; - # Further log levels can be added by extending this switch statement with more comparisons + # Further log levels can be added by extending this switch statement with more comparisons - *) # Default case, no matches - # Returns non-zero code as an improper log option was passed, this helps with using `set -e` - log "ERROR" "Invalid log level passed, received level \"${level}\" with message \"${message}\"" - return 1 - ;; esac } @@ -81,20 +77,29 @@ print-break() { log "${@}" install-fzf() { - local install_path="$/fzf" - git clone --depth 1 https://github.com/junegunn/fzf.git "${install_path}" - "${install_path}/install" \ - --key-bindings \ - --completion \ - --no-update-rc \ - --xdg >/dev/null + 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 } install-rust() { local install_path="${1}" export CARGO_HOME="${install_path}/cargo" export RUSTUP_HOME="${install_path}/rustup" - curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --quiet + + if ! [[ -e "${CARGO_HOME}" ]] || ! [[ -e "${RUSTUP_HOME}" ]]; then + curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- -y --quiet + else + log "info" "${GREEN}Rust${RESET_BOLD} already installed, skipping" + fi export PATH="${PATH}:${install_path}/cargo/bin" } @@ -112,17 +117,64 @@ check-script-deps() { local ret_code=0 for cmd in "${commands_to_check[@]}"; do - if ! command -v "${cmd}"; then - log "warning" "Could not find command: ${GREEN}${cmd}${RESET}" + 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}" ret_code=1 + else + log "info" "Found dependency ${GREEN}${cmd}${RESET_BOLD}" fi done return "${ret_code}" } +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 + + log "info" "Copying ${GREEN}${install_key}${RESET_BOLD}, ${GREEN}${src_path}${RESET_BOLD} to ${GREEN}${dest_path}${RESET_BOLD}" + if ! cp -r "${src_path}" "${dest_path}"; then + log "error" "Failed copying ${GREEN}${install_key}${RESET_BOLD}, ${GREEN}${src_path}${RESET_BOLD} to ${GREEN}${dest_path}${RESET_BOLD}" + return 1 + fi + done +} + main() { - log "info" "Dependencies directory set to ${GREEN}${DEPS_PATH}${RESET}" + 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}" mkdir -p "${DEPS_PATH}" print-break @@ -133,20 +185,20 @@ main() { log "info" "Script dependencies good" print-break - log "info" "Installing ${GREEN}FZF${RESET}" + log "info" "Installing ${GREEN}FZF${RESET_BOLD}" if ! install-fzf "${DEPS_PATH}"; then - log "error" "Failed to install ${GREEN}FZF${RESET}" + log "error" "Failed to install ${GREEN}FZF${RESET_BOLD}" exit 1 fi - log "info" "Successfully installed ${GREEN}FZF${RESET}" + log "info" "Successfully installed ${GREEN}FZF${RESET_BOLD}" print-break log "info" "Installing Rust" if ! install-rust "${DEPS_PATH}"; then - log "error" "Failed to install ${GREEN}Rust${RESET}" + log "error" "Failed to install ${GREEN}Rust${RESET_BOLD}" exit 1 fi - log "info" "Successfully installed ${GREEN}Rust${RESET}" + log "info" "Successfully installed ${GREEN}Rust${RESET_BOLD}" print-break log "info" "Installing Cargo programs" @@ -155,13 +207,34 @@ main() { exa ) for pkg in "${cargo_binaries[@]}"; do - log "info" "Installing ${GREEN}${pkg}${RESET}" + log "info" "Attempting install of ${GREEN}${pkg}${RESET_BOLD}" if ! install-cargo-binary "${pkg}"; then - log "error" "Failed installation of ${GREEN}${pkg}${RESET}" + log "error" "Failed installation of ${GREEN}${pkg}${RESET_BOLD}" exit 1 fi done log "info" "Finished installing Cargo programs" + 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" } -main +main "${@}"