Skip to content

Commit

Permalink
Merge pull request #295 from akinomyoga/check-bash-version
Browse files Browse the repository at this point in the history
oh-my-bash: Check Bash version and define `OMB_VERS{ION,INFO}`
  • Loading branch information
akinomyoga authored Jan 22, 2022
2 parents ab0ca1e + 7dce520 commit 9a0b552
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
5 changes: 1 addition & 4 deletions lib/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@

################################################################################

_omb_version=10000
_omb_bash_version=$((BASH_VERSINFO[0] * 10000 + BASH_VERSINFO[1] * 100 + BASH_VERSINFO[2]))

function _omb_util_setexit {
return "$1"
}
Expand All @@ -93,7 +90,7 @@ function __omb_util_defun_deprecate__message {

function _omb_util_defun_deprecate {
local warning=
((_omb_version>=$1)) &&
((_omb_version >= $1)) &&
warning='__omb_util_defun_deprecate__message "$2" "$3"; '
builtin eval -- "function $2 { $warning$3 \"\$@\"; }"
}
Expand Down
15 changes: 15 additions & 0 deletions oh-my-bash.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,21 @@ case $- in
*) return;;
esac

if [ ! -n "${BASH_VERSION-}" ]; then
printf '%s\n' 'oh-my-bash: This is not a Bash. Use OMB with Bash 3.2 or higher.' >&2
return 1
fi
_omb_bash_version=$((BASH_VERSINFO[0] * 10000 + BASH_VERSINFO[1] * 100 + BASH_VERSINFO[2]))
if ((_omb_bash_version < 30200)); then
printf '%s\n' "oh-my-bash: OMB does not support this version of Bash ($BASH_VERSION)" >&2
printf '%s\n' "oh-my-bash: Use OMB with Bash 3.2 or higher" >&2
return 1
fi

OMB_VERSINFO=(1 0 0 0 master noarch)
OMB_VERSION="${OMB_VERSINFO[0]}.${OMB_VERSINFO[1]}.${OMB_VERSINFO[2]}(${OMB_VERSINFO[3]})-${OMB_VERSINFO[4]} (${OMB_VERSINFO[5]})"
_omb_version=$((OMB_VERSINFO[0] * 10000 + OMB_VERSINFO[1] * 100 + OMB_VERSINFO[2]))

# Check for updates on initial load...
if [[ $DISABLE_AUTO_UPDATE != true ]]; then
source "$OSH"/tools/check_for_upgrade.sh
Expand Down
49 changes: 40 additions & 9 deletions tools/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,38 @@ if [ -z "${BASH_VERSION-}" ]; then
fi

if [[ ! ${BASH_VERSINFO[0]-} ]] || ((BASH_VERSINFO[0] < 3 || BASH_VERSINFO[0] == 3 && BASH_VERSINFO[1] < 2)); then
printf "Error: Bash 3.2 required for Oh My Bash.\n"
printf "Error: Upgrade Bash and try again.\n"
printf "Error: Bash 3.2 required for Oh My Bash.\n" >&2
printf "Error: Upgrade Bash and try again.\n" >&2
return 2 &>/dev/null || exit 2
elif ((BASH_VERSINFO[0] < 4)); then
printf "Warning: Bash >=4 is no longer required for Oh My Bash but is cool to have ;)\n" >&2
printf "Warning: Why don't you upgrade your Bash to 4 or higher?\n" >&2
fi

_omb_install_print_version() {
local OMB_VERSINFO
OMB_VERSINFO=(1 0 0 0 master noarch)
printf '%s\n' 'Install script for Oh-My-Bash (https://github.com/ohmybash/oh-my-bash)'
printf 'oh-my-bash, version %s.%s.%s(%s)-%s (%s)\n' "${OMB_VERSINFO[@]}"
}

_omb_install_print_usage() {
printf '%s\n' \
'usage: ./install.sh [--unattended | --help | --dry-run]' \
'usage: bash -c "$(< install.sh)" [--unattended | --help | --dry-run]'
'usage: ./install.sh [--unattended | --dry-run | --help | --usage | --version]' \
'usage: bash -c "$(< install.sh)" [--unattended | --dry-run | --help | --usage |' \
' --version]'
}

_omb_install_print_help() {
_omb_install_print_version
_omb_install_print_usage
printf '%s\n' \
'Install script for Oh-My-Bash (https://github.com/ohmybash/oh-my-bash)' \
'' \
'OPTIONS' \
' --help show this help' \
' --usage show usage' \
' --unattended attend the meeting' \
' --help show version' \
''
}

Expand All @@ -36,9 +49,12 @@ _omb_install_readargs() {
local arg=$1; shift
if [[ :$install_opts: != *:literal:* ]]; then
case $arg in
--help | --unattended | --dry-run)
--help | --usage | --unattended | --dry-run)
install_opts+=:${arg#--}
continue ;;
--version | -v)
install_opts+=:version
continue ;;
--)
install_opts+=:literal
continue ;;
Expand Down Expand Up @@ -93,12 +109,27 @@ _omb_install_main() {
local install_opts=
_omb_install_readargs "$@"

if [[ :$install_opts: == *:error:* ]]; then
printf '\n'
install_opts+=:usage
fi
if [[ :$install_opts: == *:help:* ]]; then
_omb_install_print_help
return 0
elif [[ :$install_opts: == *:error:* ]]; then
_omb_install_print_usage
install_opts+=:exit
else
if [[ :$install_opts: == *:version:* ]]; then
_omb_install_print_version
install_opts+=:exit
fi
if [[ :$install_opts: == *:usage:* ]]; then
_omb_install_print_usage
install_opts+=:exit
fi
fi
if [[ :$install_opts: == *:error:* ]]; then
return 2
elif [[ :$install_opts: == *:exit:* ]]; then
return 0
fi

# Only enable exit-on-error after the non-critical colorization stuff,
Expand Down

0 comments on commit 9a0b552

Please sign in to comment.