Skip to content

Commit

Permalink
Improve install script for AskAI
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Aug 20, 2024
1 parent e90ab03 commit 312b55c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function _about_() {

function _which_() {

if command -v ${@} &>/dev/null; then
if command -v ${@} &>/dev/null || "${HHS_MY_OS_PACKMAN}" info ${@} &>/dev/null; then
return 0
fi

Expand Down
8 changes: 4 additions & 4 deletions check-badge.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 10 additions & 2 deletions dotfiles/bash/hhsrc.bash
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ export HHS_BACKUP_DIR="${HHS_DIR}/backup"
export HHS_CACHE_DIR="${HHS_DIR}/cache"
export HHS_LOG_DIR="${HHS_DIR}/log"
export HHS_LOG_FILE="${HHS_LOG_DIR}/hhsrc.log"
export HHS_MOTD_DIR="${HHS_DIR}/motd"
export HHS_PROMPTS_DIR="${HHS_DIR}/askai/prompts"
export HHS_SETUP_FILE="${HHS_DIR}/.homesetup.toml"

# if the log directory is not found, we have to create it.
Expand All @@ -65,6 +67,12 @@ export HHS_SETUP_FILE="${HHS_DIR}/.homesetup.toml"
# if the cache directory is not found, we have to create it.
[[ -d "${HHS_CACHE_DIR}" ]] || mkdir -p "${HHS_CACHE_DIR}"

# if the motd directory is not found, we have to create it.
[[ -d "${HHS_MOTD_DIR}" ]] || mkdir -p "${HHS_MOTD_DIR}"

# if the prompts directory is not found, we have to create it.
[[ -d "${HHS_PROMPTS_DIR}" ]] || mkdir -p "${HHS_PROMPTS_DIR}"

# Set path so it includes user's private bin if it exists.
[[ -d "${HOME}/bin" ]] && export PATH="${PATH}:${HOME}/bin"

Expand Down Expand Up @@ -262,8 +270,8 @@ if [[ ${HHS_RESTORE_LAST_DIR} -eq 1 && -s "${HHS_DIR}/.last_dirs" ]]; then
fi

# Print HomeSetup MOTDs.
if [[ -d "${HHS_DIR}"/motd ]]; then
all=$(find "${HHS_DIR}"/motd -type f | sort | uniq)
if [[ -d "${HHS_MOTD_DIR}" ]]; then
all=$(find "${HHS_MOTD_DIR}" -type f | sort | uniq)

for motd in ${all}; do
echo -e "$(eval "echo -e \"$(<"${motd}")\"")"
Expand Down
79 changes: 37 additions & 42 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,6 @@ Usage: $APP_NAME [OPTIONS] <args>
# Missing HomeSetup dependencies
MISSING_DEPS=()

# Missing HomeSetup applications
MISSING_APPS=()

if [[ "${MY_OS}" == "Darwin" ]]; then
MY_OS_NAME=$(sw_vers -productName)
GROUP=${GROUP:-staff}
Expand Down Expand Up @@ -317,6 +314,10 @@ Usage: $APP_NAME [OPTIONS] <args>
# Define and create the HomeSetup MOTD directory
HHS_MOTD_DIR="${HHS_DIR}/motd"

# Define and create the HomeSetup AskAI prompts directory
HHS_PROMPTS_DIR="${HHS_DIR}/askai/prompts"
create_directory "${HHS_PROMPTS_DIR}"

# Define the fonts directory
if [[ "Darwin" == "${MY_OS}" ]]; then
FONTS_DIR="${HOME}/Library/Fonts"
Expand All @@ -327,10 +328,6 @@ Usage: $APP_NAME [OPTIONS] <args>
# Create fonts directory
create_directory "${FONTS_DIR}"

# Define and create the HomeSetup AskAI prompts directory
PROMPTS_DIR="${HHS_DIR}/askai/prompts"
create_directory "${PROMPTS_DIR}"

# Check the installation method
if [[ -z "${METHOD}" ]]; then
if [[ -d "${HHS_HOME}" || -f "${HHS_HOME}/.VERSION" ]]; then
Expand All @@ -346,7 +343,7 @@ Usage: $APP_NAME [OPTIONS] <args>
# Check HomeSetup required tools
check_required_tools() {

local pad pad_len install
local pad pad_len install check_pkg

has sudo &>/dev/null && SUDO=sudo

Expand All @@ -356,15 +353,17 @@ Usage: $APP_NAME [OPTIONS] <args>
OS_APP_MAN=brew
DEPENDENCIES+=('sudo' 'xcode-select' 'portaudio' 'libmagic')
install="${SUDO} brew install -y"
check_pkg="brew info "
# Debian: Ubuntu
elif has 'apt-get'; then
elif has 'apt'; then
OS_TYPE='Debian'
OS_APP_MAN='apt-get'
OS_APP_MAN='apt'
DEPENDENCIES+=(
'sudo' 'file' 'build-essential' 'python3' 'python3-pip' 'python3-pyaudio'
'libasound-dev' 'libmagic-dev'
)
install="${SUDO} apt-get install -y"
install="${SUDO} apt install -y"
check_pkg="apt list --installed | grep"
# RedHat: Fedora, CentOS
elif has 'dnf'; then
OS_TYPE='RedHat'
Expand All @@ -374,61 +373,54 @@ Usage: $APP_NAME [OPTIONS] <args>
'python3-pyaudio' 'portaudio-devel' 'redhat-rpm-config' 'libmagic-dev'
)
install="${SUDO} yum install -y"
check_pkg="dnf list installed | grep"
# Alpine: Busybox
elif has 'apk'; then
OS_TYPE='Alpine'
OS_APP_MAN='apk'
install="apk add --no-cache"
DEPENDENCIES+=('file' 'python3' 'pip3')
install="apk add --no-cache"
check_pkg="apk list | grep"
# ArchLinux
elif has 'pacman'; then
OS_TYPE='ArchLinux'
OS_APP_MAN='pacman'
install="${SUDO} pacman -Sy"
DEPENDENCIES+=('sudo' 'file' 'python3' 'python3-pip')
install="${SUDO} pacman -Sy"
check_pkg="pacman -Q | grep"
else
quit 1 "Unable to find package manager for $(uname -s)"
fi

echo -e "\nUsing ${YELLOW}\"${OS_APP_MAN}\"${NC} application manager!\n"
echo -e "${WHITE}Checking required tools [${OS_TYPE}] ...${NC}\n"
echo -e "${WHITE}Checking required tools [${OS_TYPE}] using ${YELLOW}'${check_pkg}'${WHITE} ...${NC}\n"

pad=$(printf '%0.1s' "."{1..60})
pad_len=20

for tool_name in "${DEPENDENCIES[@]}"; do
echo -en "${ORANGE}[${OS_TYPE}] ${WHITE}Checking: ${YELLOW}${tool_name}${NC}..."
printf '%*.*s' 0 $((pad_len - ${#tool_name})) "${pad}"
if has "${tool_name}"; then
if has "${tool_name}" || ${check_pkg} "${tool_name}" >/dev/null 2>&1; then
echo -e " ${GREEN}√ INSTALLED${NC}"
else
echo -e " ${RED}X NOT INSTALLED${NC}"
MISSING_DEPS+=("${tool_name}")
fi
done

for tool_name in "${REQUIRED_APPS[@]}"; do
echo -en "${ORANGE}[${OS_TYPE}] ${WHITE}Checking: ${YELLOW}${tool_name}${NC}..."
printf '%*.*s' 0 $((pad_len - ${#tool_name})) "${pad}"
if has "${tool_name}"; then
echo -e " ${GREEN}INSTALLED${NC}"
else
echo -e " ${RED}NOT INSTALLED${NC}"
MISSING_APPS+=("${tool_name}")
fi
done

# Install packages using the default package manager
install_dependencies "${install}" "${MISSING_DEPS[@]}"
install_dependencies "${install}" "${check_pkg}" "${MISSING_DEPS[@]}"
ensure_brew
}

# shellcheck disable=SC2206
# Install missing required tools.
install_dependencies() {

local install="${1}" tools pkgs
local install="${1}" check_pkg="${2}" tools pkgs

shift
shift
tools=(${@})
pkgs="${tools[*]}"
Expand All @@ -437,14 +429,19 @@ Usage: $APP_NAME [OPTIONS] <args>
if [[ ${#tools[@]} -gt 0 ]]; then
[[ -n "${SUDO}" ]] &&
echo -e "\n${ORANGE}Using 'sudo' to install apps. You may be prompted for the password.${NC}\n"
echo -e "${WHITE}(${OS_TYPE}) Installing required packages using: \"${install}\""
echo -en " |-${pkgs}... "
if ${install} "${tools[@]}" >>"${INSTALL_LOG}" 2>&1; then
echo -e "${GREEN}OK${NC}"
else
echo -e "${RED}FAILED${NC}"
quit 2 "Failed to install dependencies. Please manually install the missing tools and try again."
fi
echo -e "${WHITE}(${OS_TYPE}) Installing required packages using: ${GREEN}\"${install}\"${NC}"
echo -e " |-${pkgs}"
for tool_name in "${tools[@]}"; do
echo -en "${ORANGE}[${OS_TYPE}] ${WHITE}Installing: ${YELLOW}${tool_name}${NC}..."
if ${install} "${tool_name}" >>"${INSTALL_LOG}" 2>&1; then
printf '%*.*s' 0 $((pad_len - ${#tool_name})) "${pad}"
echo -e " ${GREEN}√ OK${NC}"
else
echo -e " ${RED}X FAILED${NC}"
MISSING_DEPS+=("${tool_name}")
quit 2 "Failed to install dependencies. Please manually install the missing tools and try again."
fi
done
fi
}

Expand Down Expand Up @@ -697,16 +694,16 @@ Usage: $APP_NAME [OPTIONS] <args>
fi

# Copy HomeSetup AskAI prompts into place.
echo -en "\n${WHITE}Copying HomeSetup AskAI prompts into ${BLUE}${PROMPTS_DIR}... "
echo -en "\n${WHITE}Copying HomeSetup AskAI prompts into ${BLUE}${HHS_PROMPTS_DIR}... "
echo ">>> Copied HomeSetup AskAI prompts:" >>"${INSTALL_LOG}"
[[ -d "${PROMPTS_DIR}" ]] || quit 2 "Unable to locate AskAI prompts (${PROMPTS_DIR}) directory !"
[[ -d "${HHS_PROMPTS_DIR}" ]] || quit 2 "Unable to locate AskAI prompts (${HHS_PROMPTS_DIR}) directory !"
if find "${HHS_HOME}"/assets/prompts -maxdepth 1 -type f -iname "*.txt" \
-print \
-exec rsync --archive {} "${PROMPTS_DIR}" \; \
-exec rsync --archive {} "${HHS_PROMPTS_DIR}" \; \
-exec chown "${USER}":"${GROUP}" {} \; >>"${INSTALL_LOG}" 2>&1; then
echo -e "${GREEN}OK${NC}"
else
quit 2 "Unable to copy AskAI prompts into fonts (${PROMPTS_DIR}) directory !"
quit 2 "Unable to copy AskAI prompts into fonts (${HHS_PROMPTS_DIR}) directory !"
fi

# -----------------------------------------------------------------------------------
Expand Down Expand Up @@ -970,8 +967,6 @@ Usage: $APP_NAME [OPTIONS] <args>
rsync --archive "${INSTALL_LOG}" "${HHS_LOG_DIR}"
}

set -e

# shellcheck disable=SC2317
abort_install() {
echo "Installation aborted: ANS=${ANS} QUIET=${QUIET} METHOD=${METHOD}" >>"${INSTALL_LOG}" 2>&1
Expand Down
7 changes: 5 additions & 2 deletions tests/sanity-tests.bats
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@
[[ \
-d "${HHS_HOME}" \
&& -d "${HHS_DIR}" \
&& -d "${HHS_LOG_DIR}" \
&& -d "${HHS_BACKUP_DIR}" \
&& -d "${HHS_CACHE_DIR}" \
&& -d "${HHS_BACKUP_DIR}" \
&& -d "${HHS_LOG_DIR}" \
&& -d "${HHS_MOTD_DIR}" \
&& -d "${HHS_PROMPTS_DIR}" \
]]
}

Expand All @@ -42,6 +44,7 @@
[[ -f "${HHS_DIR}/.profile" ]] || missing+=("${HHS_DIR}/.profile")
[[ -f "${HHS_DIR}/.prompt" ]] || missing+=("${HHS_DIR}/.prompt")
[[ -f "${HHS_DIR}/.saved_dirs" ]] || missing+=("${HHS_DIR}/.saved_dirs")
[[ -f "${HHS_PROMPTS_DIR}/homesetup.txt" ]] || missing+=("${HHS_PROMPTS_DIR}/homesetup.txt")

[[ ${#missing[@]} -eq 0 ]] || echo "Missing dotfiles: [${missing[*]}]"
[[ ${#missing[@]} -eq 0 ]]
Expand Down

0 comments on commit 312b55c

Please sign in to comment.