ghcup-hs/bootstrap-haskell

315 lines
8.9 KiB
Plaintext
Raw Normal View History

2020-04-11 19:40:01 +00:00
#!/bin/sh
# Main settings:
# * BOOTSTRAP_HASKELL_NONINTERACTIVE - any nonzero value for noninteractive installation
# * BOOTSTRAP_HASKELL_NO_UPGRADE - any nonzero value to not trigger the upgrade
# * GHCUP_USE_XDG_DIRS - any nonzero value to respect The XDG Base Directory Specification
# * BOOTSTRAP_HASKELL_VERBOSE - any nonzero value for more verbose installation
# * BOOTSTRAP_HASKELL_GHC_VERSION
# * BOOTSTRAP_HASKELL_CABAL_VERSION
# License: LGPL-3.0
2020-04-11 19:40:01 +00:00
# safety subshell to avoid executing anything in case this script is not downloaded properly
(
: "${GHCUP_INSTALL_BASE_PREFIX:=$HOME}"
2020-07-28 23:43:00 +00:00
export GHCUP_USE_XDG_DIRS
if [ -n "${GHCUP_USE_XDG_DIRS}" ] ; then
GHCUP_DIR=${XDG_DATA_HOME:=$HOME/.local/share}/ghcup
2020-07-28 23:43:00 +00:00
GHCUP_BIN=${XDG_BIN_HOME:=$HOME/.local/bin}
else
GHCUP_DIR=${GHCUP_INSTALL_BASE_PREFIX}/.ghcup
GHCUP_BIN=${GHCUP_INSTALL_BASE_PREFIX}/.ghcup/bin
fi
: "${BOOTSTRAP_HASKELL_GHC_VERSION:=recommended}"
: "${BOOTSTRAP_HASKELL_CABAL_VERSION:=recommended}"
2020-04-11 19:40:01 +00:00
die() {
(>&2 printf "\\033[0;31m%s\\033[0m\\n" "$1")
exit 2
}
2020-11-27 18:14:43 +00:00
edo() {
2020-04-11 19:40:01 +00:00
"$@" || die "\"$*\" failed!"
}
eghcup() {
edo _eghcup "$@"
}
_eghcup() {
2020-04-11 19:40:01 +00:00
if [ -z "${BOOTSTRAP_HASKELL_VERBOSE}" ] ; then
ghcup "$@"
2020-04-11 19:40:01 +00:00
else
ghcup --verbose "$@"
2020-04-11 19:40:01 +00:00
fi
}
2020-07-29 19:07:05 +00:00
_done() {
echo
echo "All done!"
echo
echo "To start a simple repl, run:"
echo " ghci"
echo
echo "To start a new haskell project in the current directory, run:"
echo " cabal init --interactive"
echo
echo "To install other GHC versions, run:"
echo " ghcup tui"
exit 0
}
2020-04-11 19:40:01 +00:00
download_ghcup() {
_plat="$(uname -s)"
_arch=$(uname -m)
2021-03-07 13:50:08 +00:00
_ghver="0.1.14"
2020-07-14 17:07:19 +00:00
_base_url="https://downloads.haskell.org/~ghcup"
2020-04-11 19:40:01 +00:00
case "${_plat}" in
"linux"|"Linux")
2020-04-13 13:22:16 +00:00
case "${_arch}" in
x86_64|amd64)
# we could be in a 32bit docker container, in which
# case uname doesn't give us what we want
if [ "$(getconf LONG_BIT)" = "32" ] ; then
_url=${_base_url}/${_ghver}/i386-linux-ghcup-${_ghver}
elif [ "$(getconf LONG_BIT)" = "64" ] ; then
_url=${_base_url}/${_ghver}/x86_64-linux-ghcup-${_ghver}
else
die "Unknown long bit size: $(getconf LONG_BIT)"
fi
2020-04-13 13:22:16 +00:00
;;
i*86)
2020-07-14 17:07:19 +00:00
_url=${_base_url}/${_ghver}/i386-linux-ghcup-${_ghver}
2020-04-13 13:22:16 +00:00
;;
2021-02-26 14:38:35 +00:00
armv7*)
_url=${_base_url}/${_ghver}/armv7-linux-ghcup-${_ghver}
;;
aarch64|arm64|armv8l)
_url=${_base_url}/${_ghver}/aarch64-linux-ghcup-${_ghver}
;;
2020-04-13 13:22:16 +00:00
*) die "Unknown architecture: ${_arch}"
;;
esac
2020-04-11 19:40:01 +00:00
;;
"FreeBSD"|"freebsd")
2020-04-13 13:22:16 +00:00
case "${_arch}" in
x86_64|amd64)
;;
i*86)
die "i386 currently not supported!"
;;
*) die "Unknown architecture: ${_arch}"
;;
esac
2020-07-14 17:07:19 +00:00
_url=${_base_url}/${_ghver}/x86_64-portbld-freebsd-ghcup-${_ghver}
2020-04-11 19:40:01 +00:00
;;
"Darwin"|"darwin")
2020-04-13 13:22:16 +00:00
case "${_arch}" in
x86_64|amd64)
;;
i*86)
die "i386 currently not supported!"
;;
*) die "Unknown architecture: ${_arch}"
;;
esac
2020-07-14 17:07:19 +00:00
_url=${_base_url}/${_ghver}/x86_64-apple-darwin-ghcup-${_ghver} ;;
2020-04-11 19:40:01 +00:00
*) die "Unknown platform: ${_plat}"
;;
esac
2020-07-28 23:43:00 +00:00
edo curl -Lf "${_url}" > "${GHCUP_BIN}"/ghcup
2020-07-28 23:43:00 +00:00
edo chmod +x "${GHCUP_BIN}"/ghcup
2020-11-27 18:14:43 +00:00
edo mkdir -p "${GHCUP_DIR}"
2020-07-28 23:43:00 +00:00
cat <<-EOF > "${GHCUP_DIR}"/env || die "Failed to create env file"
export PATH="\$HOME/.cabal/bin:${GHCUP_BIN}:\$PATH"
EOF
# shellcheck disable=SC1090
2020-07-28 23:43:00 +00:00
edo . "${GHCUP_DIR}"/env
eghcup upgrade
2020-07-14 17:07:19 +00:00
unset _plat _arch _url _ghver _base_url
2020-04-11 19:40:01 +00:00
}
echo
echo "Welcome to Haskell!"
echo
echo "This script will download and install the following binaries:"
2020-07-29 19:07:05 +00:00
echo " * ghcup - The Haskell toolchain installer"
echo " (for managing GHC/cabal versions)"
echo " * ghc - The Glasgow Haskell Compiler"
echo " * cabal - The Cabal build tool"
2020-04-11 19:40:01 +00:00
echo
2020-07-28 23:43:00 +00:00
if [ -z "${GHCUP_USE_XDG_DIRS}" ] ; then
2020-07-29 19:07:05 +00:00
echo "ghcup installs only into the following directory,"
echo "which can be removed anytime:"
2020-07-28 23:43:00 +00:00
echo " $GHCUP_INSTALL_BASE_PREFIX/.ghcup"
else
2020-07-29 19:07:05 +00:00
echo "ghcup installs into XDG directories as long as"
echo "'GHCUP_USE_XDG_DIRS' is set."
2020-07-28 23:43:00 +00:00
fi
2020-04-11 19:40:01 +00:00
echo
if [ -z "${BOOTSTRAP_HASKELL_NONINTERACTIVE}" ] ; then
printf "\\033[0;35m%s\\033[0m\\n" "Press ENTER to proceed or ctrl-c to abort."
2020-04-11 19:40:01 +00:00
printf "\\033[0;35m%s\\033[0m\\n" "Note that this script can be re-run at any given time."
echo
# Wait for user input to continue.
# shellcheck disable=SC2034
read -r answer </dev/tty
fi
2020-07-28 23:43:00 +00:00
edo mkdir -p "${GHCUP_BIN}"
2020-04-11 19:40:01 +00:00
if command -V "ghcup" >/dev/null 2>&1 ; then
if [ -z "${BOOTSTRAP_HASKELL_NO_UPGRADE}" ] ; then
_eghcup upgrade || download_ghcup
2020-04-11 19:40:01 +00:00
fi
else
download_ghcup
fi
echo
echo "$(ghcup tool-requirements)"
echo
if [ -z "${BOOTSTRAP_HASKELL_NONINTERACTIVE}" ] ; then
printf "\\033[0;35m%s\\033[0m\\n" "Press ENTER to proceed or ctrl-c to abort."
2020-04-11 19:40:01 +00:00
printf "\\033[0;35m%s\\033[0m\\n" "Installation may take a while."
echo
# Wait for user input to continue.
# shellcheck disable=SC2034
read -r answer </dev/tty
fi
eghcup --cache install ghc "${BOOTSTRAP_HASKELL_GHC_VERSION}"
2020-04-11 19:40:01 +00:00
eghcup set ghc "${BOOTSTRAP_HASKELL_GHC_VERSION}"
eghcup --cache install cabal "${BOOTSTRAP_HASKELL_CABAL_VERSION}"
2020-04-11 19:40:01 +00:00
edo cabal new-update
printf "\\033[0;35m%s\\033[0m\\n" ""
printf "\\033[0;35m%s\\033[0m\\n" "Installation done!"
printf "\\033[0;35m%s\\033[0m\\n" ""
2020-04-11 19:40:01 +00:00
if [ -z "${BOOTSTRAP_HASKELL_NONINTERACTIVE}" ] ; then
2020-09-29 08:52:31 +00:00
printf "\\033[0;35m%s\\033[0m\\n" "Do you want to install haskell-language-server (HLS) now?"
printf "\\033[0;35m%s\\033[0m\\n" "HLS is a language-server that provides IDE-like functionality"
printf "\\033[0;35m%s\\033[0m\\n" "and can integrate with different editors, such as Vim, Emacs, VS Code, Atom, ..."
printf "\\033[0;35m%s\\033[0m\\n" "Also see https://github.com/haskell/haskell-language-server/blob/master/README.md"
printf "\\033[0;35m%s\\033[0m\\n" ""
printf "\\033[0;35m%s\\033[0m\\n" "Answer with YES or NO and press ENTER."
printf "\\033[0;35m%s\\033[0m\\n" ""
2020-09-29 08:12:03 +00:00
while true; do
read -r hls_answer </dev/tty
case $hls_answer in
[Yy]*)
eghcup --cache install hls
break ;;
[Nn]*)
break ;;
*)
echo "Please type YES or NO and press enter.";;
esac
done
2020-04-11 19:40:01 +00:00
echo "In order to run ghc and cabal, you need to adjust your PATH variable."
2020-07-28 23:43:00 +00:00
echo "You may want to source '$GHCUP_DIR/env' in your shell"
2020-04-11 19:40:01 +00:00
echo "configuration to do so (e.g. ~/.bashrc)."
case $SHELL in
*/zsh) # login shell is zsh
GHCUP_PROFILE_FILE="$HOME/.zshrc"
MY_SHELL="zsh" ;;
*/bash) # login shell is bash
GHCUP_PROFILE_FILE="$HOME/.bashrc"
2020-04-11 19:40:01 +00:00
MY_SHELL="bash" ;;
*/sh) # login shell is sh, but might be a symlink to bash or zsh
if [ -n "${BASH}" ] ; then
GHCUP_PROFILE_FILE="$HOME/.bashrc"
2020-04-11 19:40:01 +00:00
MY_SHELL="bash"
elif [ -n "${ZSH_VERSION}" ] ; then
GHCUP_PROFILE_FILE="$HOME/.zshrc"
MY_SHELL="zsh"
else
2020-07-29 19:07:05 +00:00
_done
2020-04-11 19:40:01 +00:00
fi
;;
*/fish) # login shell is fish
GHCUP_PROFILE_FILE="$HOME/.config/fish/config.fish"
MY_SHELL="fish" ;;
2020-07-29 19:07:05 +00:00
*) _done ;;
2020-04-11 19:40:01 +00:00
esac
printf "\\033[0;35m%s\\033[0m\\n" ""
printf "\\033[0;35m%s\\033[0m\\n" "Detected ${MY_SHELL} shell on your system..."
printf "\\033[0;35m%s\\033[0m\\n" "If you want ghcup to automatically add the required PATH variable to \"${GHCUP_PROFILE_FILE}\""
printf "\\033[0;35m%s\\033[0m\\n" "answer with YES, otherwise with NO and press ENTER."
printf "\\033[0;35m%s\\033[0m\\n" ""
while true; do
read -r next_answer </dev/tty
case $next_answer in
[Yy]*)
case $MY_SHELL in
"") break ;;
fish)
2020-08-09 08:33:32 +00:00
if ! grep -q "ghcup-env" "${GHCUP_PROFILE_FILE}" ; then
echo "# ghcup-env" >> "${GHCUP_PROFILE_FILE}"
echo "set -q GHCUP_INSTALL_BASE_PREFIX[1]; or set GHCUP_INSTALL_BASE_PREFIX \$HOME" >> "${GHCUP_PROFILE_FILE}"
echo "test -f $GHCUP_DIR/env ; and set -gx PATH \$HOME/.cabal/bin $GHCUP_BIN \$PATH" >> "${GHCUP_PROFILE_FILE}"
2020-08-09 08:33:32 +00:00
fi
break ;;
2020-09-28 07:59:35 +00:00
bash)
if ! grep -q "ghcup-env" "${GHCUP_PROFILE_FILE}" ; then
echo "[ -f \"${GHCUP_DIR}/env\" ] && source \"${GHCUP_DIR}/env\" # ghcup-env" >> "${GHCUP_PROFILE_FILE}"
fi
case "$(uname -s)" in
"Darwin"|"darwin")
if ! grep -q "ghcup-env" "${HOME}/.bash_profile" ; then
echo "[[ -f ~/.bashrc ]] && source ~/.bashrc # ghcup-env" >> "${HOME}/.bash_profile"
fi
;;
esac
break ;;
zsh)
2020-08-09 08:33:32 +00:00
if ! grep -q "ghcup-env" "${GHCUP_PROFILE_FILE}" ; then
echo "[ -f \"${GHCUP_DIR}/env\" ] && source \"${GHCUP_DIR}/env\" # ghcup-env" >> "${GHCUP_PROFILE_FILE}"
fi
break ;;
esac
2020-04-11 19:40:01 +00:00
printf "\\033[0;35m%s\\033[0m\\n" "OK! ${GHCUP_PROFILE_FILE} has been modified. Restart your terminal for the changes to take effect,"
2020-07-28 23:43:00 +00:00
printf "\\033[0;35m%s\\033[0m\\n" "or type \"source ${GHCUP_DIR}/env\" to apply them in your current terminal session."
2020-07-29 19:07:05 +00:00
_done
;;
2020-04-11 19:40:01 +00:00
[Nn]*)
2020-07-29 19:07:05 +00:00
_done ;;
2020-04-11 19:40:01 +00:00
*)
echo "Please type YES or NO and press enter.";;
esac
done
fi
)
# vim: tabstop=4 shiftwidth=4 expandtab