Merge branch 'overhaul-list-subcommand'

This commit is contained in:
Julian Ospald 2019-04-10 19:06:31 +08:00
commit 2a92c96ea9
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
2 changed files with 161 additions and 119 deletions

View File

@ -59,8 +59,6 @@ edo mv shellcheck-latest/shellcheck "$HOME"/.local/bin/shellcheck
# check our script for errors # check our script for errors
edo shellcheck ghcup edo shellcheck ghcup
edo ghcup -v show
edo ghcup -v debug-info edo ghcup -v debug-info
edo ghcup -v list edo ghcup -v list

278
ghcup
View File

@ -185,9 +185,8 @@ FLAGS:
SUBCOMMANDS: SUBCOMMANDS:
install Install GHC$(${VERBOSE} && printf "\n compile Compile and install GHC from source (UNSTABLE!!!)") install Install GHC$(${VERBOSE} && printf "\n compile Compile and install GHC from source (UNSTABLE!!!)")
show Show current/installed GHC
set Set currently active GHC version set Set currently active GHC version
list Show available GHCs and other tools (upstream) list Show available GHCs and other tools
upgrade Upgrade this script in-place upgrade Upgrade this script in-place
rm Remove an already installed GHC rm Remove an already installed GHC
install-cabal Install cabal-install install-cabal Install cabal-install
@ -277,24 +276,6 @@ ARGS:
exit 1 exit 1
} }
# @FUNCTION: show_usage
# @DESCRIPTION:
# Print the help message for 'ghcup show' to STDERR
# and exit the script with status code 1.
show_usage() {
(>&2 echo "ghcup-show
Show the installed/current GHC versions
USAGE:
${SCRIPT} show [FLAGS]
FLAGS:
-h, --help Prints help information
-i, --installed Show installed GHC version only
")
exit 1
}
# @FUNCTION: rm_usage # @FUNCTION: rm_usage
# @DESCRIPTION: # @DESCRIPTION:
# Print the help message for 'ghcup rm' to STDERR # Print the help message for 'ghcup rm' to STDERR
@ -404,19 +385,20 @@ DISCUSSION:
# and exit the script with status code 1. # and exit the script with status code 1.
list_usage() { list_usage() {
(>&2 echo "ghcup-list (>&2 echo "ghcup-list
Show available GHCs and other tools (from upstream) Show available GHCs and other tools
USAGE: USAGE:
${SCRIPT} list ${SCRIPT} list
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
-t, --tool Tool to list versions for (e.g. 'ghc' or 'cabal-install'). -t, --tool <all|ghc|cabal-install> Tool to list versions for. Default is ghc only.
Default is showing all tools. -c, --show-criteria <installed|set> Show only installed or set tool versions
-r, --raw-format Raw format, for machine parsing
DISCUSSION: DISCUSSION:
Prints tools (e.g. GHC and cabal-install) and their Prints tools (e.g. GHC and cabal-install) and their
available upstream versions. available/installed/set versions.
") ")
exit 1 exit 1
} }
@ -868,6 +850,45 @@ ghc_already_installed() {
fi fi
} }
# @FUNCTION: cabal_already_installed
# @USAGE: <cabalversion>
# @DESCRIPTION:
# Checks whether the specified cabal version
# has been installed by ghcup already.
# @RETURN: 0 if cabal is already installed, 1 otherwise
cabal_already_installed() {
[ -z "$1" ] && die "Internal error: no argument given to cabal_already_installed"
if [ -x "${BIN_LOCATION}/cabal" ] ; then
if [ "$("${BIN_LOCATION}/cabal" --numeric-version)" = "$1" ] ; then
return 0
else
return 1
fi
else
return 1
fi
}
# @FUNCTION: tool_already_installed
# @USAGE: <tool> <cabalversion>
# @DESCRIPTION:
# Checks whether the specified tool and version
# has been installed by ghcup already.
# @RETURN: 0 if tool is already installed, 1 otherwise
tool_already_installed() {
if [ "$1" = "ghc" ] ; then
ghc_already_installed "$2"
return $?
elif [ "$1" = "cabal-install" ] ; then
cabal_already_installed "$2"
return $?
else
return 1
fi
}
# @FUNCTION: get_ghc_location # @FUNCTION: get_ghc_location
# @USAGE: <ghcversion> # @USAGE: <ghcversion>
# @DESCRIPTION: # @DESCRIPTION:
@ -1209,6 +1230,37 @@ array_contains() {
return 1 return 1
} }
# @FUNCTION: show_ghc_installed
# @DESCRIPTION:
# Prints the currently selected GHC only as version string.
# @STDOUT: current GHC version
show_ghc_installed() {
current_ghc="${BIN_LOCATION}/ghc"
real_ghc=$(posix_realpath "${current_ghc}")
if [ -L "${current_ghc}" ] ; then # is symlink
if [ -e "${real_ghc}" ] ; then # exists (posix_realpath was called)
real_ghc="$(basename "${real_ghc}" | sed 's#ghc-##')"
printf "%s" "${real_ghc}"
else # is a broken symlink
red_message "broken symlink"
fi
fi
unset real_ghc current_ghc
}
# @FUNCTION: show_cabal_installed
# @DESCRIPTION:
# Prints the currently selected cabal only as version string.
# @STDOUT: current cabal version
show_cabal_installed() {
if [ -x "${BIN_LOCATION}/cabal" ] ; then
"${BIN_LOCATION}/cabal" --numeric-version
fi
}
############################ ############################
@ -1375,58 +1427,6 @@ upgrade() {
#########################
#--[ Subcommand show ]--#
#########################
# @FUNCTION: show_ghc
# @DESCRIPTION:
# Prints the currently installed and selected GHC, in human-friendly
# format.
show_ghc() {
current_ghc=$(show_ghc_installed)
echo "Installed GHCs:"
for i in "${GHC_LOCATION}"/* ; do
if [ -e "${i}" ] ; then
echo " $(basename "${i}")"
else # directory is empty
echo " None"
exit 0
fi
done
if [ -n "${current_ghc}" ] ; then
echo
echo "Current GHC"
echo " ${current_ghc}"
fi
unset current_ghc i
}
# @FUNCTION: show_ghc_installed
# @DESCRIPTION:
# Prints the currently selected GHC only as version string.
# @STDOUT: current GHC version
show_ghc_installed() {
current_ghc="${BIN_LOCATION}/ghc"
real_ghc=$(posix_realpath "${current_ghc}")
if [ -L "${current_ghc}" ] ; then # is symlink
if [ -e "${real_ghc}" ] ; then # exists (posix_realpath was called)
real_ghc="$(basename "${real_ghc}" | sed 's#ghc-##')"
printf "%s" "${real_ghc}"
else # is a broken symlink
red_message "broken symlink"
fi
fi
unset real_ghc current_ghc
}
####################### #######################
#--[ Subcommand rm ]--# #--[ Subcommand rm ]--#
@ -1688,37 +1688,92 @@ print_debug_info() {
# @FUNCTION: list # @FUNCTION: list
# @USAGE: [tool] # @USAGE: <tool> <raw-format> <criteria>
# @DESCRIPTION: # @DESCRIPTION:
# List available tools and their versions from upstream. # List available tools and their versions from upstream.
list() { list() {
mytool=$1 mytool=$1
raw_format=$2
criteria=$3
meta_file="$(get_meta_version_file)" meta_file="$(get_meta_version_file)"
echo "Available upstream versions:" if ! ${raw_format} ; then
echo printf "\\033[1;32m%s\\033[0m\\n" "Available versions:"
if [ -z "${mytool}" ] ; then
awk "
NF {
if (\$1 != \"#\") {
if (\$1 == \"cabal-install\") {
print \$1 \"\\t\" \$2 \"\\t\" \$3
} else {
print \$1 \"\\t\\t\" \$2 \"\\t\" \$3
}
}
}" "${meta_file}" || die "awk failed!"
else
awk "
NF {
if (\$1 == \"${mytool}\") {
print \$1 \"\\t\" \$2 \"\\t\" \$3
}
}" "${meta_file}" || die "awk failed!"
fi fi
unset mytool meta_file lines=$(
if [ "${mytool}" = "all" ] ; then
awk "
NF {
if (\$1 != \"#\") {
if (\$1 == \"cabal-install\") {
print \$1 \"\\t\" \$2 \"\\t\" \$3
} else {
print \$1 \"\\t\\t\" \$2 \"\\t\" \$3
}
}
}" "${meta_file}" || die "awk failed!"
else
awk "
NF {
if (\$1 == \"${mytool}\") {
print \$1 \"\\t\" \$2 \"\\t\" \$3
}
}" "${meta_file}" || die "awk failed!"
fi
)
_print_me() {
if ${raw_format} ; then
printf "%s\\n" "$1"
else
if [ "$2" = "available" ] ; then
printf "\\033[0;32m\342\234\224\\033[0m %s\\n" "$1"
elif [ "$2" = "set" ] ; then
printf "\\033[0;32m\342\234\224 \\033[0;34m%s\\033[0m\\n" "$1"
elif [ "$2" = "unavailable" ] ; then
printf "\\033[0;31m\342\234\227\\033[0m %s\\n" "$1"
fi
fi
}
if [ -z "${lines}" ] ; then
(>&2 echo "Nothing found for tool ${mytool}")
return
fi
echo "$lines" | while read -r l; do
tool=$(echo "${l}" | cut -f1)
version=$(echo "${l}" | cut -f2)
if [ "${criteria}" = "set" ] ; then
if [ "${tool}" = "ghc" ] && [ "${version}" = "$(show_ghc_installed)" ] ; then
_print_me "${l}" "set"
fi
if [ "${tool}" = "cabal-install" ] && [ "${version}" = "$(show_cabal_installed)" ] ; then
_print_me "${l}" "set"
fi
else
if tool_already_installed "${tool}" "${version}" ; then
if [ "${tool}" = "ghc" ] && [ "${version}" = "$(show_ghc_installed)" ] ; then
_print_me "${l}" "set"
elif [ "${tool}" = "cabal-install" ] && [ "${version}" = "$(show_cabal_installed)" ] ; then
_print_me "${l}" "set"
else
_print_me "${l}" "available"
fi
else
if [ "${criteria}" != "installed" ] ; then
_print_me "${l}" "unavailable"
fi
fi
fi
done
unset mytool meta_file l lines tool version raw_format installed_only criteria
} }
@ -1899,23 +1954,6 @@ while [ $# -gt 0 ] ; do
upgrade "$(dirname "$(posix_realpath "${SOURCE}")")" upgrade "$(dirname "$(posix_realpath "${SOURCE}")")"
fi fi
break;; break;;
show)
SHOW_INSTALLED=false
shift 1
while [ $# -gt 0 ] ; do
case $1 in
-h|--help) show_usage;;
-i|--installed) SHOW_INSTALLED=true
break;;
*) show_usage;;
esac
done
if ${SHOW_INSTALLED} ; then
show_ghc_installed
else
show_ghc
fi
break;;
rm) rm)
shift 1 shift 1
while [ $# -gt 0 ] ; do while [ $# -gt 0 ] ; do
@ -1989,16 +2027,22 @@ while [ $# -gt 0 ] ; do
print_debug_info print_debug_info
break;; break;;
list) list)
RAW_FORMAT=false
TOOL="ghc"
shift 1 shift 1
while [ $# -gt 0 ] ; do while [ $# -gt 0 ] ; do
case $1 in case $1 in
-h|--help) list_usage;; -h|--help) list_usage;;
-t|--tool) TOOL=$2 -t|--tool) TOOL=$2
shift 2;; shift 2;;
-r|--raw-format) RAW_FORMAT=true
shift 1;;
-c|--show-criteria) SHOW_CRITERIA=$2
shift 2;;
*) list_usage;; *) list_usage;;
esac esac
done done
list "${TOOL}" list "${TOOL}" ${RAW_FORMAT} "${SHOW_CRITERIA}"
break;; break;;
changelog) changelog)
shift 1 shift 1