I have just started with PhoneGap. First impressions are overly positive, though I see one subtle detail worth improving – the create script. Documentation (Android) suggests to invoke the script from within the installation directory. It can obviously be called using the absolute path from any location, but either way I find it too much of a hassle. In this post, I want to share a simple solution I have created to save myself some typing.  

I have two objections when it comes to the create script which ships with PhoneGap:

  • having to navigate deep into the installation directory or type a lengthy absolute path
  • too many arguments

Let me explain my second point. PhoneGap documentation says:
Type in ./create <project_folder_path> <package_name> <project_name> then press “Enter”
 
I don’t know how about you, but I prefer to keep things simple. If I go and create a project called hello_world I store it under the directory of the same name. Since I like to put related projects into the same location, I don’t really need to specify the project folder path every time I am about to create a new project. 
 
That concludes my reasoning, here is the helper script I ended up with:
#! /bin/bash

#### GENERAL SETTINGS ####
phonegap_home=~/development/phonegap-2.5.0
project_root=~/development/phonegap-apps

#### INPUT VALIDATION ####
function quit {
echo "Usage:"
echo "$0 [android | ios] project_name package_name"
exit 1
}

if [ ! $# -eq 3 ]; then quit; fi

case $1 in
android|ios) ;;
*) echo "$1 is an unsupported platform"; quit
esac

project_dir=$project_root/$2

if [ -d "$project_dir" ]; then
echo "$project_dir already exists"; quit
fi

#### PHONEGAP CREATE ####
$phonegap_home/lib/$1/bin/create $project_dir $3 $2
echo "Project successfully created at $project_dir"

exit 0

I saved the script as ~/bin/phonegap_create and updated the PATH variable in ~/.bash_profile:

export PATH=${PATH}:~/bin

That’s all. Here is how I create a new Android project:

phonegap_create android hello_world org.zezutom.helloworld

Output:

$ls -l ~/development/phonegap-apps/hello_world/
total 48
-rw-r--r-- 1 tom staff 3309 31 Mar 23:39 AndroidManifest.xml
-rw-r--r-- 1 tom staff 698 31 Mar 23:39 ant.properties
drwxr-xr-x@ 3 tom staff 102 31 Mar 23:39 assets
drwxr-xr-x 2 tom staff 68 31 Mar 23:39 bin
-rw-r--r-- 1 tom staff 3923 31 Mar 23:39 build.xml
drwxr-xr-x 9 tom staff 306 31 Mar 23:39 cordova
drwxr-xr-x 3 tom staff 102 31 Mar 23:39 libs
-rw-r--r-- 1 tom staff 451 31 Mar 23:39 local.properties
-rw-r--r-- 1 tom staff 781 31 Mar 23:39 proguard-project.txt
-rw-r--r-- 1 tom staff 563 31 Mar 23:39 project.properties
drwxr-xr-x@ 10 tom staff 340 31 Mar 23:39 res
drwxr-xr-x 3 tom staff 102 31 Mar 23:39 src

Similarly for iOS I do:

$phonegap_create ios hello_world org.zezutom.helloworld

Which gives me:

$ls -l ~/development/phonegap-apps/hello_world/
total 0
drwxr-xr-x 6 tom staff 204 31 Mar 23:51 CordovaLib
drwxr-xr-x@ 7 tom staff 238 31 Mar 23:51 cordova
drwxr-xr-x@ 10 tom staff 340 31 Mar 23:51 hello_world
drwxr-xr-x@ 3 tom staff 102 31 Mar 23:51 hello_world.xcodeproj
drwxr-xr-x@ 10 tom staff 340 31 Mar 23:51 www
Categories: Tips & Tricks

Tomas Zezula

Hello! I'm a technology enthusiast with a knack for solving problems and a passion for making complex concepts accessible. My journey spans across software development, project management, and technical writing. I specialise in transforming rough sketches of ideas to fully launched products, all the while breaking down complex processes into understandable language. I believe a well-designed software development process is key to driving business growth. My focus as a leader and technical writer aims to bridge the tech-business divide, ensuring that intricate concepts are available and understandable to all. As a consultant, I'm eager to bring my versatile skills and extensive experience to help businesses navigate their software integration needs. Whether you're seeking bespoke software solutions, well-coordinated product launches, or easily digestible tech content, I'm here to make it happen. Ready to turn your vision into reality? Let's connect and explore the possibilities together.