Created
April 14, 2011 06:48
-
-
Save lamberta/919020 to your computer and use it in GitHub Desktop.
Command line interface for creating and building Titanium Mobile Android projects.
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 bash | |
## Command line interface for creating and building Titanium Mobile Android projects. | |
TI_SRC="$HOME/local/src/titanium_mobile" | |
TI_SDK="$TI_SRC/dist/mobilesdk/linux/1.7.0" | |
TI_VERSION_TAG="1_7_0_preview" | |
ANDROID_HOME=${ANDROID_HOME:-"$HOME/local/lib/android-sdk"} | |
JAVA_HOME=${JAVA_HOME:-"/usr/lib/jvm/java-6-sun"} #sun-java6-sdk | |
PROJECT_HOME="$HOME/projects/ti" | |
ID_PREFIX="org.lamberta" | |
AVD_ID="titanium_5_HVGA" | |
SD_CARD="$HOME/.titanium/android2.sdcard" | |
declare APP_NAME | |
function print_help { | |
echo "Usage: $(basename $0) [options]" | |
echo " -h Show this usage guide." | |
echo " -c=name Create new project." | |
echo " -b=name Build project." | |
echo " -i=name Install project application on Android emulator." | |
echo " -e Run Android emulator." | |
echo " -U Update and build Titanium from source. (tag: $TI_VERSION_TAG)" | |
} | |
function build_src { | |
cd "$TI_SRC" | |
if [ ! -x $(which scons) ]; then | |
echo "Error: Titanium requires 'scons' to build, aborting." | |
exit 1 | |
fi | |
#cleanup | |
scons android_sdk="$ANDROID_HOME" clean | |
if [ -d "$TI_SRC/dist" ]; then rm -rf "$TI_SRC/dist"; fi | |
#build | |
JAVA_HOME="$JAVA_HOME" scons android_sdk="$ANDROID_HOME" | |
} | |
function update_and_build { | |
cd "$TI_SRC" | |
#update from repo: git://github.com/appcelerator/titanium_mobile.git | |
if [[ -d "$TI_SRC/.git" && -x $(which git) ]]; then | |
git pull | |
git checkout "$TI_VERSION_TAG" | |
build_src | |
git checkout master | |
else | |
echo "Unable to update git repo, building anyway..." | |
build_src | |
fi | |
#unpack | |
cd "$TI_SRC/dist" | |
unzip -q -o mobilesdk*.zip | |
cd ./mobilesdk/*/* | |
echo "Unpacked mobile-sdk to $(pwd)" | |
} | |
function get_app_property { | |
local prop="$1" | |
local props_file="$PROJECT_HOME/$APP_NAME/tiapp.xml" | |
if [ ! -e "$props_file" ]; then | |
echo "Error: Unable to find app properties file: $props_file, aborting." | |
exit 1 | |
fi | |
local val=$(cat "$props_file" | grep "<$prop>" | cut -f2 -d '>' | cut -f1 -d '<') | |
if [ -z "$val" ]; then | |
echo "Error: Unable to get value of property '$prop' from $props_file, aborting." | |
exit 1 | |
fi | |
echo "$val" | |
} | |
function set_app_property { | |
local prop="$1" | |
local new_val="$2" | |
local props_file="$PROJECT_HOME/$APP_NAME/tiapp.xml" | |
local old_val=$(get_app_property "$prop") | |
local regexp="s/<$prop>$old_val<\/$prop>/<$prop>$new_val<\/$prop>/g" | |
#edit file in-place | |
sed -i -e "$regexp" "$props_file" | |
} | |
function set_app_defaults { | |
#set to Titanium Developer defaults, though not needed | |
set_app_property "publisher" "$(whoami)" | |
set_app_property "url" "lamberta.org" | |
set_app_property "description" "No description provided" | |
set_app_property "copyright" "2011 by $(whoami)" | |
} | |
#titanium.py doesn't create this automatically | |
function create_manifest { | |
local manifest_file="$PROJECT_HOME/$APP_NAME/manifest" | |
local app_guid=$(cat "$PROJECT_HOME/$APP_NAME/tiapp.xml" | \ | |
grep "<guid>" | cut -f2 -d '>' | cut -f1 -d '<') | |
echo "#appname: $(get_app_property name)" > "$manifest_file" | |
echo "#publisher: $(get_app_property publisher)" >> "$manifest_file" | |
echo "#url: $(get_app_property url)" >> "$manifest_file" | |
echo "#image: $(get_app_property icon)" >> "$manifest_file" | |
echo "#appid: $ID_PREFIX.$APP_NAME" >> "$manifest_file" | |
echo "#desc: $(get_app_property description)" >> "$manifest_file" | |
echo "#type: mobile" >> "$manifest_file" | |
echo "#guid: $(get_app_property guid)" >> "$manifest_file" | |
} | |
function create_project { | |
local app_id="$ID_PREFIX.$APP_NAME" | |
if [ -d "$PROJECT_HOME/$APP_NAME" ]; then | |
echo "Error: Directory $PROJECT_HOME/$APP_NAME already exists, aborting." | |
exit 1 | |
fi | |
"$TI_SDK/titanium.py" create --type="project" --name="$APP_NAME" --id="$app_id" \ | |
--dir="$PROJECT_HOME" --platform="android" --android="$ANDROID_HOME" | |
set_app_defaults | |
create_manifest | |
echo "New Titanium project located in: $PROJECT_HOME/$APP_NAME" | |
} | |
function run_android_emulator { | |
"$ANDROID_HOME/tools/emulator" -port 5560 -logcat "*:d" -no-boot-anim \ | |
-avd "$AVD_ID" -sdcard "$SD_CARD" -partition-size 128 | |
} | |
function build_android_project { | |
if [ ! -d "$PROJECT_HOME/$APP_NAME" ]; then | |
echo "Error: Unable to find project at $PROJECT_HOME/$APP_NAME, aborting." | |
exit 1 | |
fi | |
#builder.py <command> <project_name> <sdk_dir> <project_dir> <app_id> | |
"$TI_SDK/android/builder.py" 'build' "$APP_NAME" "$ANDROID_HOME" \ | |
"$PROJECT_HOME/$APP_NAME" "$ID_PREFIX.$APP_NAME" | |
} | |
function install_android_app_on_emulator { | |
local app_file="$PROJECT_HOME/$APP_NAME/build/android/bin/app.apk" | |
if [ ! -e "$app_file" ]; then | |
echo "Error: Application file does not exist: $app_file, aborting." | |
exit 1 | |
fi | |
"$ANDROID_HOME/platform-tools/adb" -e install -r "$app_file" | |
} | |
if [ "$#" -eq 0 ]; then | |
print_help; | |
exit 0 | |
fi | |
#parse args | |
while getopts "c:i:b:eUh" option; do | |
case $option in | |
c) APP_NAME="$OPTARG"; create_project; exit 0;; | |
i) APP_NAME="$OPTARG"; install_android_app_on_emulator; exit 0;; | |
b) APP_NAME="$OPTARG"; build_android_project; exit 0;; | |
e) run_android_emulator; exit 0;; | |
U) update_and_build; exit 0;; | |
h) print_help;; | |
\?) print_help;; | |
esac | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment