207 lines
5.9 KiB
Bash
207 lines
5.9 KiB
Bash
#!/usr/bin/env bash
|
|
#
|
|
# A collection of reusable functions.
|
|
|
|
###########################################################################
|
|
# Component-specific functions (component is either an alias, a plugin, or a
|
|
# completion).
|
|
###########################################################################
|
|
|
|
_bash-it-component-help() {
|
|
local component=$(_bash-it-pluralize-component "${1}")
|
|
local file=$(_bash-it-component-cache-file ${component})
|
|
if [[ ! -s "${file}" || -z $(find "${file}" -mmin -300) ]] ; then
|
|
rm -f "${file}" 2>/dev/null
|
|
local func="_bash-it-${component}"
|
|
${func} | $(_bash-it-grep) -E ' \[' | cat > ${file}
|
|
fi
|
|
cat "${file}"
|
|
}
|
|
|
|
_bash-it-component-cache-file() {
|
|
local component=$(_bash-it-pluralize-component "${1}")
|
|
local file="${BASH_IT}/tmp/cache/${component}"
|
|
[[ -f ${file} ]] || mkdir -p $(dirname ${file})
|
|
printf "${file}"
|
|
}
|
|
|
|
_bash-it-pluralize-component() {
|
|
local component="${1}"
|
|
local len=$(( ${#component} - 1 ))
|
|
# pluralize component name for consistency
|
|
[[ ${component:${len}:1} != 's' ]] && component="${component}s"
|
|
[[ ${component} == "alias" ]] && component="aliases"
|
|
printf ${component}
|
|
}
|
|
|
|
_bash-it-clean-component-cache() {
|
|
local component="$1"
|
|
local cache
|
|
local -a BASH_IT_COMPONENTS=(aliases plugins completions)
|
|
if [[ -z ${component} ]] ; then
|
|
for component in "${BASH_IT_COMPONENTS[@]}" ; do
|
|
_bash-it-clean-component-cache "${component}"
|
|
done
|
|
else
|
|
cache="$(_bash-it-component-cache-file ${component})"
|
|
if [[ -f "${cache}" ]] ; then
|
|
rm -f "${cache}"
|
|
fi
|
|
fi
|
|
}
|
|
|
|
# Returns an array of items within each compoenent.
|
|
_bash-it-component-list() {
|
|
local component="$1"
|
|
_bash-it-component-help "${component}" | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
|
}
|
|
|
|
_bash-it-component-list-matching() {
|
|
local component="$1"; shift
|
|
local term="$1"
|
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -- "${term}" | awk '{print $1}' | sort | uniq
|
|
}
|
|
|
|
_bash-it-component-list-enabled() {
|
|
local component="$1"
|
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
|
}
|
|
|
|
_bash-it-component-list-disabled() {
|
|
local component="$1"
|
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | awk '{print $1}' | uniq | sort | tr '\n' ' '
|
|
}
|
|
|
|
# Checks if a given item is enabled for a particular component/file-type.
|
|
# Uses the component cache if available.
|
|
#
|
|
# Returns:
|
|
# 0 if an item of the component is enabled, 1 otherwise.
|
|
#
|
|
# Examples:
|
|
# _bash-it-component-item-is-enabled alias git && echo "git alias is enabled"
|
|
_bash-it-component-item-is-enabled() {
|
|
local component="$1"
|
|
local item="$2"
|
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
|
|
}
|
|
|
|
# Checks if a given item is disabled for a particular component/file-type.
|
|
# Uses the component cache if available.
|
|
#
|
|
# Returns:
|
|
# 0 if an item of the component is enabled, 1 otherwise.
|
|
#
|
|
# Examples:
|
|
# _bash-it-component-item-is-disabled alias git && echo "git aliases are disabled"
|
|
_bash-it-component-item-is-disabled() {
|
|
local component="$1"
|
|
local item="$2"
|
|
_bash-it-component-help "${component}" | $(_bash-it-grep) -E -v '\[x\]' | $(_bash-it-grep) -E -q -- "^${item}\s"
|
|
}
|
|
|
|
|
|
###########################################################################
|
|
# Generic utilies
|
|
###########################################################################
|
|
|
|
# This function searches an array for an exact match against the term passed
|
|
# as the first argument to the function. This function exits as soon as
|
|
# a match is found.
|
|
#
|
|
# Returns:
|
|
# 0 when a match is found, otherwise 1.
|
|
#
|
|
# Examples:
|
|
# $ declare -a fruits=(apple orange pear mandarin)
|
|
#
|
|
# $ _bash-it-array-contains-element apple "@{fruits[@]}" && echo 'contains apple'
|
|
# contains apple
|
|
#
|
|
# $ if $(_bash-it-array-contains-element pear "${fruits[@]}"); then
|
|
# echo "contains pear!"
|
|
# fi
|
|
# contains pear!
|
|
#
|
|
#
|
|
_bash-it-array-contains-element() {
|
|
local e
|
|
for e in "${@:2}"; do
|
|
[[ "$e" == "$1" ]] && return 0
|
|
done
|
|
return 1
|
|
}
|
|
|
|
# Dedupe a simple array of words without spaces.
|
|
_bash-it-array-dedup() {
|
|
echo "$*" | tr ' ' '\n' | sort -u | tr '\n' ' '
|
|
}
|
|
|
|
# Outputs a full path of the grep found on the filesystem
|
|
_bash-it-grep() {
|
|
if [[ -z "${BASH_IT_GREP}" ]] ; then
|
|
export BASH_IT_GREP="$(which egrep || which grep || '/usr/bin/grep')"
|
|
fi
|
|
printf "%s " "${BASH_IT_GREP}"
|
|
}
|
|
|
|
# Updates a git repo by presenting a list of commit messages since the current state.
|
|
#
|
|
# Returns:
|
|
# 0 when updated successfully, otherwise 1
|
|
#
|
|
# Examples:
|
|
# _bash-it-update-repo 'bash-it' "${BASH_IT}" "${BASH_IT_REMOTE}"
|
|
#
|
|
_bash-it-update-repo() {
|
|
local name="$1"
|
|
local dir="$2"
|
|
local remote="${3:-origin}"
|
|
local old_pwd="${PWD}"
|
|
|
|
cd "${dir}" || return 1
|
|
|
|
git fetch &> /dev/null
|
|
|
|
declare status
|
|
status="$(git rev-list master.."${remote}/master" 2> /dev/null)"
|
|
|
|
if [[ -n "${status}" ]]; then
|
|
|
|
for i in $(git rev-list --merges --first-parent "master..${remote}"); do
|
|
num_of_lines=$(git log -1 --format=%B "$i" | awk 'NF' | wc -l)
|
|
if [ "$num_of_lines" -eq 1 ]; then
|
|
description="%s"
|
|
else
|
|
description="%b"
|
|
fi
|
|
git --no-pager log --format="%h: $description (%an)" -1 "$i"
|
|
done
|
|
echo ""
|
|
read -e -n 1 -p "Would you like to update to $(git log -1 --format=%h "${remote}/master")? [Y/n] " RESP
|
|
|
|
case $RESP in
|
|
[yY]|"")
|
|
if ! git pull --rebase &> /dev/null
|
|
then
|
|
echo "${name} successfully updated."
|
|
else
|
|
echo "Error updating ${name}, please, check if your ${name} installation folder (${dir}) is clean."
|
|
fi
|
|
;;
|
|
[nN])
|
|
echo "Not upgrading…"
|
|
;;
|
|
*)
|
|
echo -e "\033[91mPlease choose y or n.\033[m"
|
|
;;
|
|
esac
|
|
|
|
else
|
|
echo "${name} is up to date, nothing to do!"
|
|
fi
|
|
|
|
cd "${old_pwd}" &> /dev/null || return 1
|
|
|
|
}
|