1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 07:46:28 +00:00

fix: Update install.sh for Windows (#1300)

Adds several cross-platform utility functions to the install.sh script and 
rewrites core functionality so that it might work on Windows. Among these are:
 - Correct platform extensions to match the output of `detect_platform`
 - Check to make sure installation directory actually exists
 - If sudo is not available, print a message asking the user to manually 
   escalate
 - Unpack zip files (used for Windows builds) without using pipes, since zip 
   files cannot be read through pipes

After these changes, install.sh works on a testing copy of Git Bash on Windows,
though it still has known issues (e.g. if the `unzip` program is not installed,
it will crash).
This commit is contained in:
Kevin Song 2020-06-09 17:21:35 -05:00 committed by GitHub
parent 4a4266f6c4
commit 3ebd2c0ad5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -49,6 +49,31 @@ complete() {
printf "${GREEN}${NO_COLOR} $@\n" printf "${GREEN}${NO_COLOR} $@\n"
} }
# Gets path to a temporary file, even if
get_tmpfile(){
local suffix
suffix="$1"
if hash mktemp; then
printf "$(mktemp).${suffix}"
else
# No really good options here--let's pick a default + hope
printf "/tmp/starship.%s" "${suffix}"
fi
}
# Test if a location is writeable by trying to write to it. Windows does not let
# you test writeability other than by writing: https://stackoverflow.com/q/1999988
test_writeable(){
local path
path="${1:-}/test.txt"
if touch "${path}" 2> /dev/null; then
rm "${path}"
return 0
else
return 1
fi
}
fetch() { fetch() {
local command local command
if hash curl 2>/dev/null; then if hash curl 2>/dev/null; then
@ -82,22 +107,58 @@ fetch() {
fi fi
} }
fetch_and_unpack(){
local sudo
local tmpfile
sudo="$1"
# I'd like to separate this into a fetch() and unpack() function, but I can't
# figure out how to get bash functions to read STDIN/STDOUT from pipes
if [ "${EXT}" = "tar.gz" ]; then
fetch "${URL}" | ${sudo} tar xzf${VERBOSE} - -C "${BIN_DIR}"
elif [ "${EXT}" = "zip" ]; then
# According to https://unix.stackexchange.com/q/2690, zip files cannot be read
# through a pipe. We'll have to do our own file-based setup.
tmpfile="$(get_tmpfile "${EXT}")"
fetch "${URL}" > "${tmpfile}"
${sudo} unzip "${tmpfile}" -d "${BIN_DIR}"
rm "${tmpfile}"
else
error "Unknown package extension."
info "This almost certainly results from a bug in this script--please file a"
info "bug report at https://github.com/starship/starship/issues"
exit 1
fi
}
elevate_priv(){
if ! hash sudo 2>/dev/null; then
error "Could not find the command \"sudo\", needed to get permissions for install."
info "If you are on Windows, please run your shell as an administrator, then"
info "rerun this script. Otherwise, please run this script as root, or install"
info "sudo."
exit 1
fi
if ! sudo -v; then
error "Superuser not granted, aborting installation"
exit 1
fi
}
install() { install() {
local sudo
local msg local msg
if [ -w "$BIN_DIR" ]; then local sudo
sudo=""
msg="Installing Starship, please wait…" if test_writeable "${BIN_DIR}" ; then
else sudo=""
warn "Escalated permission are required to install to ${BIN_DIR}" msg="Installing Starship, please wait…"
sudo -v || (error "Aborting installation (Please provide root password)";exit 1) else
sudo="sudo" warn "Escalated permission are required to install to ${BIN_DIR}"
msg="Installing Starship as root, please wait…" elevate_priv
fi sudo="sudo"
info "$msg" msg="Installing Starship as root, please wait…"
fetch "${URL}" \ fi
| ${sudo} tar xzf${VERBOSE} - \ info "$msg"
-C "${BIN_DIR}" fetch_and_unpack "${sudo}"
} }
# Currently supporting: # Currently supporting:
@ -168,6 +229,12 @@ confirm() {
check_bin_dir() { check_bin_dir() {
local bin_dir="$1" local bin_dir="$1"
if [ ! -d "$BIN_DIR" ]; then
error "Installation location $BIN_DIR does not appear to be a directory"
info "Make sure the location exists and is a directory, then try again."
exit 1
fi
# https://stackoverflow.com/a/11655875 # https://stackoverflow.com/a/11655875
local good local good
good=$( IFS=: good=$( IFS=:
@ -247,14 +314,14 @@ fi
echo echo
EXT=tar.gz EXT=tar.gz
if [ "${PLATFORM}" = win ]; then if [ "${PLATFORM}" = "pc-windows-msvc" ]; then
EXT=zip EXT=zip
fi fi
URL="${BASE_URL}/latest/download/starship-${ARCH}-${PLATFORM}.${EXT}" URL="${BASE_URL}/latest/download/starship-${ARCH}-${PLATFORM}.${EXT}"
info "Tarball URL: ${UNDERLINE}${BLUE}${URL}${NO_COLOR}" info "Tarball URL: ${UNDERLINE}${BLUE}${URL}${NO_COLOR}"
check_bin_dir "${BIN_DIR}"
confirm "Install Starship ${GREEN}latest${NO_COLOR} to ${BOLD}${GREEN}${BIN_DIR}${NO_COLOR}?" confirm "Install Starship ${GREEN}latest${NO_COLOR} to ${BOLD}${GREEN}${BIN_DIR}${NO_COLOR}?"
check_bin_dir "${BIN_DIR}"
install install
complete "Starship installed" complete "Starship installed"
@ -280,6 +347,5 @@ info "Please follow the steps for your shell to complete the installation:
${BOLD}${UNDERLINE}Ion${NO_COLOR} ${BOLD}${UNDERLINE}Ion${NO_COLOR}
Add the following to the end of ${BOLD}~/.config/ion/initrc${NO_COLOR}: Add the following to the end of ${BOLD}~/.config/ion/initrc${NO_COLOR}:
eval $(starship init ion) eval \$(starship init ion)
" "