Skip to content

Commit

Permalink
Improve portability against aliases
Browse files Browse the repository at this point in the history
Using same-named functions and aliases can be avoided
  • Loading branch information
umireon committed Apr 12, 2017
1 parent eb62c6e commit 9a25cc5
Showing 1 changed file with 61 additions and 48 deletions.
109 changes: 61 additions & 48 deletions luaver
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@

__luaver_VERSION="1.0.0"

alias cd=" echo $LINENO; exit 1"
alias command=" echo $LINENO; exit 1"
alias printf=" echo $LINENO; exit 1"

curl() { echo $LINENO; exit 1; }
grep() { echo $LINENO; exit 1; }
make() { echo $LINENO; exit 1; }
mkdir() { echo $LINENO; exit 1; }
rm() { echo $LINENO; exit 1; }
sed() { echo $LINENO; exit 1; }
tar() { echo $LINENO; exit 1; }
wget() { echo $LINENO; exit 1; }

# Directories and files to be used

__luaver_LUAVER_DIR="${HOME}/.luaver" # The luaver directory
Expand All @@ -28,24 +41,24 @@ __luaver_verbose=0
# Error handling function
__luaver_error()
{
printf "%s\n" "${1}" 1>&2
'printf' "%s\n" "${1}" 1>&2
}

# Printing bold text - TODO
__luaver_print()
{
if [ ! $__luaver_verbose = 0 ]
then
tput bold
printf "==> %s\n" "${1}"
tput sgr0
'command' tput bold
'printf' "==> %s\n" "${1}"
'command' tput sgr0
fi
}

# Printing formatted text
__luaver_print_formatted()
{
printf "%s\n" "${1}"
'printf' "%s\n" "${1}"
}

