diff --git a/bin/templates/cordova/lib/list-started-emulators b/bin/templates/cordova/lib/list-started-emulators index 0d5d5c11..2aa6e996 100755 --- a/bin/templates/cordova/lib/list-started-emulators +++ b/bin/templates/cordova/lib/list-started-emulators @@ -16,7 +16,7 @@ # specific language governing permissions and limitations # under the License. -devices=`adb devices | awk '/List of devices attached/ { while(getline > 0) { print $1;}}' | grep 'emulator'` +devices=`adb devices | awk '/List of devices attached/ { while(getline > 0) { print $1;}}' | grep 'emulator' | grep -v 'offline'` read -ra emulator_list <<< "$devices" if [[ ${#emulator_list[@]} > 0 ]] ; then for i in ${emulator_list[@]} @@ -26,7 +26,7 @@ if [[ ${#emulator_list[@]} > 0 ]] ; then done exit 0 else - echo "No started emulators found, you can start an emulator by using the command" + echo "No started emulators found (it may still be booting up), you can start an emulator by using the command" echo " 'cordova/lib/start-emulator'" exit 2 fi diff --git a/bin/templates/cordova/lib/start-emulator b/bin/templates/cordova/lib/start-emulator index 86b14bfe..10e73ce4 100755 --- a/bin/templates/cordova/lib/start-emulator +++ b/bin/templates/cordova/lib/start-emulator @@ -18,6 +18,50 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_PATH=$( cd "$( dirname "$0" )/../.." && pwd ) + +function dot { + sleep 1 + echo -n "." +} + +function wait_for_emulator { + local i="0" + echo -n "Waiting for emulator" + emulator_string=$($DIR/list-started-emulators) + while [ $? != 0 ] + do + dot + i=$[i+1] + emulator_string=$($DIR/list-started-emulators) + done + read -ra target <<< "$emulator_string" + echo "" + echo -n "Waiting for it to boot up (this can take a while)" + while [ $i -lt 300 ] + do + boot_anim=$(adb -s $target shell getprop init.svc.bootanim 2>&1) + if [[ "$boot_anim" =~ "stopped" ]] ; then + break + else + i=$[i+1] + dot + fi + done + # Device timeout: emulator has not started in time + if [ $i -eq 300 ] + then + echo "" + echo "Emulator timeout!" + exit 69 + else + echo "" + echo "Connected!" + fi + # Unlock the device + adb -s $target shell input keyevent 82 + exit 0 +} + emulator_images=$("$DIR/list-emulator-images") if [ $? != 0 ]; then echo "No emulators found, if you would like to create an emulator follow the instructions" @@ -43,3 +87,5 @@ else #xterm -e emulator -avd ${emulator_list[0]} & emulator -avd ${emulator_list[0]} 1> /dev/null 2>&1 & fi + +wait_for_emulator diff --git a/bin/templates/cordova/run b/bin/templates/cordova/run index d871c09d..9641ab2b 100755 --- a/bin/templates/cordova/run +++ b/bin/templates/cordova/run @@ -19,66 +19,40 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" PROJECT_PATH=$( cd "$DIR/.." && pwd ) -function wait_for_emulator { -# TODO: this doesnt seem to work - emulator_string=$("$DIR/lib/list-started-emulators") - old_started=($emulator_string) - local new_started - local new_emulator_name - local i="0" - echo -n "Waiting for emulator..." - while [ $i -lt 300 ] - do - emulator_string=$("$DIR/lib/list-started-emulators") - new_started=($emulator_string) - if [[ ${#new_started[@]} > ${#old_started[@]} && -z "$new_emulator_name" ]] ; then - # get the name of the started emulator - local count="0" - if [[ ${#old_started[@]} == 0 ]] ; then - new_emulator_name=${new_started[$count]} - else - for count in {0...${#old_started[@]}} - do - if [[ ! ${new_started[$count]} == ${old_started[$count]} ]] ; then - new_emulator_name=${new_started[$count]} - fi - done - if [[ -z "$new_emulator_name" ]] ; then - count=$[count+1] - new_emulator_name=${new_started[$count]} - fi - fi - elif [[ "$new_emulator_name" ]] ; then - boot_anim=`adb -s $new_emulator_name shell getprop init.svc.bootanim` - if [[ $boot_anim =~ "stopped" ]] ; then - break - else - sleep 1 - i=$[i+1] - echo -n "." - fi - else - sleep 1 - i=$[i+1] - echo -n "." - fi - done - # Device timeout: emulator has not started in time - if [ $i -eq 300 ] - then - echo "emulator timeout!" - exit 69 +function run_on_device_or_emulator { + devices=`$DIR/lib/list-devices` + if [ $? = 0 ]; then + $DIR/lib/install-device else - echo "connected!" + run_on_emulator fi } +function run_on_emulator { + emulators=`$DIR/lib/list-started-emulators` + if [ $? = 0 ] ; then + $DIR/lib/install-emulator + else + images=`$DIR/lib/list-emulator-images` + if [ $? = 0 ] ; then + $DIR/lib/start-emulator + $DIR/lib/install-emulator + else + echo "No devices/emulators started nor images available to start. How are we supposed to do this, then?" + exit 2 + fi + fi +} + + + if [[ "$#" -eq 2 ]] ; then + # TODO: the order of arguments here may be reversed from the assumption below $DIR/build $2 if [[ $1 == "--device" ]] ; then $DIR/lib/install-device elif [[ $1 == "--emulator" ]] ; then - $DIR/lib/install-emulator + run_on_emulator elif [[ $1 =~ "--target=" ]]; then $DIR/lib/install-device $1 else @@ -87,12 +61,13 @@ if [[ "$#" -eq 2 ]] ; then elif [[ "$#" -eq 1 ]] ; then if [[ $1 == "--debug" || $1 == "--release" || $1 == "--nobuild" ]] ; then $DIR/build $1 + run_on_device_or_emulator elif [[ $1 == "--device" ]] ; then $DIR/build $DIR/lib/install-device elif [[ $1 == "--emulator" ]] ; then - $DIR/build - $DIR/lib/install-emulator + #$DIR/build + run_on_emulator elif [[ $1 =~ "--target=" ]]; then $DIR/build $DIR/lib/install-device $1 @@ -101,24 +76,6 @@ elif [[ "$#" -eq 1 ]] ; then fi else echo "Warning : [ --device | --emulate | --target= ] not specified, using defaults." - #$DIR/build - devices=$("$DIR/lib/list-devices") - if [ $? = 0 ]; then - $DIR/lib/install-device - else - emulators=$("$DIR/lib/list-started-emulators") - if [ $? = 0 ] ; then - $DIR/lib/install-emulator - else - images=$("$DIR/lib/list-emulator-images") - if [ $? = 0 ] ; then - $DIR/lib/start-emulator - wait_for_emulator - $DIR/lib/install-emulator - else - echo "No Android devices attached, nor emulator images available to start. How are we supposed to do this, then?" - exit 2 - fi - fi - fi + $DIR/build + run_on_device_or_emulator fi