Skip to content

Instantly share code, notes, and snippets.

@joshcangit
Last active May 18, 2022 13:13
Show Gist options
  • Save joshcangit/f1a46310c2c81b0c3c8991a83c6fa0ae to your computer and use it in GitHub Desktop.
Save joshcangit/f1a46310c2c81b0c3c8991a83c6fa0ae to your computer and use it in GitHub Desktop.
Script to download and or update ninja build tool
#!/bin/sh
DIR=$(dirname $(realpath $0))
folder_uid=$(id -u $(find "${DIR}" -maxdepth 0 -printf '%u\n'))
[ "${folder_uid}" -eq 0 ] && [ "$(id -u)" -ne "${folder_uid}" ] && sudo="sudo "
latest_version() {
local RELEASE="https://github.com/ninja-build/ninja/releases/latest"
case "$CMD" in
*xh*) xh HEAD $RELEASE --no-check-status -h | awk '/^location:/{print $2}' | awk -F/ '{print $NF}';;
*curl*) curl -ISs $RELEASE | awk '/^location:/{print $2}' | awk -F/ '{print $NF}';;
*wget*) wget --max-redirect=0 $RELEASE 2>&1 | awk '/^Location:/{print $2}' | awk -F/ '{print $NF}';;
esac
}
download() {
local URL=$1
local FILE="$(basename ${URL})"
case "$CMD" in
*aria2c*)
aria2c $URL -j5 -x10 -s8 -k1M --optimize-concurrent-downloads --header="Accept-Encoding: zstd, gzip, deflate" -c;;
*xh*)
xh -Fcd $URL Accept-Encoding:'zstd, gzip, deflate';;
*curl*)
curl -#LOJ $URL --compressed -H "Accept-Encoding: zstd, gzip, deflate" -C -;;
*wget*)
wget -q --show-progress $URL --header="Accept-Encoding: zstd, gzip, deflate" -c;;
esac
unzip $FILE && rm $FILE
chmod +x ninja
${sudo}mv -f ninja $DIR
}
CMD="$(type -P {aria2c,xh,curl,wget})"
case "$(type ${DIR}/ninja)" in
*not\ found) download "https://github.com/ninja-build/ninja/releases/latest/download/ninja-linux.zip";;
*ninja)
VERSION=$(${DIR}/ninja --version)
LATEST=$(latest_version)
OUTPUT=$(printf "$VERSION\n$LATEST" | sed '/^$/d' | sort -Vr | head -1)
if [ "$VERSION"=="$OUTPUT" ]; then
echo "Ninja is up to date"
else
download "https://github.com/ninja-build/ninja/releases/download/$LATEST/ninja-linux.zip"
fi;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment