From 7eb3e5d13955725f44f72cb816c2fc2b2f376e18 Mon Sep 17 00:00:00 2001 From: filmaj Date: Wed, 16 May 2012 19:38:11 -0700 Subject: [PATCH] [CB-659] create script should work on android --- bin/create.bat | 2 +- bin/create.js | 61 ++++++++++++++------ bin/templates/project/cordova/create.bat | 2 + bin/templates/project/cordova/create.js | 70 +++++++++++++++++++++++ bin/templates/project/cordova/debug.bat | 0 bin/templates/project/cordova/emulate.bat | 0 bin/templates/project/cordova/log.bat | 0 7 files changed, 117 insertions(+), 18 deletions(-) create mode 100644 bin/templates/project/cordova/create.bat create mode 100644 bin/templates/project/cordova/create.js create mode 100644 bin/templates/project/cordova/debug.bat create mode 100644 bin/templates/project/cordova/emulate.bat create mode 100644 bin/templates/project/cordova/log.bat diff --git a/bin/create.bat b/bin/create.bat index b9c9392b..7182d21e 100644 --- a/bin/create.bat +++ b/bin/create.bat @@ -1 +1 @@ -cscript create.js \ No newline at end of file +cscript bin\create.js %* \ No newline at end of file diff --git a/bin/create.js b/bin/create.js index 24e70325..96e61eca 100644 --- a/bin/create.js +++ b/bin/create.js @@ -25,8 +25,9 @@ */ function read(filename) { + WScript.Echo('Reading in ' + filename); var fso=WScript.CreateObject("Scripting.FileSystemObject"); - var f=fso.OpenTextFile(filename, 1, true); + var f=fso.OpenTextFile(filename, 1); var s=f.ReadAll(); f.Close(); return s; @@ -40,8 +41,24 @@ function write(filename, contents) { function replaceInFile(filename, regexp, replacement) { write(filename, read(filename).replace(regexp, replacement)); } -function exec(s) { +function exec(s, output) { + WScript.Echo('Executing ' + s); var o=shell.Exec(s); + while (o.Status == 0) { + WScript.Sleep(100); + } + WScript.Echo("Command exited with code " + o.Status); +} + +function fork(s) { + WScript.Echo('Executing ' + s); + var o=shell.Exec(s); + while (o.Status != 1) { + WScript.Sleep(100); + } + WScript.Echo(o.StdOut.ReadAll()); + WScript.Echo(o.StdErr.ReadAll()); + WScript.Echo("Command exited with code " + o.Status); } var args = WScript.Arguments, PROJECT_PATH="example", @@ -61,6 +78,14 @@ var MANIFEST_PATH=PROJECT_PATH+'\\AndroidManifest.xml'; var TARGET=shell.Exec('android.bat list targets').StdOut.ReadAll().match(/id:\s([0-9]).*/)[1]; var VERSION=read('VERSION').replace(/\r\n/,'').replace(/\n/,''); +WScript.Echo("Project path: " + PROJECT_PATH); +WScript.Echo("Package: " + PACKAGE); +WScript.Echo("Activity: " + ACTIVITY); +WScript.Echo("Package as path: " + PACKAGE_AS_PATH); +WScript.Echo("Activity path: " + ACTIVITY_PATH); +WScript.Echo("Manifest path: " + MANIFEST_PATH); +WScript.Echo("Cordova version: " + VERSION); + // clobber any existing example /* @@ -84,24 +109,26 @@ exec('ant.bat -f framework\\build.xml jar'); // copy in the project template exec('cmd /c xcopy bin\\templates\\project '+PROJECT_PATH+' /S /Y'); +// copy example www assets +exec('cmd /c xcopy ' + PROJECT_PATH + '\\cordova\\assets ' + PROJECT_PATH + ' /S /Y'); + // copy in cordova.js -exec('cmd /c copy framework\\assets\\www\\cordova-'+VERSION+'.js '+PROJECT_PATH+'\\assets\\www\\cordova-'+VERSION+'.js /Y'); +exec('cmd /c copy framework\\assets\\js\\cordova.android.js '+PROJECT_PATH+'\\.cordova\\android\\cordova-'+VERSION+'.js /Y'); // copy in cordova.jar -exec('cmd /c copy framework\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar /Y'); +exec('cmd /c copy framework\\cordova-'+VERSION+'.jar '+PROJECT_PATH+'\\.cordova\\android\\cordova-'+VERSION+'.jar /Y'); -// copy in default activity -exec('cmd /c copy bin\\templates\\Activity.java '+ACTIVITY_PATH+' /Y'); +// copy in xml +exec('cmd /c copy framework\\res\\xml\\cordova.xml ' + PROJECT_PATH + '\\.cordova\\android\\cordova.xml /Y'); +exec('cmd /c copy framework\\res\\xml\\plugins.xml ' + PROJECT_PATH + '\\.cordova\\android\\plugins.xml /Y'); -// interpolate the activity name and package -replaceInFile(ACTIVITY_PATH, /__ACTIVITY__/, ACTIVITY); -replaceInFile(ACTIVITY_PATH, /__ID__/, PACKAGE); +// write out config file +write(PROJECT_PATH + '\\.cordova\\config', + 'VERSION=' + VERSION + '\r\n' + + 'PROJECT_PATH=' + PROJECT_PATH + '\r\n' + + 'PACKAGE=' + PACKAGE + '\r\n' + + 'ACTIVITY=' + ACTIVITY + '\r\n' + + 'TARGET=' + TARGET); -replaceInFile(MANIFEST_PATH, /__ACTIVITY__/, ACTIVITY); -replaceInFile(MANIFEST_PATH, /__PACKAGE__/, PACKAGE); - -/* -# leave the id for launching -touch $PROJECT_PATH/package-activity -echo $PACKAGE/$PACKAGE.$ACTIVITY > $PROJECT_PATH/package-activity -*/ +// run project-specific create process +fork('cscript.exe ' + PROJECT_PATH + '\\cordova\\create.js'); \ No newline at end of file diff --git a/bin/templates/project/cordova/create.bat b/bin/templates/project/cordova/create.bat new file mode 100644 index 00000000..cfde65a5 --- /dev/null +++ b/bin/templates/project/cordova/create.bat @@ -0,0 +1,2 @@ +echo "BALLS" +cscript cordova\create.js \ No newline at end of file diff --git a/bin/templates/project/cordova/create.js b/bin/templates/project/cordova/create.js new file mode 100644 index 00000000..b87f0a10 --- /dev/null +++ b/bin/templates/project/cordova/create.js @@ -0,0 +1,70 @@ +var shell=WScript.CreateObject("WScript.Shell"); + +function exec(s, output) { + WScript.Echo('Executing ' + s); + var o=shell.Exec(s); + while (o.Status == 0) { + WScript.Sleep(100); + } + WScript.Echo("Command exited with code " + o.Status); +} +function read(filename) { + WScript.Echo('Reading in ' + filename); + var fso=WScript.CreateObject("Scripting.FileSystemObject"); + var f=fso.OpenTextFile(filename, 1); + var s=f.ReadAll(); + f.Close(); + return s; +} +function write(filename, contents) { + var fso=WScript.CreateObject("Scripting.FileSystemObject"); + var f=fso.OpenTextFile(filename, 2, true); + f.Write(contents); + f.Close(); +} +function replaceInFile(filename, regexp, replacement) { + write(filename, read(filename).replace(regexp, replacement)); +} + +// working dir +var PWD = WScript.ScriptFullName.split('\\cordova\\create.js').join(''); + +var fso=WScript.CreateObject("Scripting.FileSystemObject"); +var f=fso.OpenTextFile(PWD + '\\.cordova\\config', 1); +while (!f.AtEndOfStream) { + var prop = f.ReadLine().split('='); + var line = 'var ' + prop[0] + '=' + "'" + prop[1] + "';"; + eval(line); // hacky shit to load config but whatevs +} + +var PACKAGE_AS_PATH=PACKAGE.replace(/\./g, '\\'); +var ACTIVITY_PATH=PWD+'\\src\\'+PACKAGE_AS_PATH+'\\'+ACTIVITY+'.java'; +var MANIFEST_PATH=PWD+'\\AndroidManifest.xml'; + +exec('android.bat create project --target ' + TARGET + ' --path ' + PWD + ' --package ' + PACKAGE + ' --activity ' + ACTIVITY); + +// copy in activity and other android assets +exec('cmd /c xcopy ' + PWD + '\\cordova\\templates\project\* ' + PWD +' /Y /S'); + +// copy in cordova.js +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\cordova-' + VERSION + '.js ' + PWD + '\\assets\\www /Y'); + +// copy in cordova.jar +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\cordova-' + VERSION + '.jar ' + PWD + '\\libs /Y'); + +// copy in res/xml +exec('cmd /c md ' + PWD + '\\res\\xml'); +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\cordova.xml ' + PWD + '\\res\\xml /Y'); +exec('cmd /c copy ' + PWD + '\\.cordova\\android\\plugins.xml ' + PWD + '\\res\\xml /Y'); + +// copy in default activity +exec('cmd /c copy ' + PWD + '\\cordova\\templates\\Activity.java ' + ACTIVITY_PATH + ' /Y'); + +// interpolate the activity name and package +replaceInFile(ACTIVITY_PATH, /__ACTIVITY__/, ACTIVITY); +replaceInFile(ACTIVITY_PATH, /__ID__/, PACKAGE); + +replaceInFile(MANIFEST_PATH, /__ACTIVITY__/, ACTIVITY); +replaceInFile(MANIFEST_PATH, /__PACKAGE__/, PACKAGE); + +WScript.Echo('DONE!'); \ No newline at end of file diff --git a/bin/templates/project/cordova/debug.bat b/bin/templates/project/cordova/debug.bat new file mode 100644 index 00000000..e69de29b diff --git a/bin/templates/project/cordova/emulate.bat b/bin/templates/project/cordova/emulate.bat new file mode 100644 index 00000000..e69de29b diff --git a/bin/templates/project/cordova/log.bat b/bin/templates/project/cordova/log.bat new file mode 100644 index 00000000..e69de29b