Considerably tweak the list subcommand wrt #88

This commit is contained in:
Julian Ospald 2019-04-08 23:00:53 +08:00
parent ae99de5876
commit f26830c9b4
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
1 changed files with 140 additions and 29 deletions

169
ghcup
View File

@ -187,7 +187,7 @@ 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 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
@ -404,19 +404,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 +869,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:
@ -1426,6 +1466,16 @@ show_ghc_installed() {
unset real_ghc current_ghc 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
}
####################### #######################
@ -1688,37 +1738,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
} }
@ -1989,16 +2094,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