diff --git a/script/install b/script/install index 60570cdc2..7f81a3e8f 100755 --- a/script/install +++ b/script/install @@ -33,16 +33,27 @@ ohai "Unzipping tuistenv..." unzip -o /tmp/tuistenv.zip -d /tmp/tuistenv > /dev/null ohai "Installing tuistenv..." -if [[ ! -d "/usr/local/bin" ]]; then - mkdir -p /usr/local/bin/ +INSTALL_DIR="/usr/local/bin" + +sudo_if_install_dir_not_writeable() { + local command="$1" + if [ -w $INSTALL_DIR ]; then + bash -c "${command}" + else + bash -c "sudo ${command}" + fi +} + +if [[ ! -d $INSTALL_DIR ]]; then + sudo_if_install_dir_not_writeable "mkdir -p ${INSTALL_DIR}" fi -if [[ -f "/usr/local/bin/tuist" ]]; then - rm /usr/local/bin/tuist +if [[ -f "${INSTALL_DIR}/tuist" ]]; then + sudo_if_install_dir_not_writeable "rm ${INSTALL_DIR}/tuist" fi -mv /tmp/tuistenv/tuistenv /usr/local/bin/tuist -chmod +x /usr/local/bin/tuist +sudo_if_install_dir_not_writeable "mv /tmp/tuistenv/tuistenv \"${INSTALL_DIR}/tuist\"" +sudo_if_install_dir_not_writeable "chmod +x \"${INSTALL_DIR}/tuist\"" rm -rf /tmp/tuistenv rm /tmp/tuistenv.zip diff --git a/script/uninstall b/script/uninstall index 10d181d17..e2e3cb3ec 100755 --- a/script/uninstall +++ b/script/uninstall @@ -20,7 +20,18 @@ warn() { printf "${tty_red}Warning${tty_reset}: %s\n" "$(chomp "$1")" } -rm -rf /usr/local/bin/tuist +INSTALL_DIR="/usr/local/bin" + +sudo_if_install_dir_not_writeable() { + local command="$1" + if [ -w $INSTALL_DIR ]; then + bash -c $command + else + bash -c "sudo ${command}" + fi +} + +sudo_if_install_dir_not_writeable "rm -rf /usr/local/bin/tuist" rm -rf ~/.tuist ohai "Tuist uninstalled"