# A wrapper function to execute commands on the terminal and exit on error
Expand All @@ -63,27 +76,27 @@ __luaver_init()
{
if [ ! -e "${__luaver_LUAVER_DIR}" ]
then
'mkdir' "${__luaver_LUAVER_DIR}"
'command' mkdir "${__luaver_LUAVER_DIR}"
fi

if [ ! -e "${__luaver_SRC_DIR}" ]
then
'mkdir' "${__luaver_SRC_DIR}"
'command' mkdir "${__luaver_SRC_DIR}"
fi

if [ ! -e "${__luaver_LUA_DIR}" ]
then
'mkdir' "${__luaver_LUA_DIR}"
'command' mkdir "${__luaver_LUA_DIR}"
fi

if [ ! -e "${__luaver_LUAJIT_DIR}" ]
then
'mkdir' "${__luaver_LUAJIT_DIR}"
'command' mkdir "${__luaver_LUAJIT_DIR}"
fi

if [ ! -e "${__luaver_LUAROCKS_DIR}" ]
then
'mkdir' "${__luaver_LUAROCKS_DIR}"
'command' mkdir "${__luaver_LUAROCKS_DIR}"
fi

if [ -f "${__luaver_LUA_DEFAULT_FILE}" ]
Expand Down Expand Up @@ -114,7 +127,7 @@ __luaver_init()
# Synopsis:
# __luaver_download url [archive_path]
# Exit status:
# 0 if already exists or successfully downloaded
# 0 if al'read'y exists or successfully downloaded
# 1 if any error is occurred
__luaver_download()
{
Expand All @@ -125,18 +138,18 @@ __luaver_download()

__luaver_print "Downloading from ${url}"
if {
if 'curl' -V >/dev/null 2>&1
if 'command' curl -V >/dev/null 2>&1
then
'curl' -fsSL "${url}" # cURL
'command' curl -fsSL "${url}" # cURL
else
'wget' -qO- "${url}" # GNU wget or busybox
'command' wget -qO- "${url}" # GNU wget or busybox
fi
} >"${archive_path}"
then
__luaver_print "Download successful"
return 0
else
rm -f "${archive_path}"
'command' rm -f "${archive_path}"
__luaver_error "'wget' or 'curl' must be installed"
return 1
fi
Expand All @@ -146,7 +159,7 @@ __luaver_download()
# Synopsis:
# __luaver_unpack url unpack_dir_name [basedir]
# Exit status:
# 0 if already exists or successfully unpacked
# 0 if al'read'y exists or successfully unpacked
# 1 if any error is occurred
__luaver_unpack()
{
Expand All @@ -157,12 +170,12 @@ __luaver_unpack()
[ -e "${basedir}/${unpack_dir_name}" ] && return 0

__luaver_print "Unpacking ${url}"
if 'tar' -xzf "${url}" -C "${basedir}" # GNU tar, BSD tar or busybox
if 'command' tar -xzf "${url}" -C "${basedir}" # GNU tar, BSD tar or busybox
then
__luaver_print "Unpack successful"
return 0
else
rm -rf "${basedir:?}/${unpack_dir_name}"
'command' rm -rf "${basedir:?}/${unpack_dir_name}"
__luaver_error "'tar' must be installed"
return 1
fi
Expand All @@ -174,7 +187,7 @@ __luaver_remove_previous_paths()
local prefix=$1

local new_path
new_path=$(echo "${PATH}" | sed \
new_path=$('echo' "${PATH}" | 'command' sed \
-e "s#${prefix}/[^/]*/bin[^:]*:##g" \
-e "s#:${prefix}/[^/]*/bin[^:]*##g" \
-e "s#${prefix}/[^/]*/bin[^:]*##g")
Expand Down Expand Up @@ -208,7 +221,7 @@ __luaver_uninstall()
return 1
fi

'rm' -r "${package_path:?}/${package_dir}" && __luaver_print "Successfully uninstalled ${package_name}"
'command' rm -r "${package_path:?}/${package_dir}" && __luaver_print "Successfully uninstalled ${package_name}"
}

# Returns the platform# Returns the platform
Expand Down Expand Up @@ -239,8 +252,8 @@ __luaver_get_platform()
# 1 if lua was not found
__luaver_get_current_lua_version()
{
case "$(command -v lua)" in
"${__luaver_LUA_DIR}"/*/bin/lua ) command -v lua | 'awk' -F/ '{ print $(NF-2) }' ;;
case "$('command' -v lua)" in
"${__luaver_LUA_DIR}"/*/bin/lua ) 'command' -v lua | 'command' awk -F/ '{ print $(NF-2) }' ;;
* ) return 1
esac
}
Expand All @@ -253,7 +266,7 @@ __luaver_get_current_lua_version()
# 1 if lua was not found
__luaver_get_current_lua_version_short()
{
__luaver_get_current_lua_version | 'awk' -F. -vOFS=. '{ print $1, $2 }'
__luaver_get_current_lua_version | 'command' awk -F. -vOFS=. '{ print $1, $2 }'
}

# Returns the current luajit version
Expand All @@ -265,7 +278,7 @@ __luaver_get_current_lua_version_short()
__luaver_get_current_luajit_version()
{
case "$(command -v luajit)" in
"${__luaver_LUAJIT_DIR}"/*/bin/luajit ) command -v luajit | 'awk' -F/ '{ print $(NF-2) }' ;;
"${__luaver_LUAJIT_DIR}"/*/bin/luajit ) 'command' -v luajit | 'command' awk -F/ '{ print $(NF-2) }' ;;
* ) return 1
esac
}
Expand All @@ -278,8 +291,8 @@ __luaver_get_current_luajit_version()
# 1 if luarocks was not found
__luaver_get_current_luarocks_version()
{
case "$(command -v luarocks)" in
"${__luaver_LUAROCKS_DIR}"/*/bin/luarocks ) command -v luarocks | 'awk' -F/ '{ print $(NF-2) }' | 'awk' -F_ '{ print $1 }';;
case "$('command' -v luarocks)" in
"${__luaver_LUAROCKS_DIR}"/*/bin/luarocks ) 'command' -v luarocks | 'command' awk -F/ '{ print $(NF-2) }' | 'command' awk -F_ '{ print $1 }';;
* ) return 1
esac
}
Expand All @@ -292,8 +305,8 @@ __luaver_get_current_luarocks_version()
# 1 if luarocks was not found
__luaver_get_lua_version_by_current_luarocks()
{
case "$(command -v luarocks)" in
"${__luaver_LUAROCKS_DIR}"/*/bin/luarocks ) command -v luarocks | 'awk' -F/ '{ print $(NF-2) }' | 'awk' -F_ '{ print $2 }';;
case "$('command' -v luarocks)" in
"${__luaver_LUAROCKS_DIR}"/*/bin/luarocks ) 'command' -v luarocks | 'command' awk -F/ '{ print $(NF-2) }' | 'command' awk -F_ '{ print $2 }';;
* ) return 1
esac
}
Expand Down Expand Up @@ -362,11 +375,11 @@ __luaver_install_lua()
if (
__luaver_print "Compiling ${lua_dir_name}"
'cd' "${__luaver_SRC_DIR}/${lua_dir_name}" &&
'make' "$(__luaver_get_platform || echo "posix")" install INSTALL_TOP="${__luaver_LUA_DIR}/${version}"
'command' make "$(__luaver_get_platform || 'echo' "posix")" install INSTALL_TOP="${__luaver_LUA_DIR}/${version}"
)
then
__luaver_print "${lua_dir_name} successfully installed. Do you want to switch to this version? [Y/n]: "
read -r choice
'read' -r choice
case $choice in
[yY][eE][sS] | [yY] )
__luaver_use_lua "${version}"
Expand Down Expand Up @@ -396,7 +409,7 @@ __luaver_use_lua()
if [ ! -e "${__luaver_LUA_DIR}/${version}" ]
then
__luaver_print "${lua_name} is not installed. Want to install it? [Y/n]: "
read -r choice
'read' -r choice
case $choice in
[yY][eE][sS] | [yY] )
__luaver_install_lua "${version}"
Expand Down Expand Up @@ -436,7 +449,7 @@ __luaver_set_default_lua()

