public
Created

shell script to create Cordova xcode project using Libary version of Cordova

  • Download Gist
create_project.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
#!/bin/bash
 
# ############################################################################
#
# MIT licensed
# http://www.opensource.org/licenses/mit-license.php
#
# Script to create a new PhoneGap project from the PhoneGap Template.
# You need to install PhoneGapLib first (through the installer)
# before this script can work.
#
# Make sure you set the executable bit on this script. In Terminal.app, type:
# chmod 755 create_project.sh
#
# Usage:
# ./create_project.sh <PROJECT_NAME> <PATH_TO_PUT_NEW_PROJECT>
# <PROJECT_NAME> - the name of the project (cannot have spaces in it)
# <PATH_TO_PUT_NEW_PROJECT> - the path to put the new project folder in
#
# Written by Shazron Abdullah (2011)
#
# ############################################################################
 
set -o pipefail
 
function checkExitCode {
rc=$?
if [ $rc != 0 ] ; then
echo "Error $rc, exiting."
cleanUp
exit $rc
fi
}
 
function cleanUp {
echo 'Cleaning up...'
cd -
rm -rf $TEMP_PROJECT_DIR_PATH
}
 
CORDOVA_TEMPLATE_PATH="$HOME/Library/Application Support/Developer/Shared/Xcode/Project Templates/Cordova/Cordova-based Application/"
 
# ##############################################
# SCRIPT ARGUMENTS
# ##############################################
 
# 1st argument: name of the project
PROJECT_NAME=$1
 
# 2nd argument: path to put new project
NEW_PROJECT_PATH=$2
 
# ##############################################
# CHECKS
# ##############################################
 
if [ ! -d "$CORDOVA_TEMPLATE_PATH" ]; then
read -p "PhoneGapLib is not installed. Download the iOS PhoneGap installer from http://phonegap.com. Go there now (y/n)? "
[ "$REPLY" == "y" ] && open http://www.phonegap.com/download/
exit 1
fi
 
if [ ! -d "$NEW_PROJECT_PATH" ]; then
echo "The target folder '$NEW_PROJECT_PATH' does not exist."
exit 1
fi
 
NEW_PROJECT_PATH=`cd $NEW_PROJECT_PATH; pwd`
 
# ##############################################
# TEMPORARY WORKING DIRECTORY
# ##############################################
 
# create temporary working directory
TEMP_PROJECT_DIR_PATH=`mktemp -d -t 'cordova'`
trap "{ cd - ; rm -rf $TEMP_PROJECT_DIR_PATH; exit 255; }" SIGINT
cd $TEMP_PROJECT_DIR_PATH
 
# ##############################################
# TEMPLATE COPY, FIND/REPLACE
# ##############################################
 
# copy CORDOVA_TEMPLATE_PATH into TEMP_PROJECT_DIR_PATH
cp -r "$CORDOVA_TEMPLATE_PATH" "$TEMP_PROJECT_DIR_PATH"
 
checkExitCode
 
# replace file contents of ___PROJECTNAME___ token
find "$TEMP_PROJECT_DIR_PATH" | xargs grep '___PROJECTNAME___' -sl | xargs -L1 sed -i "" "s/___PROJECTNAME___/${PROJECT_NAME}/g"
 
checkExitCode
 
# replace file contents of ___PROJECTNAMEASIDENTIFIER___ token
find "$TEMP_PROJECT_DIR_PATH" | xargs grep '___PROJECTNAMEASIDENTIFIER___' -sl | xargs -L1 sed -i "" "s/___PROJECTNAMEASIDENTIFIER___/${PROJECT_NAME}/g"
 
checkExitCode
 
# replace filenames that have ___PROJECTNAME___ token
cd "$TEMP_PROJECT_DIR_PATH";find . -name "*___PROJECTNAME___*"| awk '{print("mv "$1" "$1)}' | sed "s/___PROJECTNAME___/${PROJECT_NAME}/2" | sh;cd -
 
checkExitCode
 
# replace filenames that have ___PROJECTNAMEASIDENTIFIER___ token
cd "$TEMP_PROJECT_DIR_PATH";find . -name "*___PROJECTNAMEASIDENTIFIER___*" | awk '{print("mv "$1" "$1)}' | sed "s/___PROJECTNAMEASIDENTIFIER___/${PROJECT_NAME}/2" | sh;cd -
 
checkExitCode
 
# copy CORDOVA_TEMPLATE_PATH into NEW_PROJECT_PATH
mkdir -p "$NEW_PROJECT_PATH/$PROJECT_NAME"
cp -r "$TEMP_PROJECT_DIR_PATH/" "$NEW_PROJECT_PATH/$PROJECT_NAME"
 
checkExitCode
 
exit 0

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.