Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make the update script work on macOS #349

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 107 additions & 28 deletions update_skyscraper.sh
Original file line number Diff line number Diff line change
@@ -1,40 +1,119 @@
#!/bin/bash
{
LATEST=`wget -q -O - "https://api.github.com/repos/muldjord/skyscraper/releases/latest" | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'`

if [ ! -f VERSION ]
then
echo "VERSION=0.0.0" > VERSION
fi
source VERSION
LATEST_URL='https://api.github.com/repos/muldjord/skyscraper/releases/latest'
LATEST=`wget -q -O - $LATEST_URL | grep '"tag_name":' | sed -E 's/.*"([^"]+)".*/\1/'`
VERSION_FILE='VERSION'

handle_error () {
local EXITCODE=$?
local ACTION=$1
rm --force VERSION
echo "--- Failed to $ACTION Skyscraper v.${LATEST}, exiting with code $EXITCODE ---"
exit $EXITCODE
local EXITCODE=$?
local ACTION=$1
rm -f "$VERSION_FILE"
echo "--- Failed to $ACTION Skyscraper v.${LATEST}, exiting with code $EXITCODE ---"
exit $EXITCODE
}

prep () {
if [[ ! $(which qmake) ]]
then
handle_error "find 'qmake' in your PATH. Did you install QT?"
fi

if [[ ! $(which wget) ]]
then
handle_error "find 'wget' in your PATH. Did you install wget?"
fi

if [ ! -f "$VERSION_FILE" ]
then
echo "VERSION=0.0.0" > "$VERSION_FILE"
fi

source "$VERSION_FILE"
}

running_mac_os () {
[[ "$OSTYPE" == "darwin"* ]] && return
false
}

fetch_latest () {
echo "--- Fetching Skyscraper v.$LATEST ---"
wget -N https://github.com/muldjord/skyscraper/archive/${LATEST}.tar.gz || handle_error "fetch"
}

untar_latest () {
echo "--- Unpacking ---"
command="tar xvzf ${LATEST}.tar.gz --strip-components 1"
# BSD tar doesn't recognize --overwrite, and overwrites by default
if [[ ! running_mac_os ]]
then
command="$command --overwrite"
fi
$command || handle_error "unpack"
rm ${LATEST}.tar.gz
}

clean_out_old_build () {
echo "--- Cleaning out old build if one exists ---"
# don't attempt 'make clean' if a Makefile doesn't yet exist
if [[ -e Makefile ]]
then
make --ignore-errors clean
fi
rm -f .qmake.stash
}

generate_makefile () {
echo "--- Generating Makefile with Qmake ---"
qmake || handle_error "makefile"
}

build_skyscraper () {
echo "--- Building Skyscraper v.$LATEST ---"

# On macOS if the version file is in the same location as the source,
# it will be treated as a source file and fail the build. Rename the
# file and then rename it back, regardless of build status.
if [[ running_mac_os ]]
then
mv "$VERSION_FILE" "${VERSION_FILE}.bak"
fi

make -j$(nproc)
result=$?

if [[ running_mac_os ]]
then
mv "${VERSION_FILE}.bak" "$VERSION_FILE"
fi

if [[ ! $result -eq 0 ]]
then
handle_error "build"
fi
}

install_latest () {
echo "--- Installing Skyscraper v.$LATEST ---"
echo "NOTE: Installation is done with sudo, you may be prompted for a password"
sudo make install || handle_error "install"
echo "--- Skyscraper has been updated to v.$LATEST ---"
}

prep

if [ $LATEST != $VERSION ]
then
echo "--- Fetching Skyscraper v.$LATEST ---"
wget -N https://github.com/muldjord/skyscraper/archive/${LATEST}.tar.gz || handle_error "fetch"
echo "--- Unpacking ---"
tar xvzf ${LATEST}.tar.gz --strip-components 1 --overwrite || handle_error "unpack"
rm ${LATEST}.tar.gz
echo "--- Cleaning out old build if one exists ---"
make --ignore-errors clean
rm --force .qmake.stash
qmake || handle_error "clean old"
echo "--- Building Skyscraper v.$LATEST ---"
make -j$(nproc) || handle_error "build"
echo "--- Installing Skyscraper v.$LATEST ---"
sudo make install || handle_error "install"
echo "--- Skyscraper has been updated to v.$LATEST ---"
fetch_latest
untar_latest
clean_out_old_build
generate_makefile
build_skyscraper
install_latest
else
echo "--- Skyscraper is already the latest version, exiting ---"
echo "You can force a reinstall by removing the VERSION file by running rm VERSION. Then rerun ./update_skyscraper.sh afterwards."
echo "--- Skyscraper is already the latest version, exiting ---"
echo "You can force a reinstall by removing the '$VERSION_FILE' file by"
echo "running 'rm $VERSION_FILE'. Then rerun ./update_skyscraper.sh afterwards."
fi
exit
}