Allow to specify tags for 'install' and 'install-cabal'

This commit is contained in:
Julian Ospald 2018-11-06 13:32:55 +08:00
parent a8032d54d5
commit cbb027d1ea
No known key found for this signature in database
GPG Key ID: 511B62C09D50CD28
2 changed files with 220 additions and 87 deletions

View File

@ -25,10 +25,17 @@ edo ./ghcup -v rm -f 8.6.1
# set GHC # set GHC
edo ./ghcup -v set 8.2.2 edo ./ghcup -v set 8.2.2
edo ./ghcup -v rm -f 8.2.2
# install default GHC # install default GHC
edo ./ghcup -v install edo ./ghcup -v install
# install latest GHC
edo ./ghcup -v install latest
# FIXME: this might break from time to time depending on recommended GHC
edo ./ghcup -v set 8.4.4
export PATH="$HOME/.cabal/bin:$HOME/.ghcup/bin:$HOME/.local/bin:$PATH" export PATH="$HOME/.cabal/bin:$HOME/.ghcup/bin:$HOME/.local/bin:$PATH"
edo mkdir -p "$HOME"/.local/bin edo mkdir -p "$HOME"/.local/bin

220
ghcup
View File

@ -118,12 +118,12 @@ SOURCE="$0"
# download information for ghc/cabal-install etc. # download information for ghc/cabal-install etc.
META_DOWNLOAD_URL="https://raw.githubusercontent.com/haskell/ghcup/master/.download-urls" META_DOWNLOAD_URL="https://raw.githubusercontent.com/haskell/ghcup/master/.download-urls"
# @VARIABLE: META_DOWNLOAD_URL_FORMAT # @VARIABLE: META_DOWNLOAD_FORMAT
# DESCRIPTION: # DESCRIPTION:
# The version of the meta file format. # The version of the meta file format.
# This determines whether this script can read the # This determines whether this script can read the
# file from "${META_DOWNLOAD_URL}". # file from "${META_DOWNLOAD_URL}".
META_DOWNLOAD_URL_FORMAT="1" META_DOWNLOAD_FORMAT="1"
# @VARIABLE: META_VERSION_URL # @VARIABLE: META_VERSION_URL
# DESCRIPTION: # DESCRIPTION:
@ -202,14 +202,15 @@ install_usage() {
Install GHC from binary tarball Install GHC from binary tarball
USAGE: USAGE:
${SCRIPT} install [FLAGS] [VERSION] ${SCRIPT} install [FLAGS] [VERSION|TAG]
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
-f, --force Overwrite already existing installation -f, --force Overwrite already existing installation
ARGS: ARGS:
[VERSION] E.g. \"8.4.3\" or \"8.6.1\" [VERSION|TAG] E.g. \"8.4.3\" or \"8.6.1\" or
a tag like \"recommended\" or \"latest\"
(default: discovers recommended version) (default: discovers recommended version)
DISCUSSION: DISCUSSION:
@ -313,13 +314,14 @@ install_cabal_usage() {
Install the specified or a default cabal version Install the specified or a default cabal version
USAGE: USAGE:
${SCRIPT} install-cabal [FLAGS] [VERSION] ${SCRIPT} install-cabal [FLAGS] [VERSION|TAG]
FLAGS: FLAGS:
-h, --help Prints help information -h, --help Prints help information
ARGS: ARGS:
[VERSION] E.g. \"2.4.0.0\" [VERSION|TAG] E.g. \"2.4.0.0\" or a tag
like \"recommended\" or \"latest\"
DISCUSSION: DISCUSSION:
Installs the specified cabal-install version (or the default recommended) Installs the specified cabal-install version (or the default recommended)
@ -678,38 +680,31 @@ get_download_url() {
myarch=$(get_arch) myarch=$(get_arch)
mydistro=$(get_distro_alias "$(get_distro_name)") mydistro=$(get_distro_alias "$(get_distro_name)")
mydistrover=$(get_distro_ver) mydistrover=$(get_distro_ver)
meta_file_name="$(basename "${META_DOWNLOAD_URL}")" meta_file="$(get_meta_download_file)"
(
edo cd "${CACHE_LOCATION}"
download_silent "${META_DOWNLOAD_URL}"
check_meta_file_version "${meta_file_name}" "${META_DOWNLOAD_URL_FORMAT}"
# 1st try with full distro=ver # 1st try with full distro=ver
url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "${mydistro}=${mydistrover}" "${meta_file_name}") url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "${mydistro}=${mydistrover}" "${meta_file}")
if [ -n "${url}" ] ; then if [ -n "${url}" ] ; then
printf "%s" "${url}" printf "%s" "${url}"
exit 0 exit 0
fi fi
# 2nd try with just distro # 2nd try with just distro
url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "${mydistro}" "${meta_file_name}") url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "${mydistro}" "${meta_file}")
if [ -n "${url}" ] ; then if [ -n "${url}" ] ; then
printf "%s" "${url}" printf "%s" "${url}"
exit 0 exit 0
fi fi
# 3rd try with unknown # 3rd try with unknown
url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "unknown" "${meta_file_name}") url=$(try_download_url "${mytool}" "${myver}" "${myarch}" "unknown" "${meta_file}")
if [ -n "${url}" ] ; then if [ -n "${url}" ] ; then
printf "%s" "${url}" printf "%s" "${url}"
exit 0 exit 0
fi fi
)
unset mytool myver myarch mydistro mydistrover meta_file_name unset mytool myver myarch mydistro mydistrover meta_file
} }
# @FUNCTION: get_tool_ver_from_tag # @FUNCTION: get_tool_ver_from_tag
@ -723,14 +718,8 @@ get_tool_ver_from_tag() {
mytool=$1 mytool=$1
mytag=$2 mytag=$2
meta_file_name="$(basename "${META_VERSION_URL}")"
( meta_file="$(get_meta_version_file)"
edo cd "${CACHE_LOCATION}"
download_silent "${META_VERSION_URL}"
check_meta_file_version "${meta_file_name}" "${META_VERSION_FORMAT}"
awk " awk "
NF { NF {
@ -741,10 +730,9 @@ get_tool_ver_from_tag() {
exit exit
} }
} }
}" "${meta_file_name}" || die "awk failed!" }" "${meta_file}" || die "awk failed!"
)
unset mytool mytag meta_file_name unset mytool mytag meta_file
} }
# @FUNCTION: ghc_already_installed # @FUNCTION: ghc_already_installed
@ -962,6 +950,130 @@ is_sourced() {
return 1 return 1
} }
# @FUNCTION: get_meta_version_file
# @DESCRIPTION:
# Downloads the META_VERSION_URL
# in case it hasn't been downloaded
# during the execution of this script yet
# and checks the format version matches
# the expected one.
# @STDOUT: file location
get_meta_version_file() {
meta_file_name="$(basename "${META_VERSION_URL}")"
meta_filepath="${CACHE_LOCATION}/${meta_file_name}"
if [ ! -e "${meta_filepath}" ] ; then
(
edo cd "${CACHE_LOCATION}"
download_silent "${META_VERSION_URL}"
) || die "downloading failed"
fi
check_meta_file_version "${meta_file_name}" "${META_VERSION_FORMAT}"
printf "%s" "${meta_filepath}"
unset meta_file_name meta_filepath
}
# @FUNCTION: get_meta_download_file
# @DESCRIPTION:
# Downloads the META_DOWNLOAD_URL
# in case it hasn't been downloaded
# during the execution of this script yet
# and checks the format version matches
# the expected one.
# @STDOUT: file location
get_meta_download_file() {
meta_file_name="$(basename "${META_DOWNLOAD_URL}")"
meta_filepath="${CACHE_LOCATION}/${meta_file_name}"
if [ ! -e "${meta_filepath}" ] ; then
(
edo cd "${CACHE_LOCATION}"
download_silent "${META_DOWNLOAD_URL}"
) || die "downloading failed!"
fi
check_meta_file_version "${meta_file_name}" "${META_DOWNLOAD_FORMAT}"
printf "%s" "${CACHE_LOCATION}/${meta_file_name}"
unset meta_file_name meta_filepath
}
# @FUNCTION: known_tool_versions
# @USAGE: <tool>
# @DESCRIPTION:
# Prints the known tool versions from
# META_VERSION_URL.
# @STDOUT: known ghc versions
known_tool_versions() {
[ -z "$1" ] && die "Internal error: no argument given to posix_realpath"
mytool=$1
meta_file="$(get_meta_version_file)"
awk "
NF {
if (\$1 == \"${mytool}\") {
print \$2
}
}" "${meta_file}" || die "awk failed!"
unset mytool meta_file
}
# @FUNCTION: known_tool_tags
# @USAGE: <tool>
# @DESCRIPTION:
# Prints the known tool tags from
# META_VERSION_URL.
# @STDOUT: known tool tags
known_tool_tags() {
[ -z "$1" ] && die "Internal error: no argument given to known_tool_tags"
mytool=$1
meta_file="$(get_meta_version_file)"
awk "
NF {
if (\$1 == \"${mytool}\") {
split(\$3,a,\",\");
for (i in a) {
print a[i]
}
}
}" "${meta_file}" | sort -u || die "awk failed!"
unset mytool meta_file
}
# @FUNCTION: array_contains
# @USAGE: <element> <array>
# @DESCRIPTION:
# Checks whether the given elements
# is in the array.
# @RETURNS: returns 0 if the element is in the array, 1 otherwise
array_contains() {
{ [ -z "$1" ] || [ -z "$2" ] ;} && die "Internal error: not enough arguments given to array_contains"
element=$1
array=$2
for e in ${array} ; do
if [ "${e}" = "${element}" ] ; then
unset e element array
return 0
fi
done
unset e element array
return 1
}
############################ ############################
@ -1419,7 +1531,7 @@ print_debug_info() {
echo " Script update url: ${SCRIPT_UPDATE_URL}" echo " Script update url: ${SCRIPT_UPDATE_URL}"
echo " GHC download baseurl: ${GHC_DOWNLOAD_BASEURL}" echo " GHC download baseurl: ${GHC_DOWNLOAD_BASEURL}"
echo " Meta download url ${META_DOWNLOAD_URL}" echo " Meta download url ${META_DOWNLOAD_URL}"
echo " Meta download format ${META_DOWNLOAD_URL_FORMAT}" echo " Meta download format ${META_DOWNLOAD_FORMAT}"
echo " Meta version url ${META_VERSION_URL}" echo " Meta version url ${META_VERSION_URL}"
echo " Meta version format ${META_VERSION_FORMAT}" echo " Meta version format ${META_VERSION_FORMAT}"
echo echo
@ -1442,14 +1554,7 @@ print_debug_info() {
list() { list() {
mytool=$1 mytool=$1
meta_file_name="$(basename "${META_VERSION_URL}")" meta_file="$(get_meta_version_file)"
(
edo cd "${CACHE_LOCATION}"
download_silent "${META_VERSION_URL}"
check_meta_file_version "${meta_file_name}" "${META_VERSION_FORMAT}"
echo "Available upstream versions:" echo "Available upstream versions:"
echo echo
@ -1459,18 +1564,17 @@ list() {
if (\$1 != \"#\") { if (\$1 != \"#\") {
print \$1 \" \" \$2 print \$1 \" \" \$2
} }
}" "${meta_file_name}" || die "awk failed!" }" "${meta_file}" || die "awk failed!"
else else
awk " awk "
NF { NF {
if (\$1 == \"${mytool}\") { if (\$1 == \"${mytool}\") {
print \$1 \" \" \$2 print \$1 \" \" \$2
} }
}" "${meta_file_name}" || die "awk failed!" }" "${meta_file}" || die "awk failed!"
fi fi
)
unset mytool meta_file_name unset mytool meta_file
} }
@ -1484,11 +1588,6 @@ if [ -z "$HOME" ] ; then
die "HOME env not set, cannot operate" die "HOME env not set, cannot operate"
fi fi
if ! is_sourced ; then
edo mkdir -p "${INSTALL_BASE}"
edo mkdir -p "${BIN_LOCATION}"
edo mkdir -p "${CACHE_LOCATION}"
fi
@ -1530,7 +1629,20 @@ if ! is_sourced ; then
usage usage
fi fi
;; ;;
*) *) ## startup tasks ##
edo mkdir -p "${INSTALL_BASE}"
edo mkdir -p "${BIN_LOCATION}"
edo mkdir -p "${CACHE_LOCATION}"
# clean up old meta files
if [ -e "${CACHE_LOCATION}/$(basename "${META_VERSION_URL}")" ] ; then
edo rm "${CACHE_LOCATION}/$(basename "${META_VERSION_URL}")"
fi
if [ -e "${CACHE_LOCATION}/$(basename "${META_DOWNLOAD_URL}")" ] ; then
edo rm "${CACHE_LOCATION}/$(basename "${META_DOWNLOAD_URL}")"
fi
# check for available commands # check for available commands
missing_commands="$(check_required_commands ${DOWNLOADER})" missing_commands="$(check_required_commands ${DOWNLOADER})"
if [ -n "${missing_commands}" ] ; then if [ -n "${missing_commands}" ] ; then
@ -1556,7 +1668,14 @@ if ! is_sourced ; then
fi fi
install_ghc "${_tool_ver}" install_ghc "${_tool_ver}"
else else
# could be a version or a tag, let's check
if array_contains "${GHC_VER}" "$(known_tool_versions "ghc")" ; then
install_ghc "${GHC_VER}" install_ghc "${GHC_VER}"
elif array_contains "${GHC_VER}" "$(known_tool_tags "ghc")" ; then
install_ghc "$(get_tool_ver_from_tag "ghc" "${GHC_VER}")"
else
die "\"${GHC_VER}\" is not a known version or tag!"
fi
fi fi
break;; break;;
set) set)
@ -1629,7 +1748,14 @@ if ! is_sourced ; then
esac esac
done done
if [ -n "${CABAL_VER}" ] ; then if [ -n "${CABAL_VER}" ] ; then
# could be a version or a tag, let's check
if array_contains "${CABAL_VER}" "$(known_tool_versions "cabal-install")" ; then
install_cabal "${CABAL_VER}" install_cabal "${CABAL_VER}"
elif array_contains "${CABAL_VER}" "$(known_tool_tags "cabal-install")" ; then
install_cabal "$(get_tool_ver_from_tag "cabal-install" "${CABAL_VER}")"
else
die "\"${CABAL_VER}\" is not a known version or tag!"
fi
else else
_cabal_ver="$(get_tool_ver_from_tag "cabal-install" "recommended")" _cabal_ver="$(get_tool_ver_from_tag "cabal-install" "recommended")"
if [ -z "${_cabal_ver}" ] ; then if [ -z "${_cabal_ver}" ] ; then