Skip to content

Commit

Permalink
Fix installation script and add homesetup.toml update check
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Nov 6, 2024
1 parent 8259c97 commit c6f4349
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 71 deletions.
82 changes: 43 additions & 39 deletions bin/hhs-functions/bash/hhs-text.bash
Original file line number Diff line number Diff line change
Expand Up @@ -64,51 +64,55 @@ function __hhs_ascof() {
if [[ $# -eq 0 || '-h' == "$1" ]]; then
echo "usage: ${FUNCNAME[0]} <string>"
return 1
elif __hhs_has od; then
echo ''
echo -en "${GREEN}Dec:${NC}"
echo -en "${@}" | od -An -t uC | head -n 1 | sed 's/^ */ /g'
echo -en "${GREEN}Hex:${NC}"
echo -en "${@}" | od -An -t xC | head -n 1 | sed 's/^ */ /g'
echo -en "${GREEN}Str:${NC}"
echo -e " ${*}"
echo ''
fi
echo ''
echo -en "${GREEN}Dec:${NC}"
echo -en "${@}" | od -An -t uC | head -n 1 | sed 's/^ */ /g'
echo -en "${GREEN}Hex:${NC}"
echo -en "${@}" | od -An -t xC | head -n 1 | sed 's/^ */ /g'
echo -en "${GREEN}Str:${NC}"
echo -e " ${*}"
echo ''

return 0
}

# @function: Convert unicode to hexadecimal.
# @param $1..$N [Req] : The unicode values to convert.
function __hhs_utoh() {
if __hhs_has "hexdump"; then

local result converted uni ret_val=1
# @function: Convert unicode to hexadecimal.
# @param $1..$N [Req] : The unicode values to convert.
function __hhs_utoh() {

if [[ $# -le 0 || "$1" == "-h" || "$1" == "--help" ]]; then
echo "usage: ${FUNCNAME[0]} <4d-unicode...>"
echo ''
echo ' Notes: '
echo ' - unicode is a four digits hexadecimal number. E.g:. F205'
echo ' - exceeding digits will be ignored'
return 1
else
echo ''
for next in "$@"; do
hexa="${next:0:4}"
# More digits will be ignored
uni="$(printf '%04s' "${hexa}")"
[[ ${uni} =~ [0-9A-Fa-f]{4} ]] || continue
echo -e "[${HHS_HIGHLIGHT_COLOR}Unicode:'\u${uni}'${NC}]"
converted=$(python3 -c "import struct; print(bytes.decode(struct.pack('<I', int('${uni}', 16)), 'utf_32_le'))" | hexdump -Cb)
ret_val=$?
result=$(awk '
NR == 1 {printf " Hex => "; print "\\\\x"$2"\\\\x"$3"\\\\x"$4}
NR == 2 {printf " Oct => "; print "\\"$2"\\"$3"\\"$4}
NR == 1 {printf " Icn => "; print "\\x"$2"\\x"$3"\\x"$4}
' <<<"${converted}")
echo -e "${GREEN}${result}${NC}"
local result converted uni ret_val=1

if [[ $# -le 0 || "$1" == "-h" || "$1" == "--help" ]]; then
echo "usage: ${FUNCNAME[0]} <4d-unicode...>"
echo ''
done
fi
echo ' Notes: '
echo ' - unicode is a four digits hexadecimal number. E.g:. F205'
echo ' - exceeding digits will be ignored'
return 1
else
echo ''
for next in "$@"; do
hexa="${next:0:4}"
# More digits will be ignored
uni="$(printf '%04s' "${hexa}")"
[[ ${uni} =~ [0-9A-Fa-f]{4} ]] || continue
echo -e "[${HHS_HIGHLIGHT_COLOR}Unicode:'\u${uni}'${NC}]"
converted=$(python3 -c "import struct; print(bytes.decode(struct.pack('<I', int('${uni}', 16)), 'utf_32_le'))" | hexdump -Cb)
ret_val=$?
result=$(awk '
NR == 1 {printf " Hex => "; print "\\\\x"$2"\\\\x"$3"\\\\x"$4}
NR == 2 {printf " Oct => "; print "\\"$2"\\"$3"\\"$4}
NR == 1 {printf " Icn => "; print "\\x"$2"\\x"$3"\\x"$4}
' <<<"${converted}")
echo -e "${GREEN}${result}${NC}"
echo ''
done
fi

return ${ret_val}
}
return ${ret_val}
}
fi
14 changes: 8 additions & 6 deletions dotfiles/bash/bash_aliases.bash
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,6 @@ alias df='\df -H'
alias du='\du -hcd 1'
# @alias: Make `ps' command output pretty and human readable format
alias psg='\ps aux | \grep -v grep | \grep -i -e VSZ -e'
# @alias: Display current value of IFS
alias ifs='echo -en "${IFS}" | hexdump -C'
# @alias: Use the assigned app to open a file
alias open="__hhs_open"
# @alias: Display/Set/unset current Shell Options
Expand Down Expand Up @@ -232,12 +230,16 @@ Darwin)
alias show-deskicons="defaults write com.apple.finder CreateDesktop -bool true && killall Finder"
# @alias: Hide all desktop icons
alias hide-deskicons="defaults write com.apple.finder CreateDesktop -bool false && killall Finder"
# @alias: Canonical hex dump; some systems have this symlinked
__hhs_has "hd" || alias hd='\hexdump -C'
if __hhs_has "hexdump"; then
# @alias: Display current value of IFS
alias ifs='echo -en "${IFS}" | hexdump -C'
# @alias: Canonical hex dump; some systems have this symlinked
__hhs_has "hd" || alias hd='hexdump -C'
fi
# @alias: If `md5sum' is not available, use `md5' instead`
__hhs_has "md5sum" || alias md5sum='\md5'
__hhs_has "md5sum" || alias md5sum='md5'
# @alias: If `sha1' is not available, use `shasum' instead`
__hhs_has "sha1" || alias sha1='\shasum'
__hhs_has "sha1" || alias sha1='shasum'
;;
esac

Expand Down
25 changes: 13 additions & 12 deletions dotfiles/bash/hhsrc.bash
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ if [[ ${HHS_SET_LOCALES} -eq 1 ]] && __hhs_has "locale"; then
fi

# Initialize Blesh plug-in if it's enabled.
if [[ ${HHS_USE_BLESH} -eq 1 && -d "${HHS_BLESH_DIR}" ]]; then
__hhs_log "INFO" "Loading Blesh plug-in"
if [[ ${HHS_USE_BLESH} -eq 1 ]]; then
__hhs_log "INFO" "Loading Ble-sh plug-in"
[[ $- == *i* ]] && source "${HHS_BLESH_DIR}/out/ble.sh" --noattach
else
__hhs_log "WARN" "Blesh could not be initialized ! UB=${HHS_USE_BLESH}"
__hhs_log "WARN" "Ble-sh initialization was disabled !"
fi

# -----------------------------------------------------------------------------------
Expand Down Expand Up @@ -242,14 +242,14 @@ if [[ ${HHS_LOAD_COMPLETIONS} -eq 1 ]]; then
while read -r cpl; do
app_name="$(basename "${cpl//-completion/}")"
app_name="${app_name//\.${HHS_MY_SHELL}/}"
if [[ "${app_name}" == "fzf" && ${HHS_USE_BLESH} -eq 1 && -d "${HHS_BLESH_DIR}" ]]; then
# Note: If you want to combine fzf-completion with bash_completion, you need to load bash_completion
# earlier than fzf-completion. This is required regardless of whether to use ble.sh or not.
# source /etc/profile.d/bash_completion.sh
ble-import -d integration/fzf-completion
ble-import -d integration/fzf-key-bindings
fi
if __hhs_has "${app_name}"; then
if [[ "${app_name}" == "fzf" && ${HHS_USE_BLESH} -eq 1 ]]; then
# Note: If you want to combine fzf-completion with bash_completion, you need to load bash_completion
# earlier than fzf-completion. This is required regardless of whether to use ble.sh or not.
# source /etc/profile.d/bash_completion.sh
ble-import -d integration/fzf-completion
ble-import -d integration/fzf-key-bindings
fi
__hhs_source "${cpl}"
HHS_COMPLETIONS="${HHS_COMPLETIONS}${app_name} "
else
Expand Down Expand Up @@ -312,10 +312,11 @@ fi

# Attach ble-sh to bash if it's enabled.
if [[ ${HHS_USE_BLESH} -eq 1 && -d "${HHS_BLESH_DIR}" ]]; then
__hhs_log "DEBUG" "Attaching Blesh plug-in"
__hhs_log "DEBUG" "Attaching Ble-sh plug-in"
[[ ! ${BLE_VERSION-} ]] || ble-attach
else
__hhs_log "WARN" "Blesh could not be attached !"
unset HHS_USE_BLESH
__hhs_log "WARN" "Ble-sh could not be attached !"
fi

# Attach atuin to bash if it's enabled
Expand Down
6 changes: 3 additions & 3 deletions dotfiles/homesetup.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
# Copyright (c) 2024, HomeSetup team

# Notice: Please do not edit this file, instead use: $ hhs setup.
# @version: v1.0.7
# @version: v1.0.8

# Dev notes:
# .hspm -> homesetup.toml
Expand All @@ -29,9 +29,9 @@ hhs_restore_last_dir = true
# Set to enable starship prompt.
hhs_use_starship = true
# Set to enable blesh plug-in.
hhs_use_blesh = true
hhs_use_blesh = false
# Set to enable atuin plug-in.
hhs_use_atuin = true
hhs_use_atuin = false
# Set to enable loading shell options.
hhs_load_shell_options = true
# Set to disable HomeBrew updates.
Expand Down
41 changes: 30 additions & 11 deletions install.bash
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ usage: $APP_NAME [OPTIONS] <args>
if [[ -n "${SUDO_USER}" ]]; then
USER="${SUDO_USER}"
HOME="$(eval echo ~"${SUDO_USER}")"
has sudo &>/dev/null && SUDO=sudo
else
USER="${USER:-$(whoami)}"
HOME="${HOME:-$(eval echo ~"${USER}")}"
unset SUDO
fi

[[ -z "${USER}" || -z "${HOME}" ]] && quit 1 "Unable to determine USER/HOME -> ${USER}/${HOME}"
Expand Down Expand Up @@ -95,6 +97,9 @@ usage: $APP_NAME [OPTIONS] <args>
# Whether to install the AskAI functionalities or not
INSTALL_AI="${GITHUB_ACTIONS:-}"

# Streamed installation is disabled by default
[[ -n "${STREAMED}" && -z "${GITHUB_ACTIONS}" ]] && unset INSTALL_AI

# Shell type
SHELL_TYPE="${SHELL##*/}"

Expand Down Expand Up @@ -133,7 +138,7 @@ usage: $APP_NAME [OPTIONS] <args>

# HomeSetup application dependencies
DEPENDENCIES=(
'git' 'curl' 'ruby' 'rsync' 'mkdir' 'vim' 'gawk'
'git' 'curl' 'ruby' 'rsync' 'mkdir' 'vim' 'gawk' 'hexdump'
)

# Missing HomeSetup dependencies
Expand Down Expand Up @@ -171,7 +176,7 @@ usage: $APP_NAME [OPTIONS] <args>
# @function: Check if a command exists
# @param $1 [Req] : The command to check
has() {
type "${1}" >/dev/null 2>&1
type "${1}" &>/dev/null
}

# @function: Create a directory and check for write permissions
Expand Down Expand Up @@ -344,7 +349,10 @@ usage: $APP_NAME [OPTIONS] <args>

# Prompt the user for AskAI installation
query_askai_install() {
if [[ -z "${GITHUB_ACTIONS}" ]]; then
PIP=$(command -v pip3 2>/dev/null)
if PIP show hspylib-askai &>/dev/null; then
INSTALL_AI=1
elif [[ -z "${STREAMED}" && -z "${GITHUB_ACTIONS}" ]]; then
echo -e "${ORANGE}"
read -rn 1 -p 'Would you like to install HomeSetup AI capabilities (y/[n])? ' ANS
echo -e "${NC}" && [[ -n "${ANS}" ]] && echo ''
Expand Down Expand Up @@ -376,8 +384,6 @@ usage: $APP_NAME [OPTIONS] <args>

local pad pad_len install check_pkg

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

[[ -n "${INSTALL_AI}" ]] && PYTHON_MODULES+=('hspylib-askai')

# macOS
Expand All @@ -387,8 +393,8 @@ usage: $APP_NAME [OPTIONS] <args>
DEPENDENCIES+=('sudo' 'xcode-select')
[[ -n "${INSTALL_AI}" ]] &&
DEPENDENCIES+=('ffmpeg' 'portaudio' 'libmagic')
install="${SUDO} brew install -y"
check_pkg="brew info "
install="${SUDO} brew install -f"
check_pkg="brew list "
# Debian: Ubuntu
elif has 'apt'; then
OS_TYPE='Debian'
Expand Down Expand Up @@ -436,7 +442,7 @@ usage: $APP_NAME [OPTIONS] <args>
for tool_name in "${DEPENDENCIES[@]}"; do
echo -en "${BLUE}[${OS_TYPE}] ${WHITE}Checking: ${YELLOW}${tool_name}${NC}..."
printf '%*.*s' 0 $((pad_len - ${#tool_name})) "${pad}"
if has "${tool_name}" || ${check_pkg} "${tool_name}" >/dev/null 2>&1; then
if has "${tool_name}" || ${check_pkg} "${tool_name}" &>/dev/null; then
echo -e " ${GREEN}√ INSTALLED${NC}"
else
echo -e " ${RED}X NOT INSTALLED${NC}"
Expand Down Expand Up @@ -855,12 +861,25 @@ usage: $APP_NAME [OPTIONS] <args>
fi

# .aliasdef Needs to be updated, so, we need to replace it.
if [[ -f "${HOME}/.aliasdef" || -f "${HHS_HOME}/dotfiles/aliasdef" ]]; then
[[ -f "${HOME}/.aliasdef" ]] && copy_file "${HOME}/.aliasdef" "${HHS_BACKUP_DIR}/aliasdef-${TIMESTAMP}.bak"
[[ -f "${HHS_HOME}/dotfiles/aliasdef" ]] && copy_file "${HHS_HOME}/dotfiles/aliasdef" "${HHS_BACKUP_DIR}/aliasdef-${TIMESTAMP}.bak"
if [[ -f "${HOME}/.aliasdef" ]]; then
\mv -f "${HOME}/.aliasdef" "${HHS_BACKUP_DIR}/aliasdef-${TIMESTAMP}.bak"
copy_file "${HHS_HOME}/dotfiles/aliasdef" "${HHS_DIR}/.aliasdef"
echo -e "\n${YELLOW}Your old .aliasdef had to be replaced by a newer version. Your old file it located at ${HHS_BACKUP_DIR}/aliasdef-${TIMESTAMP}.bak ${NC}"
fi

if [[ -f "${HHS_DIR}/.homesetup.toml" ]]; then
# Check if the homesetup.toml is outdated
user_version=$(grep -o '^# @version: v[0-9]*\.[0-9]*\.[0-9]*' "${HHS_DIR}/.homesetup.toml" | sed 's/# @version: v//')
hhs_version=$(grep -o '^# @version: v[0-9]*\.[0-9]*\.[0-9]*' "${HHS_HOME}/dotfiles/homesetup.toml" | sed 's/# @version: v//')
user_num=$(echo "${user_version}" | awk -F. '{ printf "%d%02d%02d", $1, $2, $3 }')
hhs_num=$(echo "${hhs_version}" | awk -F. '{ printf "%d%02d%02d", $1, $2, $3 }')
if [[ "${hhs_num}" -gt "${user_num}" ]]; then
\mv -f "$HHS_DIR/.homesetup.toml" "${HHS_BACKUP_DIR}/homesetup-${TIMESTAMP}.toml.bak"
copy_file "${HHS_HOME}/dotfiles/homesetup.toml" "${HHS_DIR}/.homesetup.toml"
echo -e "\n${YELLOW}Your old .homesetup.toml had to be replaced by a newer version. Your old file it located at ${HHS_BACKUP_DIR}/homesetup-${TIMESTAMP}.toml.bak ${NC}"
fi
fi

# Moving .path file to .hhs .
if [[ -f "${HOME}/.path" ]]; then
\mv -f "${HOME}/.path" "${HHS_DIR}/.path"
Expand Down

0 comments on commit c6f4349

Please sign in to comment.