build-phar

#!/usr/bin/env bash
# MUST BE RUN FROM PROJECT ROOT
#
# For automated environments, 
#       pass --build-phar as the first arg
#       bin/build-phar --build-phar 
# For automated environments that also want to force use of an incorrect phar-composer version, 
#       pass --build-phar as first arg and --use-bad-version as second arg
#       bin/build-phar --build-phar --use-bad-version
#
# The order described above is required
#

if [[ ! -d build ]];then
    mkdir build
fi

if [[ ! -f "build/phar-composer.phar" ]]; then
    phar_composer_url="https://github.com/clue/phar-composer/releases/download/v1.4.0/phar-composer-1.4.0.phar"
    if [[ "$1" == "--build-phar" ]];then 
        answer="y";
    else
        read -p \
        "
        phar-composer is not installed. 
        Download from $phar_composer_url? 
        (y/n)" \
        answer
    fi

    if [[ "$answer" == "y" || "$answer" == "Y" ]];then
        cd build;
        curl -JL -o phar-composer.phar $phar_composer_url 
        cd ..;
    else
        echo "Can't create phar without installing phar-composer. Try again."
        exit
    fi
fi

color_version="$(php build/phar-composer.phar --version)"

# xargs echo trims text. See https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable
# sed removes the backslashes in front of bash color indicators. I just added the 3Xms into the comparison below because it was easier than a better `sed` and idc
version="$(echo "$color_version" | xargs echo | sed 's/[^a-zA-Z0-9\. ]//g')"

if [[ "$version" != "phar-composer version 1.4.0" && "$version" != "32mpharcomposer39m version 33m1.4.039m" ]];then
    echo "Incorrect version of phar-composer"
    echo "Version: $color_version"

    read -p "Continue with incorrect phar-composer version? (y/n)" answer
    if [[ "$2" != "--use-bad-version" && "$answer" != "y" && "$answer" != "Y" ]];then
        echo "Delete existing build/phar-composer.phar then re-run build in order to get correct version";
        exit;
    fi
fi

if [[ -f build/scrawl.phar ]];then
    rm build/scrawl.phar
fi
php -d phar.readonly=0 build/phar-composer.phar build "$(pwd)/"
git_hash="git rev-parse --short HEAD"
mv code-scrawl.phar "build/scrawl.phar"

echo "

    bin/scrawl.phar created

"