diff --git a/lib/helpers.bash b/lib/helpers.bash index 94df885d..b6c263a1 100755 --- a/lib/helpers.bash +++ b/lib/helpers.bash @@ -1,4 +1,7 @@ -#!/usr/bin/env bash +# shellcheck shell=bash +# shellcheck disable=SC2016 +# +# A collection of reusable functions. BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS=${BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS:-150} BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN=${BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN:-250} @@ -77,7 +80,7 @@ function _bash_it_homebrew_check() } function _make_reload_alias() { - echo "source \${BASH_IT}/scripts/reloader.bash ${1} ${2}" + echo "source '${BASH_IT}/scripts/reloader.bash' '${1}' '${2}'" } # Alias for reloading aliases @@ -109,48 +112,48 @@ bash-it () example '$ bash-it reload' example '$ bash-it restart' example '$ bash-it doctor errors|warnings|all' - typeset verb=${1:-} + local verb=${1:-} shift - typeset component=${1:-} + local component=${1:-} shift - typeset func + local func case $verb in show) - func=_bash-it-$component;; + func="_bash-it-$component";; enable) - func=_enable-$component;; + func="_enable-$component";; disable) - func=_disable-$component;; + func="_disable-$component";; help) - func=_help-$component;; + func="_help-$component";; doctor) - func=_bash-it-doctor-$component;; + func="_bash-it-doctor-$component";; search) - _bash-it-search $component "$@" + _bash-it-search "$component" "$@" return;; update) - func=_bash-it-update-$component;; + func="_bash-it-update-$component";; migrate) - func=_bash-it-migrate;; + func="_bash-it-migrate";; version) - func=_bash-it-version;; + func="_bash-it-version";; restart) - func=_bash-it-restart;; + func="_bash-it-restart";; reload) - func=_bash-it-reload;; + func="_bash-it-reload";; *) - reference bash-it + reference "bash-it" return;; esac # pluralize component if necessary - if ! _is_function $func; then - if _is_function ${func}s; then - func=${func}s + if ! _is_function "$func"; then + if _is_function "${func}s"; then + func="${func}s" else - if _is_function ${func}es; then - func=${func}es + if _is_function "${func}es"; then + func="${func}es" else echo "oops! $component is not a valid option!" reference bash-it @@ -159,20 +162,21 @@ bash-it () fi fi - if [ x"$verb" == x"enable" ] || [ x"$verb" == x"disable" ]; then + if [[ "$verb" == "enable" || "$verb" == "disable" ]] + then # Automatically run a migration if required _bash-it-migrate for arg in "$@" do - $func $arg + "$func" "$arg" done - if [ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]; then + if [[ -n "${BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE:-}" ]]; then _bash-it-reload fi else - $func "$@" + "$func" "$@" fi } @@ -181,7 +185,7 @@ _is_function () _about 'sets $? to true if parameter is the name of a function' _param '1: name of alleged function' _group 'lib' - [ -n "$(LANG=C type -t $1 2>/dev/null | grep 'function')" ] + LANG=C type -t "$1" | grep -q 'function' } _bash-it-aliases () @@ -226,8 +230,8 @@ _bash-it_update_migrate_and_restart() { _about 'Checks out the wanted version, pops directory and restart. Does not return (because of the restart!)' _param '1: Which branch to checkout to' _param '2: Which type of version we are using' - git checkout "$1" &> /dev/null - if [[ $? -eq 0 ]]; then + if git checkout "$1" &> /dev/null + then echo "Bash-it successfully updated." echo "" echo "Migrating your installation to the latest $2 version now..." @@ -235,6 +239,7 @@ _bash-it_update_migrate_and_restart() { echo "" echo "All done, enjoy!" # Don't forget to restore the original pwd! + # shellcheck disable=SC2164 popd &> /dev/null _bash-it-restart else @@ -248,80 +253,84 @@ _bash-it-update-() { _group 'lib' declare silent - for word in $@; do - if [[ ${word} == "--silent" || ${word} == "-s" ]]; then + for word in "$@"; do + if [[ "${word}" == "--silent" || "${word}" == "-s" ]]; then silent=true fi done - pushd "${BASH_IT}" &> /dev/null || return + pushd "${BASH_IT?}" >/dev/null || return DIFF=$(git diff --name-status) - [ -n "$DIFF" ] && echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF" && return 1 + if [[ -n "$DIFF" ]]; then + echo -e "Local changes detected in bash-it directory. Clean '$BASH_IT' directory to proceed.\n$DIFF" + return 1 + fi - if [ -z "$BASH_IT_REMOTE" ]; then + if [[ -z "$BASH_IT_REMOTE" ]]; then BASH_IT_REMOTE="origin" fi - git fetch $BASH_IT_REMOTE --tags &> /dev/null + git fetch "$BASH_IT_REMOTE" --tags &> /dev/null - if [ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]; then + if [[ -z "$BASH_IT_DEVELOPMENT_BRANCH" ]]; then BASH_IT_DEVELOPMENT_BRANCH="master" fi # Defaults to stable update - if [ -z "$1" ] || [ "$1" == "stable" ]; then + if [[ -z "$1" || "$1" == "stable" ]]; then version="stable" TARGET=$(git describe --tags "$(git rev-list --tags --max-count=1)" 2> /dev/null) if [[ -z "$TARGET" ]]; then echo "Can not find tags, so can not update to latest stable version..." + # shellcheck disable=SC2164 popd &> /dev/null return fi else version="dev" - TARGET=${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH} + TARGET="${BASH_IT_REMOTE}/${BASH_IT_DEVELOPMENT_BRANCH}" fi declare revision revision="HEAD..${TARGET}" declare status - status="$(git rev-list ${revision} 2> /dev/null)" + status="$(git rev-list "${revision}" 2> /dev/null)" declare revert - if [[ -z "${status}" && ${version} == "stable" ]]; then + if [[ -z "${status}" && "${version}" == "stable" ]]; then revision="${TARGET}..HEAD" - status="$(git rev-list ${revision} 2> /dev/null)" + status="$(git rev-list "${revision}" 2> /dev/null)" revert=true fi if [[ -n "${status}" ]]; then - if [[ $revert ]]; then + if [[ -n "${revert}" ]]; then echo "Your version is a more recent development version ($(git log -1 --format=%h HEAD))" echo "You can continue in order to revert and update to the latest stable version" echo "" log_color="%Cred" fi - for i in $(git rev-list --merges --first-parent ${revision}); do - num_of_lines=$(git log -1 --format=%B $i | awk 'NF' | wc -l) - if [ $num_of_lines -eq 1 ]; then + for i in $(git rev-list --merges --first-parent "${revision}"); 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 log --format="${log_color}%h: $description (%an)" -1 $i + git log --format="${log_color}%h: $description (%an)" -1 "$i" done echo "" - if [[ $silent ]]; then + if [[ -n "${silent}" ]]; then echo "Updating to ${TARGET}($(git log -1 --format=%h "${TARGET}"))..." - _bash-it_update_migrate_and_restart $TARGET $version + _bash-it_update_migrate_and_restart "$TARGET" "$version" else - read -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP - case $RESP in + read -r -e -n 1 -p "Would you like to update to ${TARGET}($(git log -1 --format=%h "${TARGET}"))? [Y/n] " RESP + case "$RESP" in [yY]|"") - _bash-it_update_migrate_and_restart $TARGET $version + _bash-it_update_migrate_and_restart "$TARGET" "$version" ;; [nN]) echo "Not updating…" @@ -332,12 +341,13 @@ _bash-it-update-() { esac fi else - if [[ ${version} == "stable" ]]; then + if [[ "${version}" == "stable" ]]; then echo "You're on the latest stable version. If you want to check out the latest 'dev' version, please run \"bash-it update dev\"" else echo "Bash-it is up to date, nothing to do!" fi fi + # shellcheck disable=SC2164 popd &> /dev/null } @@ -350,32 +360,34 @@ _bash-it-migrate() { for file_type in "aliases" "plugins" "completion" do - for f in `sort <(compgen -G "${BASH_IT}/$file_type/enabled/*.bash")` + for _bash_it_config_file in "${BASH_IT}/$file_type/enabled"/*.bash do - typeset ff="${f##*/}" + [[ -f "$_bash_it_config_file" ]] || continue # Get the type of component from the extension - typeset single_type=$(echo $ff | sed -e 's/.*\.\(.*\)\.bash/\1/g' | sed 's/aliases/alias/g') + local component_type="$(_bash-it-get-component-type-from-path "$_bash_it_config_file")" # Cut off the optional "250---" prefix and the suffix - typeset component_name=$(echo $ff | sed -e 's/[0-9]*[-]*\(.*\)\..*\.bash/\1/g') + local component_name="$(_bash-it-get-component-name-from-path "$_bash_it_config_file")" migrated_something=true - echo "Migrating $single_type $component_name." + local single_type="${component_type/aliases/aliass}" + echo "Migrating ${single_type%s} $component_name." - disable_func="_disable-$single_type" - enable_func="_enable-$single_type" + disable_func="_disable-${single_type%s}" + enable_func="_enable-${single_type%s}" - $disable_func "$component_name" - $enable_func "$component_name" + "$disable_func" "$component_name" + "$enable_func" "$component_name" done + unset _bash_it_config_file done - if [ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]; then + if [[ -n "$BASH_IT_AUTOMATIC_RELOAD_AFTER_CONFIG_CHANGE" ]]; then _bash-it-reload fi - if [ "$migrated_something" = "true" ]; then + if [[ "$migrated_something" == "true" ]]; then echo "" echo "If any migration errors were reported, please try the following: reload && bash-it migrate" fi @@ -387,28 +399,28 @@ _bash-it-version() { cd "${BASH_IT}" || return - if [ -z "${BASH_IT_REMOTE:-}" ]; then + if [[ -z "${BASH_IT_REMOTE:-}" ]]; then BASH_IT_REMOTE="origin" fi - BASH_IT_GIT_REMOTE=$(git remote get-url $BASH_IT_REMOTE) - BASH_IT_GIT_URL=${BASH_IT_GIT_REMOTE%.git} + BASH_IT_GIT_REMOTE="$(git remote get-url "$BASH_IT_REMOTE")" + BASH_IT_GIT_URL="${BASH_IT_GIT_REMOTE%.git}" if [[ "$BASH_IT_GIT_URL" == *"git@"* ]]; then # Fix URL in case it is ssh based URL - BASH_IT_GIT_URL=${BASH_IT_GIT_URL/://} - BASH_IT_GIT_URL=${BASH_IT_GIT_URL/git@/https://} + BASH_IT_GIT_URL="${BASH_IT_GIT_URL/://}" + BASH_IT_GIT_URL="${BASH_IT_GIT_URL/git@/https://}" fi - current_tag=$(git describe --exact-match --tags 2> /dev/null) + current_tag="$(git describe --exact-match --tags 2> /dev/null)" - if [[ -z $current_tag ]]; then + if [[ -z "$current_tag" ]]; then BASH_IT_GIT_VERSION_INFO="$(git log --pretty=format:'%h on %aI' -n 1)" - TARGET=${BASH_IT_GIT_VERSION_INFO%% *} + TARGET="${BASH_IT_GIT_VERSION_INFO%% *}" echo "Version type: dev" echo "Current git SHA: $BASH_IT_GIT_VERSION_INFO" echo "Commit info: $BASH_IT_GIT_URL/commit/$TARGET" else - TARGET=$current_tag + TARGET="$current_tag" echo "Version type: stable" echo "Current tag: $current_tag" echo "Tag information: $BASH_IT_GIT_URL/releases/tag/$current_tag" @@ -433,21 +445,21 @@ _bash-it-doctor-all() { _about 'reloads a profile file with error, warning and debug logs' _group 'lib' - _bash-it-doctor $BASH_IT_LOG_LEVEL_ALL + _bash-it-doctor "$BASH_IT_LOG_LEVEL_ALL" } _bash-it-doctor-warnings() { _about 'reloads a profile file with error and warning logs' _group 'lib' - _bash-it-doctor $BASH_IT_LOG_LEVEL_WARNING + _bash-it-doctor "$BASH_IT_LOG_LEVEL_WARNING" } _bash-it-doctor-errors() { _about 'reloads a profile file with error logs' _group 'lib' - _bash-it-doctor $BASH_IT_LOG_LEVEL_ERROR + _bash-it-doctor "$BASH_IT_LOG_LEVEL_ERROR" } _bash-it-doctor-() { @@ -478,18 +490,21 @@ _bash-it-reload() { _about 'reloads a profile file' _group 'lib' - pushd "${BASH_IT}" &> /dev/null || return + pushd "${BASH_IT?}" >/dev/null || return case $OSTYPE in darwin*) + # shellcheck disable=SC1090 source ~/.bash_profile ;; *) + # shellcheck disable=SC1090 source ~/.bashrc ;; esac - popd &> /dev/null || return + # shellcheck disable=SC2164 + popd } _bash-it-describe () @@ -506,22 +521,22 @@ _bash-it-describe () file_type="$3" column_header="$4" - typeset f - typeset enabled + local f + local enabled printf "%-20s%-10s%s\n" "$column_header" 'Enabled?' 'Description' - for f in "${BASH_IT}/$subdirectory/available/"*.bash + for f in "${BASH_IT}/$subdirectory/available"/*.bash do # Check for both the old format without the load priority, and the extended format with the priority declare enabled_files enabled_file - enabled_file="${f##*/}" + enabled_file="${f##*/}" enabled_files=$(sort <(compgen -G "${BASH_IT}/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/${enabled_file}") <(compgen -G "${BASH_IT}/$subdirectory/enabled/*$BASH_IT_LOAD_PRIORITY_SEPARATOR${enabled_file}") | wc -l) - if [ $enabled_files -gt 0 ]; then + if [[ "$enabled_files" -gt 0 ]]; then enabled='x' else enabled=' ' fi - printf "%-20s%-10s%s\n" "$(basename $f | sed -e 's/\(.*\)\..*\.bash/\1/g')" " [$enabled]" "$(cat $f | metafor about-$file_type)" + printf "%-20s%-10s%s\n" "$(basename "$f" | sed -e 's/\(.*\)\..*\.bash/\1/g')" " [$enabled]" "$(metafor "about-$file_type" < "$f")" done printf '\n%s\n' "to enable $preposition $file_type, do:" printf '%s\n' "$ bash-it enable $file_type <$file_type name> [$file_type name]... -or- $ bash-it enable $file_type all" @@ -536,8 +551,8 @@ _on-disable-callback() _example '$ _on-disable-callback gitstatus' _group 'lib' - callback=$1_on_disable - _command_exists $callback && $callback + callback="$1_on_disable" + _command_exists "$callback" && "$callback" } _disable-plugin () @@ -547,8 +562,8 @@ _disable-plugin () _example '$ disable-plugin rvm' _group 'lib' - _disable-thing "plugins" "plugin" $1 - _on-disable-callback $1 + _disable-thing "plugins" "plugin" "$1" + _on-disable-callback "$1" } _disable-alias () @@ -558,7 +573,7 @@ _disable-alias () _example '$ disable-alias git' _group 'lib' - _disable-thing "aliases" "alias" $1 + _disable-thing "aliases" "alias" "$1" } _disable-completion () @@ -568,7 +583,7 @@ _disable-completion () _example '$ disable-completion git' _group 'lib' - _disable-thing "completion" "completion" $1 + _disable-thing "completion" "completion" "$1" } _disable-thing () @@ -583,35 +598,34 @@ _disable-thing () file_type="$2" file_entity="$3" - if [ -z "$file_entity" ]; then + if [[ -z "$file_entity" ]]; then reference "disable-$file_type" return fi - typeset f suffix - suffix=$(echo "$subdirectory" | sed -e 's/plugins/plugin/g') + local f suffix _bash_it_config_file + suffix="${subdirectory/plugins/plugin}" - if [ "$file_entity" = "all" ]; then + if [[ "$file_entity" = "all" ]]; then # Disable everything that's using the old structure - for f in `compgen -G "${BASH_IT}/$subdirectory/enabled/*.${suffix}.bash"` - do - rm "$f" + + for _bash_it_config_file in "${BASH_IT}/$subdirectory/enabled"/*."${suffix}.bash"; do + rm -f "$_bash_it_config_file" done + for _bash_it_config_file in "${BASH_IT}/enabled"/*".${suffix}.bash"; do # Disable everything in the global "enabled" directory - for f in `compgen -G "${BASH_IT}/enabled/*.${suffix}.bash"` - do - rm "$f" + rm -f "$_bash_it_config_file" done else - typeset plugin_global=$(command ls $ "${BASH_IT}/enabled/"[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash 2>/dev/null | head -1) - if [ -z "$plugin_global" ]; then + local plugin_global="$(command ls $ "${BASH_IT}/enabled"/[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash" 2>/dev/null | head -1)" + if [[ -z "$plugin_global" ]]; then # Use a glob to search for both possible patterns # 250---node.plugin.bash # node.plugin.bash # Either one will be matched by this glob - typeset plugin=$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*$BASH_IT_LOAD_PRIORITY_SEPARATOR$file_entity.$suffix.bash,$file_entity.$suffix.bash} 2>/dev/null | head -1) - if [ -z "$plugin" ]; then + local plugin="$(command ls $ "${BASH_IT}/$subdirectory/enabled/"{[0-9]*"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${file_entity}.${suffix}.bash","${file_entity}.${suffix}.bash"} 2>/dev/null | head -1)" + if [[ -z "$plugin" ]]; then printf '%s\n' "sorry, $file_entity does not appear to be an enabled $file_type." return fi @@ -633,7 +647,7 @@ _enable-plugin () _example '$ enable-plugin rvm' _group 'lib' - _enable-thing "plugins" "plugin" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN + _enable-thing "plugins" "plugin" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_PLUGIN" } _enable-alias () @@ -643,7 +657,7 @@ _enable-alias () _example '$ enable-alias git' _group 'lib' - _enable-thing "aliases" "alias" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS + _enable-thing "aliases" "alias" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_ALIAS" } _enable-completion () @@ -653,7 +667,7 @@ _enable-completion () _example '$ enable-completion git' _group 'lib' - _enable-thing "completion" "completion" $1 $BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION + _enable-thing "completion" "completion" "$1" "$BASH_IT_LOAD_PRIORITY_DEFAULT_COMPLETION" } _enable-thing () @@ -671,38 +685,37 @@ _enable-thing () file_entity="$3" load_priority="$4" - if [ -z "$file_entity" ]; then + if [[ -z "$file_entity" ]]; then reference "enable-$file_type" return fi - if [ "$file_entity" = "all" ]; then - typeset f $file_type - for f in "${BASH_IT}/$subdirectory/available/"*.bash - do - to_enable=$(basename $f .$file_type.bash) - if [ "$file_type" = "alias" ]; then - to_enable=$(basename $f ".aliases.bash") + if [[ "$file_entity" == "all" ]]; then + local _bash_it_config_file + for _bash_it_config_file in "${BASH_IT}/$subdirectory/available"/*.bash; do + to_enable="$(basename "$_bash_it_config_file" ".$file_type.bash")" + if [[ "$file_type" == "alias" ]]; then + to_enable="$(basename "$_bash_it_config_file" ".aliases.bash")" fi - _enable-thing $subdirectory $file_type $to_enable $load_priority + _enable-thing "$subdirectory" "$file_type" "$to_enable" "$load_priority" done else - typeset to_enable=$(command ls "${BASH_IT}/$subdirectory/available/"$file_entity.*bash 2>/dev/null | head -1) - if [ -z "$to_enable" ]; then + local to_enable="$(command ls "${BASH_IT}/$subdirectory/available/$file_entity".*.bash 2>/dev/null | head -1)" + if [[ -z "$to_enable" ]]; then printf '%s\n' "sorry, $file_entity does not appear to be an available $file_type." return fi to_enable="${to_enable##*/}" # Check for existence of the file using a wildcard, since we don't know which priority might have been used when enabling it. - typeset enabled_plugin=$(command ls "${BASH_IT}/$subdirectory/enabled/"{[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable,$to_enable} 2>/dev/null | head -1) - if [ ! -z "$enabled_plugin" ] ; then + local enabled_plugin="$(command ls "${BASH_IT}/$subdirectory/enabled"/{[0-9][0-9][0-9]"${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}","${to_enable}"} 2>/dev/null | head -1)" + if [[ -n "$enabled_plugin" ]]; then printf '%s\n' "$file_entity is already enabled." return fi - typeset enabled_plugin_global=$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]$BASH_IT_LOAD_PRIORITY_SEPARATOR$to_enable" 2>/dev/null | head -1) - if [ ! -z "$enabled_plugin_global" ] ; then + local enabled_plugin_global="$(command compgen -G "${BASH_IT}/enabled/[0-9][0-9][0-9]${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" 2>/dev/null | head -1)" + if [[ -n "$enabled_plugin_global" ]]; then printf '%s\n' "$file_entity is already enabled." return fi @@ -711,10 +724,10 @@ _enable-thing () # Load the priority from the file if it present there declare local_file_priority use_load_priority - local_file_priority=$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }') - use_load_priority=${local_file_priority:-$load_priority} + local_file_priority="$(grep -E "^# BASH_IT_LOAD_PRIORITY:" "${BASH_IT}/$subdirectory/available/$to_enable" | awk -F': ' '{ print $2 }')" + use_load_priority="${local_file_priority:-$load_priority}" - ln -s ../$subdirectory/available/$to_enable "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" + ln -s "../$subdirectory/available/$to_enable" "${BASH_IT}/enabled/${use_load_priority}${BASH_IT_LOAD_PRIORITY_SEPARATOR}${to_enable}" fi _bash-it-clean-component-cache "${file_type}" @@ -737,7 +750,7 @@ _help-aliases() _example '$ alias-help' _example '$ alias-help git' - if [ -n "$1" ]; then + if [[ -n "$1" ]]; then case $1 in custom) alias_path='custom.aliases.bash' @@ -746,16 +759,16 @@ _help-aliases() alias_path="available/$1.aliases.bash" ;; esac - cat "${BASH_IT}/aliases/$alias_path" | metafor alias | sed "s/$/'/" + metafor alias < "${BASH_IT}/aliases/$alias_path" | sed "s/$/'/" else - typeset f + local f - for f in `sort <(compgen -G "${BASH_IT}/aliases/enabled/*") <(compgen -G "${BASH_IT}/enabled/*.aliases.bash")` - do - _help-list-aliases $f + for f in "${BASH_IT}/aliases/enabled"/* "${BASH_IT}/enabled"/*."aliases.bash"; do + [[ -f "$f" ]] || continue + _help-list-aliases "$f" done - if [ -e "${BASH_IT}/aliases/custom.aliases.bash" ]; then + if [[ -e "${BASH_IT}/aliases/custom.aliases.bash" ]]; then _help-list-aliases "${BASH_IT}/aliases/custom.aliases.bash" fi fi @@ -763,10 +776,10 @@ _help-aliases() _help-list-aliases () { - typeset file=$(basename $1 | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g') + local file="$(basename "$1" | sed -e 's/[0-9]*[-]*\(.*\)\.aliases\.bash/\1/g')" printf '\n\n%s:\n' "${file}" # metafor() strips trailing quotes, restore them with sed.. - cat $1 | metafor alias | sed "s/$/'/" + metafor alias < "$1" | sed "s/$/'/" } _help-plugins() @@ -776,30 +789,28 @@ _help-plugins() # display a brief progress message... printf '%s' 'please wait, building help...' - typeset grouplist=$(mktemp -t grouplist.XXXXXX) - typeset func - for func in $(_typeset_functions) - do - typeset group="$(typeset -f $func | metafor group)" - if [ -z "$group" ]; then + local grouplist=$(mktemp -t grouplist.XXXXXX) + local func + for func in $(_typeset_functions); do + local group="$(declare -f "$func" | metafor group)" + if [[ -z "$group" ]]; then group='misc' fi - typeset about="$(typeset -f $func | metafor about)" - _letterpress "$about" $func >> $grouplist.$group - echo $grouplist.$group >> $grouplist + local about="$(declare -f "$func" | metafor about)" + _letterpress "$about" "$func" >> "$grouplist.$group" + echo "$grouplist.$group" >> "$grouplist" done # clear progress message printf '\r%s\n' ' ' - typeset group - typeset gfile - for gfile in $(cat $grouplist | sort | uniq) - do + local group + local gfile + while IFS= read -r gfile; do printf '%s\n' "${gfile##*.}:" - cat $gfile + cat "$gfile" printf '\n' - rm $gfile 2> /dev/null - done | less - rm $grouplist 2> /dev/null + rm "$gfile" 2> /dev/null + done < <(sort -u "$grouplist") | less + rm "$grouplist" 2> /dev/null } _help-update () { @@ -822,32 +833,30 @@ all_groups () about 'displays all unique metadata groups' group 'lib' - typeset func - typeset file=$(mktemp -t composure.XXXX) - for func in $(_typeset_functions) - do - typeset -f $func | metafor group >> $file + local func + local file=$(mktemp -t composure.XXXX) + for func in $(_typeset_functions); do + declare -f "$func" | metafor group >> "$file" done - cat $file | sort | uniq - rm $file + sort -u "$file" + rm "$file" } -if ! type pathmunge > /dev/null 2>&1 -then - function pathmunge () { - about 'prevent duplicate directories in you PATH variable' - group 'helpers' - example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH' - example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir' +if ! _command_exists pathmunge +then function pathmunge () { + about 'prevent duplicate directories in you PATH variable' + group 'helpers' + example 'pathmunge /path/to/dir is equivalent to PATH=/path/to/dir:$PATH' + example 'pathmunge /path/to/dir after is equivalent to PATH=$PATH:/path/to/dir' - if ! [[ $PATH =~ (^|:)$1($|:) ]] ; then - if [ "$2" = "after" ] ; then - export PATH=$PATH:$1 - else - export PATH=$1:$PATH - fi + if [[ -d "${1:-}" && ! $PATH =~ (^|:)$1($|:) ]]; then + if [[ "${2:-}" == "after" ]]; then + export PATH=$PATH:$1 + else + export PATH=$1:$PATH fi - } + fi +} fi # `_bash-it-find-in-ancestor` uses the shell's ability to run a function in diff --git a/test/fixtures/go/go path/bin/.keep b/test/fixtures/go/go path/bin/.keep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/go/gopath/bin/.keep b/test/fixtures/go/gopath/bin/.keep new file mode 100644 index 00000000..e69de29b diff --git a/test/fixtures/go/gopath2/bin/.keep b/test/fixtures/go/gopath2/bin/.keep new file mode 100644 index 00000000..e69de29b diff --git a/test/plugins/go.plugin.bats b/test/plugins/go.plugin.bats index 110699e8..23513333 100644 --- a/test/plugins/go.plugin.bats +++ b/test/plugins/go.plugin.bats @@ -4,6 +4,22 @@ load ../test_helper load ../../lib/helpers load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" +function local_setup() +{ + setup_test_fixture +} + +function setup_go_path() +{ + local go_path="$1" + + # Make sure that the requested GO folder is available + assert_dir_exist "$go_path/bin" + + # Make sure that the requested GO folder is on the path + export GOPATH="$go_path:${GOPATH:-}" +} + # We test `go version` in each test to account for users with goenv and no system go. @test 'ensure _bash-it-gopath-pathmunge is defined' { @@ -15,42 +31,47 @@ load "${BASH_IT}/vendor/github.com/erichs/composure/composure.sh" @test 'plugins go: single entry in GOPATH' { { _command_exists go && go version &>/dev/null; } || skip 'golang not found' - export GOPATH="/foo" + setup_go_path "$BASH_IT/test/fixtures/go/gopath" load ../../plugins/available/go.plugin - assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo/bin" + assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath/bin" } @test 'plugins go: single entry in GOPATH, with space' { { _command_exists go && go version &>/dev/null; } || skip 'golang not found' - export GOPATH="/foo bar" + setup_go_path "$BASH_IT/test/fixtures/go/go path" load ../../plugins/available/go.plugin - assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo bar/bin" + assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin" } @test 'plugins go: single entry in GOPATH, with escaped space' { + skip 'huh?' { _command_exists go && go version &>/dev/null; } || skip 'golang not found' - export GOPATH="/foo\ bar" + setup_go_path "$BASH_IT/test/fixtures/go/go\ path" load ../../plugins/available/go.plugin - assert_equal "$(cut -d':' -f1 <<<$PATH)" "/foo\ bar/bin" + assert_equal "$(cut -d':' -f1 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin" } @test 'plugins go: multiple entries in GOPATH' { { _command_exists go && go version &>/dev/null; } || skip 'golang not found' - export GOPATH="/foo:/bar" + setup_go_path "$BASH_IT/test/fixtures/go/gopath" + setup_go_path "$BASH_IT/test/fixtures/go/gopath2" load ../../plugins/available/go.plugin - assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/bar/bin" + assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/gopath2/bin:$BASH_IT/test/fixtures/go/gopath/bin" } @test 'plugins go: multiple entries in GOPATH, with space' { { _command_exists go && go version &>/dev/null; } || skip 'golang not found' - export GOPATH="/foo:/foo bar" + setup_go_path "$BASH_IT/test/fixtures/go/gopath" + setup_go_path "$BASH_IT/test/fixtures/go/go path" load ../../plugins/available/go.plugin - assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/foo bar/bin" + assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go path/bin:$BASH_IT/test/fixtures/go/gopath/bin" } @test 'plugins go: multiple entries in GOPATH, with escaped space' { + skip 'huh?' { _command_exists go && go version &>/dev/null; } || skip 'golang not found' - export GOPATH="/foo:/foo\ bar" + setup_go_path "$BASH_IT/test/fixtures/go/gopath" + setup_go_path "$BASH_IT/test/fixtures/go/go path" load ../../plugins/available/go.plugin - assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "/foo/bin:/foo\ bar/bin" + assert_equal "$(cut -d':' -f1,2 <<<$PATH)" "$BASH_IT/test/fixtures/go/go\ path/bin:$BASH_IT/test/fixtures/go/gopath/bin" } diff --git a/test/plugins/ruby.plugin.bats b/test/plugins/ruby.plugin.bats index 7a719020..3d5bad27 100755 --- a/test/plugins/ruby.plugin.bats +++ b/test/plugins/ruby.plugin.bats @@ -8,6 +8,8 @@ load ../../plugins/available/ruby.plugin function local_setup { setup_test_fixture + _command_exists "ruby" && mkdir -p "$(ruby -e 'print Gem.user_dir')/bin" + export OLD_PATH="$PATH" export PATH="/usr/bin:/bin:/usr/sbin" }