create.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  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. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing,
  11. software distributed under the License is distributed on an
  12. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  13. KIND, either express or implied. See the License for the
  14. specific language governing permissions and limitations
  15. under the License.
  16. */
  17. /*
  18. * create a cordova/android project
  19. *
  20. * USAGE
  21. * ./create [path package activity]
  22. */
  23. var fso = WScript.CreateObject('Scripting.FileSystemObject');
  24. function read(filename) {
  25. var fso=WScript.CreateObject("Scripting.FileSystemObject");
  26. var f=fso.OpenTextFile(filename, 1);
  27. var s=f.ReadAll();
  28. f.Close();
  29. return s;
  30. }
  31. function checkTargets(targets) {
  32. if(!targets) {
  33. WScript.Echo("You do not have any android targets setup. Please create at least one target with the `android` command");
  34. WScript.Quit(69);
  35. }
  36. }
  37. function setTarget() {
  38. var targets = shell.Exec('android.bat list targets').StdOut.ReadAll().match(/id:\s\d+/g);
  39. checkTargets(targets);
  40. return targets[targets.length - 1].replace(/id: /, ""); // TODO: give users the option to set their target
  41. }
  42. function setApiLevel() {
  43. var targets = shell.Exec('android.bat list targets').StdOut.ReadAll().match(/API level:\s\d+/g);
  44. checkTargets(targets);
  45. return targets[targets.length - 1].replace(/API level: /, "");
  46. }
  47. function write(filename, contents) {
  48. var fso=WScript.CreateObject("Scripting.FileSystemObject");
  49. var f=fso.OpenTextFile(filename, 2, true);
  50. f.Write(contents);
  51. f.Close();
  52. }
  53. function replaceInFile(filename, regexp, replacement) {
  54. write(filename, read(filename).replace(regexp, replacement));
  55. }
  56. function exec(command) {
  57. var oShell=shell.Exec(command);
  58. while (oShell.Status == 0) {
  59. if(!oShell.StdOut.AtEndOfStream) {
  60. var line = oShell.StdOut.ReadLine();
  61. // XXX: Change to verbose mode
  62. // WScript.StdOut.WriteLine(line);
  63. }
  64. WScript.sleep(100);
  65. }
  66. }
  67. function createAppInfoJar() {
  68. if(!fso.FileExists(ROOT+"\\bin\\templates\\cordova\\appinfo.jar")) {
  69. WScript.Echo("Creating appinfo.jar...");
  70. var cur = shell.CurrentDirectory;
  71. shell.CurrentDirectory = ROOT+"\\bin\\templates\\cordova\\ApplicationInfo";
  72. exec("javac ApplicationInfo.java");
  73. exec("jar -cfe ..\\appinfo.jar ApplicationInfo ApplicationInfo.class");
  74. shell.CurrentDirectory = cur;
  75. }
  76. }
  77. function cleanup() {
  78. // Cleanup
  79. // if(fso.FileExists(ROOT + '\\framework\\libs\\commons-codec-1.6.jar')) {
  80. // fso.DeleteFile(ROOT + '\\framework\\libs\\commons-codec-1.6.jar');
  81. // fso.DeleteFolder(ROOT + '\\framework\\libs', true);
  82. // }
  83. if(fso.FileExists(ROOT + '\\framework\\cordova-'+VERSION+'.jar')) {
  84. fso.DeleteFile(ROOT + '\\framework\\cordova-'+VERSION+'.jar');
  85. }
  86. if(fso.FileExists(ROOT + '\\framework\\assets\\www\\cordova-'+VERSION+'.js')) {
  87. fso.DeleteFile(ROOT + '\\framework\\assets\\www\\cordova-'+VERSION+'.js');
  88. }
  89. }
  90. function downloadCommonsCodec() {
  91. if (!fso.FileExists(ROOT + '\\framework\\libs\\commons-codec-1.7.jar')) {
  92. // We need the .jar
  93. var url = 'http://archive.apache.org/dist/commons/codec/binaries/commons-codec-1.7-bin.zip';
  94. var libsPath = ROOT + '\\framework\\libs';
  95. var savePath = libsPath + '\\commons-codec-1.7-bin.zip';
  96. if (!fso.FileExists(savePath)) {
  97. if(!fso.FolderExists(ROOT + '\\framework\\libs')) {
  98. fso.CreateFolder(libsPath);
  99. }
  100. // We need the zip to get the jar
  101. var xhr = WScript.CreateObject('MSXML2.XMLHTTP');
  102. xhr.open('GET', url, false);
  103. xhr.send();
  104. if (xhr.status == 200) {
  105. var stream = WScript.CreateObject('ADODB.Stream');
  106. stream.Open();
  107. stream.Type = 1;
  108. stream.Write(xhr.ResponseBody);
  109. stream.Position = 0;
  110. stream.SaveToFile(savePath);
  111. stream.Close();
  112. } else {
  113. WScript.Echo('Could not retrieve the commons-codec. Please download it yourself and put into the framework/libs directory. This process may fail now. Sorry.');
  114. }
  115. }
  116. var app = WScript.CreateObject('Shell.Application');
  117. var source = app.NameSpace(savePath).Items();
  118. var target = app.NameSpace(ROOT + '\\framework\\libs');
  119. target.CopyHere(source, 256);
  120. // Move the jar into libs
  121. fso.MoveFile(ROOT + '\\framework\\libs\\commons-codec-1.7\\commons-codec-1.7.jar', ROOT + '\\framework\\libs\\commons-codec-1.7.jar');
  122. // Clean up
  123. fso.DeleteFile(ROOT + '\\framework\\libs\\commons-codec-1.7-bin.zip');
  124. fso.DeleteFolder(ROOT + '\\framework\\libs\\commons-codec-1.7', true);
  125. }
  126. }
  127. var args = WScript.Arguments, PROJECT_PATH="example",
  128. PACKAGE="org.apache.cordova.example", ACTIVITY="cordovaExample",
  129. shell=WScript.CreateObject("WScript.Shell");
  130. // working dir
  131. var ROOT = WScript.ScriptFullName.split('\\bin\\create.js').join('');
  132. if (args.Count() == 3) {
  133. PROJECT_PATH=args(0);
  134. PACKAGE=args(1);
  135. ACTIVITY=args(2);
  136. }
  137. if(fso.FolderExists(PROJECT_PATH)) {
  138. WScript.Echo("Project already exists!");
  139. WScript.Quit(1);
  140. }
  141. var PACKAGE_AS_PATH=PACKAGE.replace(/\./g, '\\');
  142. var ACTIVITY_PATH=PROJECT_PATH+'\\src\\'+PACKAGE_AS_PATH+'\\'+ACTIVITY+'.java';
  143. var MANIFEST_PATH=PROJECT_PATH+'\\AndroidManifest.xml';
  144. var TARGET=setTarget();
  145. var API_LEVEL=setApiLevel();
  146. var VERSION=read(ROOT+'\\VERSION').replace(/\r\n/,'').replace(/\n/,'');
  147. // create the project
  148. WScript.Echo("Creating new android project...");
  149. exec('android.bat create project --target '+TARGET+' --path '+PROJECT_PATH+' --package '+PACKAGE+' --activity '+ACTIVITY);
  150. // build from source. distro should have these files
  151. if (!fso.FileExists(ROOT+'\\cordova-'+VERSION+'.jar') &&
  152. !fso.FileExists(ROOT+'\\cordova-'+VERSION+'.js')) {
  153. WScript.Echo("Building jar and js files...");
  154. // update the cordova framework project to a target that exists on this machine
  155. exec('android.bat update project --target '+TARGET+' --path '+ROOT+'\\framework');
  156. // pull down commons codec if necessary
  157. downloadCommonsCodec();
  158. exec('ant.bat -f \"'+ ROOT +'\\framework\\build.xml\" jar');
  159. }
  160. // copy in the project template
  161. WScript.Echo("Copying template files...");
  162. exec('%comspec% /c xcopy "'+ ROOT + '"\\bin\\templates\\project\\res '+PROJECT_PATH+'\\res\\ /E /Y');
  163. exec('%comspec% /c xcopy "'+ ROOT + '"\\bin\\templates\\project\\assets '+PROJECT_PATH+'\\assets\\ /E /Y');
  164. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\project\\AndroidManifest.xml ' + PROJECT_PATH + '\\AndroidManifest.xml /Y');
  165. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\project\\Activity.java '+ ACTIVITY_PATH +' /Y');
  166. // check if we have the source or the distro files
  167. WScript.Echo("Copying js, jar & config.xml files...");
  168. if(fso.FolderExists(ROOT + '\\framework')) {
  169. exec('%comspec% /c copy "'+ROOT+'"\\framework\\assets\\www\\cordova-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\cordova-'+VERSION+'.js /Y');
  170. exec('%comspec% /c copy "'+ROOT+'"\\framework\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar /Y');
  171. fso.CreateFolder(PROJECT_PATH + '\\res\\xml');
  172. exec('%comspec% /c copy "'+ROOT+'"\\framework\\res\\xml\\config.xml ' + PROJECT_PATH + '\\res\\xml\\config.xml /Y');
  173. } else {
  174. // copy in cordova.js
  175. exec('%comspec% /c copy "'+ROOT+'"\\cordova-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\cordova-'+VERSION+'.js /Y');
  176. // copy in cordova.jar
  177. exec('%comspec% /c copy "'+ROOT+'"\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar /Y');
  178. // copy in xml
  179. fso.CreateFolder(PROJECT_PATH + '\\res\\xml');
  180. exec('%comspec% /c copy "'+ROOT+'"\\xml\\config.xml ' + PROJECT_PATH + '\\res\\xml\\config.xml /Y');
  181. }
  182. // copy cordova scripts
  183. fso.CreateFolder(PROJECT_PATH + '\\cordova');
  184. createAppInfoJar();
  185. WScript.Echo("Copying cordova command tools...");
  186. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\appinfo.jar ' + PROJECT_PATH + '\\cordova\\appinfo.jar /Y');
  187. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\cordova.js ' + PROJECT_PATH + '\\cordova\\cordova.js /Y');
  188. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\cordova.bat ' + PROJECT_PATH + '\\cordova\\cordova.bat /Y');
  189. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\clean.bat ' + PROJECT_PATH + '\\cordova\\clean.bat /Y');
  190. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\build.bat ' + PROJECT_PATH + '\\cordova\\build.bat /Y');
  191. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\log.bat ' + PROJECT_PATH + '\\cordova\\log.bat /Y');
  192. exec('%comspec% /c copy "'+ROOT+'"\\bin\\templates\\cordova\\run.bat ' + PROJECT_PATH + '\\cordova\\run.bat /Y');
  193. // interpolate the activity name and package
  194. WScript.Echo("Updating AndroidManifest.xml and Main Activity...");
  195. replaceInFile(ACTIVITY_PATH, /__ACTIVITY__/, ACTIVITY);
  196. replaceInFile(ACTIVITY_PATH, /__ID__/, PACKAGE);
  197. replaceInFile(MANIFEST_PATH, /__ACTIVITY__/, ACTIVITY);
  198. replaceInFile(MANIFEST_PATH, /__PACKAGE__/, PACKAGE);
  199. replaceInFile(MANIFEST_PATH, /__APILEVEL__/, API_LEVEL);
  200. cleanup();