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
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:
1: #! /bin/bash
2:
3: #### GENERAL SETTINGS ####
4: phonegap_home=~/development/phonegap-2.5.0
5: project_root=~/development/phonegap-apps
6:
7: #### INPUT VALIDATION ####
8: function quit {
9: echo "Usage:"
10: echo "$0 [android | ios] project_name package_name"
11: exit 1
12: }
13:
14: if [ ! $# -eq 3 ]; then quit; fi
15:
16: case $1 in
17: android|ios) ;;
18: *) echo "$1 is an unsupported platform"; quit
19: esac
20:
21: project_dir=$project_root/$2
22:
23: if [ -d "$project_dir" ]; then
24: echo "$project_dir already exists"; quit
25: fi
26:
27: #### PHONEGAP CREATE ####
28: $phonegap_home/lib/$1/bin/create $project_dir $3 $2
29: echo "Project successfully created at $project_dir"
30:
31: 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