__luaver_unset_default_lua()
{
'rm' "${__luaver_LUA_DEFAULT_FILE}" &&
'command' rm "${__luaver_LUA_DEFAULT_FILE}" &&
__luaver_print "Removed default version for lua"
}

Expand Down Expand Up @@ -464,7 +477,7 @@ __luaver_uninstall_lua()
__luaver_list_lua()
{
__luaver_print "Installed versions: (currently $(__luaver_get_current_lua_version || echo none))"
'find' "${__luaver_LUA_DIR}" -name '*.*' -prune | 'awk' -F/ '{ print $NF }'
'command' find "${__luaver_LUA_DIR}" -name '*.*' -prune | 'command' awk -F/ '{ print $NF }'
}

# Synopsis:
Expand All @@ -486,12 +499,12 @@ __luaver_install_luajit()
if (
__luaver_print "Compiling ${lua_dir_name}"
'cd' "${__luaver_LUAJIT_DIR}/${lua_dir_name}" &&
'make' PREFIX="${__luaver_LUAJIT_DIR}/${version}" &&
'make' install PREFIX="${__luaver_LUAJIT_DIR}/${version}"
'command' make PREFIX="${__luaver_LUAJIT_DIR}/${version}" &&
'command' make install PREFIX="${__luaver_LUAJIT_DIR}/${version}"
)
then
__luaver_print "${lua_dir_name} successfully installed. Do you want to switch to this version? [Y/n]: "
read -r choice
'read' -r choice
case $choice in
[yY][eE][sS] | [yY] )
__luaver_use_luajit "${version}"
Expand Down Expand Up @@ -520,7 +533,7 @@ __luaver_use_luajit()
if [ ! -e "${__luaver_LUAJIT_DIR}/${version}" ]
then
__luaver_print "${luajit_name} is not installed. Want to install it? [Y/n]: "
read -r choice
'read' -r choice
case $choice in
[yY][eE][sS] | [yY] )
__luaver_install_lua "${version}"
Expand All @@ -546,7 +559,7 @@ __luaver_set_default_luajit()

