Created
October 31, 2014 05:24
-
-
Save anonymous/2abf05e2bf91bdd8211c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/bin/sh | |
source ./generified_apk_installer_settings.config | |
## TESTS FOR REQUIRED CONFIG SETTINGS ## | |
#Require $FILE_URL | |
if [[ ! -n "$APK_URL" ]] ; then | |
echo "APK_URL variable not found from source. Please update your config." | |
exit 0 | |
fi | |
#Require $FILEPATH | |
if [[ ! -n "$FILEPATH" ]] ; then | |
echo "FILEPATH variable not found from source. Please update your config." | |
exit 0 | |
fi | |
#Require $PACKAGE | |
if [[ ! -n "$PACKAGE" ]] ; then | |
echo "PACKAGE variable not found from source. Please update your config." | |
exit 0 | |
fi | |
## MAKE SURE ADB IS RUNNING ## | |
adb devices | |
## DEFAULTS ## | |
FILE_URL="local copy" | |
LAUNCH_ENABLED="false" | |
## USAGE ## | |
usage(){ | |
echo | |
echo | |
echo | |
echo "#################### USAGE ####################" | |
echo | |
echo " -i: Install. This uninstalls the current app and carries out an install based on the supplied" | |
echo " parameters. Valid args include 'new', which will download the latest build, 'saved' which" | |
echo " will install the current locally saved build, and 'upgrade' which will install over the top of" | |
echo " the current build using install -r." | |
echo | |
echo " -c: Clear current install. This force stops the provided "'$PACKAGE'" and clears" | |
echo " it's current cache. "'$PACKAGE'" can be overridden if provided." | |
echo | |
echo " -l: Launch app. This launches the app with a given "'$OPTARG'". The args should map to valid" | |
echo " app entry points such as startup activities, intents, etc." | |
echo | |
echo " -h: Help. Displays script usage." | |
echo | |
echo "#################### /USAGE ####################" | |
echo | |
echo | |
echo | |
} | |
## DEVICEINFO ## | |
getConnectedDeviceInfo(){ | |
adb devices | grep device | grep -v attached | cut -f 1 > actiontmpfile | |
while read deviceId; | |
do | |
DEVICE_OS_VERSION=`adb -s $deviceId shell getprop ro.build.version.release` | |
DEVICE_OS_SDK=`adb -s $deviceId shell getprop ro.build.version.sdk` | |
DEVICE_CARRIER=`adb -s $deviceId shell getprop ro.carrier` | |
DEVICE_BRAND=`adb -s $deviceId shell getprop ro.product.brand` | |
DEVICE_MODEL=`adb -s $deviceId shell getprop ro.product.model` | |
DEVICE_SIM_STATE=`adb -s $deviceId shell getprop gsm.sim.state` | |
DEVICE_SCREEN_DENSITY=`adb -s $deviceId shell getprop ro.sf.lcd_density` | |
DEVICE_SCREEN_RESOLUTION=`adb -s $deviceId shell dumpsys window policy | grep mUnrestrictedScreen | cut -d' ' -f6` | |
echo "## device - $deviceId ###################" | |
echo "Device OS:" | |
echo " release version: $DEVICE_OS_VERSION" | |
echo " SDK: $DEVICE_OS_SDK" | |
echo "Device Build:" | |
echo " brand: $DEVICE_BRAND" | |
echo " model: $DEVICE_MODEL" | |
echo " carrier: $DEVICE_CARRIER" | |
echo " GSM SIM state: $DEVICE_SIM_STATE" | |
echo "Device Screen:" | |
echo " screen density: $DEVICE_SCREEN_DENSITY" | |
echo " screen resolution: $DEVICE_SCREEN_RESOLUTION" | |
done < actiontmpfile | |
rm actiontmpfile | |
} | |
## VERSIONNAME ## | |
getVersionName(){ # takes $FILEPATH as a parameter and gets versionName by dumping the apk badging | |
# check for $FILEPATH in local folder. If it doesn't exist, suggest running with "-i new" first | |
if [[ ! -e "$1" ]] ; then | |
echo "unable to find expected file. Did you remember to download it? Try running" | |
echo "this script again but with -i new." | |
exit 0 | |
fi | |
# check if a parameter was supplied and apply it otherwise just use the default $FILEPATH | |
if [ $# -eq 0 ] | |
then | |
echo "No argument supplied. Using default filepath: $FILEPATH" | |
test_file=$FILEPATH | |
else | |
echo "Getting versionName from $1" | |
test_file=$1 | |
fi | |
# update this logic to handle multiple APKs | |
rm -f appVersionName.txt | |
appVersionName=`aapt dump badging $test_file | grep package | awk '{print $4}' | sed s/versionName=//g | sed s/\'//g` | |
echo "App versionName = $appVersionName" | |
echo $appVersionName > appVersionName.txt | |
} | |
## INSTALL ## | |
installApp(){ | |
rm -f ./apklist.txt | |
# where the first parameter is the $FILEPATH and the second is the $deviceId. Uninstall always happens first. | |
echo "installApp called with params $1 and $2..." | |
# handle multiple APKs - this is primarily to support instrumentation tests compiled as APKs | |
if [[ "$1" = "all" ]] ; then | |
echo "TEST MESSAGE ** installApp() called with 'all'." | |
ls *.apk > apklist.txt | |
elif [[ "$1" = "$FILEPATH" ]] ; then | |
echo "TEST MESSAGE ** installApp() called with $1" | |
echo "$FILEPATH" > apklist.txt | |
fi | |
apkArray=( $( cat ./apklist.txt ) ) | |
for i in "${apkArray[@]}" | |
do | |
echo "TEST MESSAGE ** installApp() called with i: $i" | |
PACKAGE=`aapt dump badging $i | grep package | awk '{print $2}' | sed s/name=//g | sed s/\'//g` | |
echo "Uninstalling $PACKAGE from $2" | |
adb -s $2 uninstall $PACKAGE | |
versionName=`aapt dump badging $i | grep package | awk '{print $4}' | sed s/versionName=//g | sed s/\'//g` | |
echo "Installing $versionName on $2" | |
adb -s $2 install $i | |
done | |
} | |
## UPGRADE ## | |
upgradeApp(){ | |
echo "upgradeApp called with params $1 and $2..." # where the first parameter is the $FILEPATH and the second is the $deviceId. | |
# Handle multiple APKs - this is primarily to support instrumentation tests compiled as APKs | |
if [[ "$1" = "all" ]] ; then | |
ls *.apk > apklist.txt | |
elif [[ "$1" = "$FILEPATH" ]] ; then | |
ls $FILEPATH > apklist.txt | |
fi | |
apkArray=( $( cat ./apklist.txt ) ) | |
for i in "${apkArray[@]}" | |
do | |
versionName=`aapt dump badging $i | grep package | awk '{print $4}' | sed s/versionName=//g | sed s/\'//g` | |
echo "Upgrading with $versionName on $2" | |
adb -s $2 install -r $i | |
done | |
rm -f ./apklist.txt | |
} | |
## DOWNLOAD ## | |
download(){ | |
#Grab URL from config | |
FILE_URL=$APK_URL | |
CURL_RESPONSE=`curl -s -o /dev/null -I -w "%{http_code}" $FILE_URL` | |
if [[ "$CURL_RESPONSE" != "200" ]] ; then | |
echo "Bummer dude, your request got a $CURL_RESPONSE." | |
echo "Make sure $FILE_URL is correct" | |
exit 0 | |
fi | |
#for now we're just hard-coding the download target to a single file rather than multiples | |
echo "## Downloading using $FILE_URL ##" | |
curl -O $FILE_URL | |
#consider making a getVersionName() method for here and for installApp() | |
rm -f $APP_VERSION_NAME_FILE | |
appVersionName=`aapt dump badging $FILEPATH | grep package | awk '{print $4}' | sed s/versionName=//g | sed s/\'//g` | |
echo $appVersionName > $APP_VERSION_NAME_FILE | |
} | |
## CLEAR ## | |
clearApp(){ | |
echo "clearing $PACKAGE from device $2..." | |
adb -s $2 shell am force-stop $PACKAGE | |
adb -s $2 shell pm clear $PACKAGE | |
} | |
## LAUNCH ## | |
launch(){ | |
LAUNCH_ENABLED=$1 | |
if [[ "$LAUNCH_ENABLED" = "true" ]] ; then | |
echo "set launch target to $2" | |
case $2 in | |
custom_uri_example) ## UPDATE THIS CASE TO A SIMPLE NAME AND THE $LAUNCH_CONFIG TO YOUR CUSTOM LAUNCH URI | |
LAUNCH_CONFIG="shell am start -a android.intent.action.VIEW -d customURI://yourserver.com/path/to/launch/config/data -f 0x14000000" | |
echo $LAUNCH_CONFIG > $LAUNCH_CONFIG_FILE | |
;; | |
default_example) ## UPDATE THIS CASE TO A SIMPLE NAME AND THE $LAUNCH_CONFIG TO YOUR APP'S DEFAULT LAUNCH ACTIVITY | |
LAUNCH_CONFIG="shell am start -n com.example.app/com.example.app.activities.FirstActivity -f 0x14000000" | |
echo $LAUNCH_CONFIG > $LAUNCH_CONFIG_FILE | |
;; | |
*) | |
echo "Invalid launch target. Please review the usage of this script" | |
usage | |
exit 0 | |
esac | |
adb devices | grep device | grep -v attached | cut -f 1 > launchtmpfile | |
while read deviceId; | |
do | |
echo "My launch config is: $LAUNCH_CONFIG" | |
adb -s $deviceId $LAUNCH_CONFIG | |
done < launchtmpfile | |
rm launchtmpfile | |
## launch should exit the script since there's nothing more to do after launching on all devices | |
exit 0 | |
fi | |
} | |
## ACTION ## | |
installAction(){ | |
#I've separated these checks in case I want to do anything differently with upgrade paths in the future | |
if [[ "$1" = "install_new" ]] ; then | |
download | |
elif [[ "$1" = "upgrade" ]] ; then | |
download | |
else # call getVersionName directly since we're not sure whether the file has been updated manually | |
getVersionName | |
fi | |
adb devices | grep device | grep -v attached | cut -f 1 > actiontmpfile | |
while read deviceId; | |
do | |
case $1 in | |
install_new) | |
echo "installAction $1" | |
installApp $FILEPATH $deviceId | |
# echo the $FILE_URL for TextExpander's benefit | |
echo $FILE_URL > $BUILD_URL_FILE | |
;; | |
reinstall) #identical to install_new for now because download logic is handled elsewhere | |
echo "installAction $1" | |
installApp $FILEPATH $deviceId | |
# echo the $FILE_URL for TextExpander's benefit | |
echo $FILE_URL > $BUILD_URL_FILE | |
;; | |
upgrade) #for cases where you wish to upgrade the app without nuking app data | |
echo "installAction $1" | |
upgradeApp $FILEPATH $deviceId | |
# echo the $FILE_URL for TextExpander's benefit | |
echo $FILE_URL > $BUILD_URL_FILE | |
;; | |
clear_app) | |
echo "installAction $1" | |
clearApp $PACKAGE $deviceId | |
;; | |
esac | |
done < actiontmpfile | |
rm actiontmpfile | |
} | |
######################################################################## | |
########################## MAIN FUNCTIONALITY ########################## | |
# Start by gathering information about the connected devices | |
rm -f $DEVICE_INFO_FILE | |
getConnectedDeviceInfo > $DEVICE_INFO_FILE | |
# Default behavior - If no arguments are provided the script will uninstall and reinstall | |
# the app at the hardcoded $PACKAGE and $FILEPATH values without launching it afterwards | |
if (( $# < 1 )) ; then | |
installAction reinstall | |
fi | |
# Set option index to 1 | |
OPTIND=1 | |
while getopts "hci:l:" VALUE "$@" ; do | |
if [ "$VALUE" = "h" ] ; then | |
usage | |
exit 0 | |
fi | |
if [ "$VALUE" = "l" ] ; then | |
LAUNCH_ENABLED="true" | |
LAUNCH_TARGET=$OPTARG | |
fi | |
if [ "$VALUE" = "c" ] ; then | |
if [ "$ACTION" != "none" ] ; then | |
echo "-c for Clear and -i for Install are mutually exclusive. Please pick one or the other." | |
usage | |
exit 1 | |
fi | |
echo "user just wants the current install cleared" | |
ACTION="clear_app" | |
fi | |
if [ "$VALUE" = "i" ] ; then | |
if [ "$ACTION" = "clear_app" ] ; then | |
echo "-c for Clear and -i for Install are mutually exclusive. Please pick one or the other." | |
usage | |
exit 1 | |
fi | |
case $OPTARG in | |
new) | |
echo "user wants to install the newest build" | |
ACTION="install_new" | |
;; | |
saved) | |
echo "user wants to reinstall without a download" | |
ACTION="reinstall" | |
;; | |
upgrade) | |
echo "user wants to upgrade the current installation" | |
ACTION="upgrade" | |
;; | |
*) | |
echo "invalid -i parameter" | |
usage | |
exit 1 | |
;; | |
esac | |
fi | |
if [ "$VALUE" = "?" ] ; then | |
usage | |
exit 1 | |
fi | |
if [ "$VALUE" = ":" ] ; then | |
usage | |
exit 1 | |
fi | |
done | |
## call installAction after gathering inputs | |
installAction $ACTION | |
## call launch after gathering inputs and installs complete | |
launch $LAUNCH_ENABLED $LAUNCH_TARGET |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment