create 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #! /bin/bash
  2. # Licensed to the Apache Software Foundation (ASF) under one
  3. # or more contributor license agreements. See the NOTICE file
  4. # distributed with this work for additional information
  5. # regarding copyright ownership. The ASF licenses this file
  6. # to you under the Apache License, Version 2.0 (the
  7. # "License"); you may not use this file except in compliance
  8. # with the License. You may obtain a copy of the License at
  9. #
  10. # http://www.apache.org/licenses/LICENSE-2.0
  11. #
  12. # Unless required by applicable law or agreed to in writing,
  13. # software distributed under the License is distributed on an
  14. # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. # KIND, either express or implied. See the License for the
  16. # specific language governing permissions and limitations
  17. # under the License.
  18. #
  19. # create a cordova/android project
  20. #
  21. # USAGE
  22. # ./create [path package activity]
  23. #
  24. set -e
  25. if [ -z "$1" ] || [ "$1" == "-h" ]
  26. then
  27. echo "Usage: $0 <path_to_new_project> <package_name> <project_name>"
  28. echo "Make sure the Android SDK tools folder is in your PATH!"
  29. echo " <path_to_new_project>: Path to your new Cordova iOS project"
  30. echo " <package_name>: Package name, following reverse-domain style convention"
  31. echo " <project_name>: Project name"
  32. exit 0
  33. fi
  34. BUILD_PATH="$( cd "$( dirname "$0" )/.." && pwd )"
  35. VERSION=$(cat "$BUILD_PATH"/VERSION)
  36. PROJECT_PATH="${1:-'./example'}"
  37. PACKAGE=${2:-"org.apache.cordova.example"}
  38. ACTIVITY=${3:-"cordovaExample"}
  39. # clobber any existing example
  40. if [ -d "$PROJECT_PATH" ]
  41. then
  42. echo "Project already exists! Delete and recreate"
  43. exit 1
  44. fi
  45. # cleanup after exit and/or on error
  46. function on_exit {
  47. # [ -f "$BUILD_PATH"/framework/libs/commons-codec-1.6.jar ] && rm "$BUILD_PATH"/framework/libs/commons-codec-1.6.jar
  48. # [ -d "$BUILD_PATH"/framework/libs ] && rmdir "$BUILD_PATH"/framework/libs
  49. if [ -f "$BUILD_PATH"/framework/assets/www/cordova-$VERSION.js ]
  50. then
  51. rm "$BUILD_PATH"/framework/assets/www/cordova-$VERSION.js
  52. fi
  53. if [ -f "$BUILD_PATH"/framework/cordova-$VERSION.jar ]
  54. then
  55. rm "$BUILD_PATH"/framework/cordova-$VERSION.jar
  56. fi
  57. }
  58. function createAppInfoJar {
  59. pushd "$BUILD_PATH"/bin/templates/cordova/ApplicationInfo > /dev/null
  60. javac ApplicationInfo.java
  61. jar -cfe ../appinfo.jar ApplicationInfo ApplicationInfo.class
  62. popd > /dev/null
  63. }
  64. function on_error {
  65. echo "An unexpected error occurred: $previous_command exited with $?"
  66. echo "Deleting project..."
  67. [ -d "$PROJECT_PATH" ] && rm -rf "$PROJECT_PATH"
  68. exit 1
  69. }
  70. function replace {
  71. local pattern=$1
  72. local filename=$2
  73. # Mac OS X requires -i argument
  74. if [[ "$OSTYPE" =~ "darwin" ]]
  75. then
  76. /usr/bin/sed -i '' -e $pattern "$filename"
  77. elif [[ "$OSTYPE" =~ "linux" ]]
  78. then
  79. /bin/sed -i -e $pattern "$filename"
  80. fi
  81. }
  82. # we do not want the script to silently fail
  83. trap 'previous_command=$this_command; this_command=$BASH_COMMAND' DEBUG
  84. trap on_error ERR
  85. trap on_exit EXIT
  86. ANDROID_BIN="${ANDROID_BIN:=$( which android )}"
  87. PACKAGE_AS_PATH=$(echo $PACKAGE | sed 's/\./\//g')
  88. ACTIVITY_PATH="$PROJECT_PATH"/src/$PACKAGE_AS_PATH/$ACTIVITY.java
  89. MANIFEST_PATH="$PROJECT_PATH"/AndroidManifest.xml
  90. TARGET=$("$ANDROID_BIN" list targets | grep id: | tail -1 | cut -f 2 -d ' ' )
  91. API_LEVEL=$("$ANDROID_BIN" list target | grep "API level:" | tail -n 1 | cut -f 2 -d ':' | tr -d ' ')
  92. # check that build targets exist
  93. if [ -z "$TARGET" ] || [ -z "$API_LEVEL" ]
  94. then
  95. echo "No Android Targets are installed. Please install at least one via the android SDK"
  96. exit 1
  97. fi
  98. # if this a distribution release no need to build a jar
  99. if [ ! -e "$BUILD_PATH"/cordova-$VERSION.jar ] && [ -d "$BUILD_PATH"/framework ]
  100. then
  101. # update the cordova-android framework for the desired target
  102. "$ANDROID_BIN" update project --target $TARGET --path "$BUILD_PATH"/framework &> /dev/null
  103. if [ ! -e "$BUILD_PATH"/framework/libs/commons-codec-1.7.jar ]; then
  104. # Use curl to get the jar (TODO: Support Apache Mirrors)
  105. curl -OL http://archive.apache.org/dist/commons/codec/binaries/commons-codec-1.7-bin.zip &> /dev/null
  106. unzip commons-codec-1.7-bin.zip &> /dev/null
  107. mkdir -p "$BUILD_PATH"/framework/libs
  108. cp commons-codec-1.7/commons-codec-1.7.jar "$BUILD_PATH"/framework/libs
  109. # cleanup yo
  110. rm commons-codec-1.7-bin.zip && rm -rf commons-codec-1.7
  111. fi
  112. # compile cordova.js and cordova.jar
  113. pushd "$BUILD_PATH"/framework > /dev/null
  114. ant jar > /dev/null
  115. popd > /dev/null
  116. fi
  117. # create new android project
  118. "$ANDROID_BIN" create project --target $TARGET --path "$PROJECT_PATH" --package $PACKAGE --activity $ACTIVITY &> /dev/null
  119. # copy project template
  120. cp -r "$BUILD_PATH"/bin/templates/project/assets "$PROJECT_PATH"
  121. cp -r "$BUILD_PATH"/bin/templates/project/res "$PROJECT_PATH"
  122. # copy cordova.js, cordova.jar and res/xml
  123. if [ -d "$BUILD_PATH"/framework ]
  124. then
  125. cp -r "$BUILD_PATH"/framework/res/xml "$PROJECT_PATH"/res
  126. cp "$BUILD_PATH"/framework/assets/www/cordova-$VERSION.js "$PROJECT_PATH"/assets/www/cordova-$VERSION.js
  127. cp "$BUILD_PATH"/framework/cordova-$VERSION.jar "$PROJECT_PATH"/libs/cordova-$VERSION.jar
  128. else
  129. cp -r "$BUILD_PATH"/xml "$PROJECT_PATH"/res/xml
  130. cp "$BUILD_PATH"/cordova-$VERSION.js "$PROJECT_PATH"/assets/www/cordova-$VERSION.js
  131. cp "$BUILD_PATH"/cordova-$VERSION.jar "$PROJECT_PATH"/libs/cordova-$VERSION.jar
  132. fi
  133. # interpolate the activity name and package
  134. cp "$BUILD_PATH"/bin/templates/project/Activity.java "$ACTIVITY_PATH"
  135. replace "s/__ACTIVITY__/${ACTIVITY}/g" "$ACTIVITY_PATH"
  136. replace "s/__ID__/${PACKAGE}/g" "$ACTIVITY_PATH"
  137. cp "$BUILD_PATH"/bin/templates/project/AndroidManifest.xml "$MANIFEST_PATH"
  138. replace "s/__ACTIVITY__/${ACTIVITY}/g" "$MANIFEST_PATH"
  139. replace "s/__PACKAGE__/${PACKAGE}/g" "$MANIFEST_PATH"
  140. replace "s/__APILEVEL__/${API_LEVEL}/g" "$MANIFEST_PATH"
  141. # creating cordova folder and copying run/build/log/launch scripts
  142. mkdir "$PROJECT_PATH"/cordova
  143. createAppInfoJar
  144. cp "$BUILD_PATH"/bin/templates/cordova/appinfo.jar "$PROJECT_PATH"/cordova/appinfo.jar
  145. cp "$BUILD_PATH"/bin/templates/cordova/cordova "$PROJECT_PATH"/cordova/cordova
  146. cp "$BUILD_PATH"/bin/templates/cordova/build "$PROJECT_PATH"/cordova/build
  147. cp "$BUILD_PATH"/bin/templates/cordova/release "$PROJECT_PATH"/cordova/release
  148. cp "$BUILD_PATH"/bin/templates/cordova/clean "$PROJECT_PATH"/cordova/clean
  149. cp "$BUILD_PATH"/bin/templates/cordova/log "$PROJECT_PATH"/cordova/log
  150. cp "$BUILD_PATH"/bin/templates/cordova/run "$PROJECT_PATH"/cordova/run