From f26830c9b4ab7dc0c623c47a16fa367901f055c9 Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 8 Apr 2019 23:00:53 +0800 Subject: [PATCH 1/2] Considerably tweak the list subcommand wrt #88 --- ghcup | 169 ++++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 140 insertions(+), 29 deletions(-) diff --git a/ghcup b/ghcup index e66e998..9617078 100755 --- a/ghcup +++ b/ghcup @@ -187,7 +187,7 @@ SUBCOMMANDS: 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 - list Show available GHCs and other tools (upstream) + list Show available GHCs and other tools upgrade Upgrade this script in-place rm Remove an already installed GHC install-cabal Install cabal-install @@ -404,19 +404,20 @@ DISCUSSION: # and exit the script with status code 1. list_usage() { (>&2 echo "ghcup-list -Show available GHCs and other tools (from upstream) +Show available GHCs and other tools USAGE: ${SCRIPT} list FLAGS: - -h, --help Prints help information - -t, --tool Tool to list versions for (e.g. 'ghc' or 'cabal-install'). - Default is showing all tools. + -h, --help Prints help information + -t, --tool Tool to list versions for. Default is ghc only. + -c, --show-criteria Show only installed or set tool versions + -r, --raw-format Raw format, for machine parsing DISCUSSION: Prints tools (e.g. GHC and cabal-install) and their - available upstream versions. + available/installed/set versions. ") exit 1 } @@ -868,6 +869,45 @@ ghc_already_installed() { fi } +# @FUNCTION: cabal_already_installed +# @USAGE: +# @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: +# @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 # @USAGE: # @DESCRIPTION: @@ -1426,6 +1466,16 @@ show_ghc_installed() { 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 -# @USAGE: [tool] +# @USAGE: # @DESCRIPTION: # List available tools and their versions from upstream. list() { mytool=$1 + raw_format=$2 + criteria=$3 meta_file="$(get_meta_version_file)" - echo "Available upstream versions:" - echo - 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!" + if ! ${raw_format} ; then + printf "\\033[1;32m%s\\033[0m\\n" "Available versions:" 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 break;; list) + RAW_FORMAT=false + TOOL="ghc" shift 1 while [ $# -gt 0 ] ; do case $1 in -h|--help) list_usage;; -t|--tool) TOOL=$2 shift 2;; + -r|--raw-format) RAW_FORMAT=true + shift 1;; + -c|--show-criteria) SHOW_CRITERIA=$2 + shift 2;; *) list_usage;; esac done - list "${TOOL}" + list "${TOOL}" ${RAW_FORMAT} "${SHOW_CRITERIA}" break;; changelog) shift 1 From 032b95aa9bf9df0813fa20010076beb27ad136bd Mon Sep 17 00:00:00 2001 From: Julian Ospald Date: Mon, 8 Apr 2019 23:32:04 +0800 Subject: [PATCH 2/2] Remove show command, which is obsolete now --- .travis.sh | 2 - ghcup | 129 +++++++++++++---------------------------------------- 2 files changed, 31 insertions(+), 100 deletions(-) diff --git a/.travis.sh b/.travis.sh index 959f434..37ea58f 100755 --- a/.travis.sh +++ b/.travis.sh @@ -59,8 +59,6 @@ edo mv shellcheck-latest/shellcheck "$HOME"/.local/bin/shellcheck # check our script for errors edo shellcheck ghcup -edo ghcup -v show - edo ghcup -v debug-info edo ghcup -v list diff --git a/ghcup b/ghcup index 9617078..fbf11be 100755 --- a/ghcup +++ b/ghcup @@ -185,7 +185,6 @@ FLAGS: SUBCOMMANDS: 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 list Show available GHCs and other tools upgrade Upgrade this script in-place @@ -277,24 +276,6 @@ ARGS: 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 # @DESCRIPTION: # Print the help message for 'ghcup rm' to STDERR @@ -1249,6 +1230,37 @@ array_contains() { 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 +} + + ############################ @@ -1415,68 +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 -} - -# @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 -} - - ####################### #--[ Subcommand rm ]--# @@ -2004,23 +1954,6 @@ while [ $# -gt 0 ] ; do upgrade "$(dirname "$(posix_realpath "${SOURCE}")")" fi 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) shift 1 while [ $# -gt 0 ] ; do