Last active
December 28, 2020 08:14
-
-
Save fadeltd/77e49d84aa6dd3887c408b90fe5e88ac to your computer and use it in GitHub Desktop.
Download, update, uninstall, create shortcut for https://github.com/google/bundletool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env sh | |
shopt -s expand_aliases | |
source ~/.bash_profile | |
# Function to grep and get value from github releases | |
function extract_value { | |
echo "$1" | grep "$2" | cut -d : -f 2,3 | tr -d \", | |
} | |
function latest_bundletool_url { | |
echo "https://api.github.com/repos/google/bundletool/releases/latest" | |
} | |
function specific_bundletool_url { | |
version=${1} | |
echo "https://api.github.com/repos/google/bundletool/releases/tags/${version}" | |
} | |
function release_bundletool { | |
version=${1} | |
response_file='github_bundletool_response.json' | |
if [ ! ${version} ]; then api_url=$(latest_bundletool_url); else api_url=$(specific_bundletool_url $version); fi | |
curl -s ${api_url} > ${response_file} | |
response=$(cat ${response_file}) | |
rm ${response_file} | |
echo "${response}" | |
} | |
function download_bundletool { | |
version=${1} | |
response=$(release_bundletool ${version}) | |
download_url=$(extract_value "${response}" "browser_download_url") | |
bundletool_jar="/usr/local/bin/bundletool.jar" | |
curl --progress-bar -o ${bundletool_jar} -L ${download_url} | |
bundletool_path="/usr/local/bin/bundletool" | |
echo "#!/usr/bin/env bash" > ${bundletool_path} | |
echo 'java -jar /usr/local/bin/bundletool.jar "$@"' >> ${bundletool_path} | |
chmod +x ${bundletool_path} | |
} | |
function install_bundletool { | |
echo >&2 "Bundletool is not installed. Installing bundletool." | |
version=${1} | |
if [ ! ${version} ]; then bundletool="latest bundletool"; else bundletool="bundletool version ${version}"; fi | |
echo "Downloading ${bundletool}..." | |
download_bundletool ${version} | |
echo "Done installing bundletool, you can now restart terminal and try 'bundletool help'" | |
} | |
function current_version { | |
current_version=`bundletool version` | |
current_version=${current_version//[^0-9.]/} | |
echo ${current_version} | |
} | |
function update_bundletool { | |
# Hit github API, to get latest release | |
current_version=$(current_version) | |
echo "Checking for latest bundletool version (current ${current_version})..." | |
# Get values that we need | |
response=$(release_bundletool) | |
tag_name=$(extract_value "${response}" "tag_name") | |
version="${tag_name//[^0-9.]/}" | |
if [ ${current_version} == ${version} ]; then | |
echo "Your bundletool is already updated to the latest version." | |
exit 0 | |
fi | |
echo "There is a new update for bundletool version ${current_version} -> (${version})" | |
echo "Updating bundletool to version ${version}..." | |
# Delete older version | |
uninstall_bundletool | |
# Download latest release, and delete the old one | |
echo "Downloading latest bundletool version (${version})..." | |
download_bundletool | |
echo "Done updating bundletool to version ${version}" | |
} | |
function uninstall_bundletool { | |
bash_wrapper="/usr/local/bin/bundletool" | |
rm ${bash_wrapper} | |
jar_file="/usr/local/bin/bundletool.jar" | |
rm ${jar_file} | |
echo "Finished uninstalling older version of bundletool" | |
} | |
if command -v bundletool &> /dev/null; then | |
if [[ ${1} = 'uninstall' ]]; then | |
uninstall_bundletool | |
else | |
update_bundletool | |
fi | |
else | |
if [[ ${1} = 'uninstall' ]]; then | |
echo 'Cannot uninstall bundletool, bundletool is not installed' | |
else | |
version=$1 | |
install_bundletool ${version} | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This script can install, update and, uninstall bundletool (installed from this script). This script will install bundletool jar file to
/usr/local/bin
, and a wrapper bash script that runsjava -jar bundletool.jar
How to use:
install_bundletool.sh
, or you can use it directly withcurl -sL https://git.io/JLyon | bash
sh install_bundletool.sh
sh install_bundletool.sh ${version}
, examplesh install_bundletool.sh 1.4.0
sh install_bundletool.sh
, it will automatically uninstall older version and install the new onesh install_bundletool.sh uninstall