__luaver_unset_default_luajit()
{
'rm' "${__luaver_LUAJIT_DEFAULT_FILE}" &&
'command' rm "${__luaver_LUAJIT_DEFAULT_FILE}" &&
__luaver_print "Removed default version for LuaJIT"
}

Expand Down Expand Up @@ -574,7 +587,7 @@ __luaver_uninstall_luajit()
__luaver_list_luajit()
{
__luaver_print "Installed versions: (currently $(__luaver_get_current_luajit_version || echo none))"
'find' "${__luaver_LUAJIT_DIR}" -name '*.*' -prune | 'awk' -F/ '{ print $NF }'
'command' find "${__luaver_LUAJIT_DIR}" -name '*.*' -prune | 'command' awk -F/ '{ print $NF }'
}

# Synopsis:
Expand Down Expand Up @@ -603,19 +616,19 @@ __luaver_install_luarocks()
if (
__luaver_print "Compiling ${luarocks_dir_name}"
'cd' "${__luaver_SRC_DIR}/${luarocks_dir_name}" &&
'./configure' \
./configure \
--prefix="${__luaver_LUAROCKS_DIR}/${version}_$(__luaver_get_current_lua_version_short)" \
--with-lua="${__luaver_LUA_DIR}/$(__luaver_get_current_lua_version)" \
--with-lua-bin="${__luaver_LUA_DIR}/$(__luaver_get_current_lua_version)/bin" \
--with-lua-include="${__luaver_LUA_DIR}/$(__luaver_get_current_lua_version)/include" \
--with-lua-lib="${__luaver_LUA_DIR}/$(__luaver_get_current_lua_version)/lib" \
--versioned-rocks-dir &&
'make' build &&
'make' install
'command' make build &&
'command' make install
)
then
__luaver_print "${luarocks_dir_name} successfully installed. Do you want to switch to this version? [Y/n]: "
read -r choice
'read' -r choice
case $choice in
[yY][eE][sS] | [yY] )
__luaver_use_luarocks "${version}"
Expand Down Expand Up @@ -650,7 +663,7 @@ __luaver_use_luarocks()
if [ ! -e "${__luaver_LUAROCKS_DIR}/${version}_$(__luaver_get_current_lua_version_short)" ]
then
__luaver_print "${luarocks_name} is not installed with lua version $(__luaver_get_current_lua_version_short). Want to install it? [Y/n]: "
read -r choice
'read' -r choice
case $choice in
[yY][eE][sS] | [yY] )
__luaver_install_luarocks "${version}"
Expand Down Expand Up @@ -679,7 +692,7 @@ __luaver_set_default_luarocks()

__luaver_unset_default_luarocks()
{
'rm' "${__luaver_LUAROCKS_DEFAULT_FILE}" &&
'command' rm "${__luaver_LUAROCKS_DEFAULT_FILE}" &&
__luaver_print "Removed default version for luarocks"
}

Expand Down Expand Up @@ -707,7 +720,7 @@ __luaver_uninstall_luarocks()
__luaver_list_luarocks()
{
__luaver_print "Installed versions: (currently $(__luaver_get_current_luarocks_version || echo none) in lua $(__luaver_get_lua_version_by_current_luarocks || echo none))"
'find' "${__luaver_LUAROCKS_DIR}" -name '*.*' -prune | 'awk' -F/ '{ print $NF }' | 'awk' -F_ '{ print $1 "\tlua:" $2}'
'command' find "${__luaver_LUAROCKS_DIR}" -name '*.*' -prune | 'command' awk -F/ '{ print $NF }' | 'command' awk -F_ '{ print $1 "\tlua:" $2}'
}

__luaver_current()
Expand Down Expand Up @@ -743,10 +756,10 @@ __luaver_init

luaver()
{
local command="${1}"
local cmd="${1}"
shift

case $command in
case $cmd in
"help" ) __luaver_usage;;

"install" ) __luaver_install_lua "${@}";;
Expand Down

0 comments on commit 9a25cc5

Please sign in to comment.