2012-05-08 01:56:37 +08:00
/ *
Licensed to the Apache Software Foundation ( ASF ) under one
or more contributor license agreements . See the NOTICE file
distributed with this work for additional information
regarding copyright ownership . The ASF licenses this file
to you under the Apache License , Version 2.0 ( the
"License" ) ; you may not use this file except in compliance
with the License . You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing ,
software distributed under the License is distributed on an
"AS IS" BASIS , WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND , either express or implied . See the License for the
specific language governing permissions and limitations
under the License .
* /
2011-08-21 04:42:46 +08:00
/ *
2012-02-02 09:45:49 +08:00
* create a cordova / android project
2011-08-21 04:42:46 +08:00
*
* USAGE
* . / create [ path package activity ]
* /
2012-06-09 08:42:53 +08:00
var fso = WScript . CreateObject ( 'Scripting.FileSystemObject' ) ;
2011-08-21 04:42:46 +08:00
function read ( filename ) {
var fso = WScript . CreateObject ( "Scripting.FileSystemObject" ) ;
2012-05-17 10:38:11 +08:00
var f = fso . OpenTextFile ( filename , 1 ) ;
2011-08-21 04:42:46 +08:00
var s = f . ReadAll ( ) ;
f . Close ( ) ;
return s ;
}
2012-06-21 10:08:18 +08:00
function setTarget ( ) {
var targets = shell . Exec ( 'android.bat list targets' ) . StdOut . ReadAll ( ) . match ( /id:\s\d+/g ) ;
return targets [ targets . length - 1 ] . replace ( /id: / , "" ) ; // TODO: give users the option to set their target
}
2011-08-21 04:42:46 +08:00
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 ) ) ;
}
2012-06-21 10:08:18 +08:00
function exec ( command ) {
var oShell = shell . Exec ( command ) ;
while ( oShell . Status == 0 ) {
2012-09-19 05:11:22 +08:00
if ( ! oShell . StdOut . AtEndOfStream ) {
var line = oShell . StdOut . ReadLine ( ) ;
// XXX: Change to verbose mode
2012-09-19 05:45:04 +08:00
// WScript.StdOut.WriteLine(line);
2012-09-19 05:11:22 +08:00
}
2012-06-21 10:08:18 +08:00
WScript . sleep ( 100 ) ;
2012-05-17 10:38:11 +08:00
}
}
2012-09-18 08:14:25 +08:00
function createAppInfoJar ( ) {
2012-09-19 05:11:22 +08:00
if ( ! fso . FileExists ( ROOT + "\\bin\\templates\\cordova\\appinfo.jar" ) ) {
WScript . Echo ( "Creating appinfo.jar..." ) ;
var cur = shell . CurrentDirectory ;
shell . CurrentDirectory = ROOT + "\\bin\\templates\\cordova\\ApplicationInfo" ;
exec ( "javac ApplicationInfo.java" ) ;
exec ( "jar -cfe ..\\appinfo.jar ApplicationInfo ApplicationInfo.class" ) ;
shell . CurrentDirectory = cur ;
}
2012-09-18 08:14:25 +08:00
}
2012-06-09 08:42:53 +08:00
function cleanup ( ) {
// Cleanup
2012-06-14 06:52:26 +08:00
// if(fso.FileExists(ROOT + '\\framework\\libs\\commons-codec-1.6.jar')) {
// fso.DeleteFile(ROOT + '\\framework\\libs\\commons-codec-1.6.jar');
// fso.DeleteFolder(ROOT + '\\framework\\libs', true);
// }
2012-06-09 08:42:53 +08:00
if ( fso . FileExists ( ROOT + '\\framework\\cordova-' + VERSION + '.jar' ) ) {
fso . DeleteFile ( ROOT + '\\framework\\cordova-' + VERSION + '.jar' ) ;
}
if ( fso . FileExists ( ROOT + '\\framework\\assets\\www\\cordova-' + VERSION + '.js' ) ) {
fso . DeleteFile ( ROOT + '\\framework\\assets\\www\\cordova-' + VERSION + '.js' ) ;
}
}
2011-08-21 04:42:46 +08:00
2012-06-09 09:04:08 +08:00
function downloadCommonsCodec ( ) {
2012-09-18 00:44:47 +08:00
if ( ! fso . FileExists ( ROOT + '\\framework\\libs\\commons-codec-1.7.jar' ) ) {
2012-06-09 09:04:08 +08:00
// We need the .jar
2012-09-18 00:44:47 +08:00
var url = 'http://apache.osuosl.org/commons/codec/binaries/commons-codec-1.7-bin.zip' ;
2012-06-09 09:04:08 +08:00
var libsPath = ROOT + '\\framework\\libs' ;
2012-09-18 00:44:47 +08:00
var savePath = libsPath + '\\commons-codec-1.7-bin.zip' ;
2012-06-09 09:04:08 +08:00
if ( ! fso . FileExists ( savePath ) ) {
if ( ! fso . FolderExists ( ROOT + '\\framework\\libs' ) ) {
fso . CreateFolder ( libsPath ) ;
}
// We need the zip to get the jar
var xhr = WScript . CreateObject ( 'MSXML2.XMLHTTP' ) ;
xhr . open ( 'GET' , url , false ) ;
xhr . send ( ) ;
if ( xhr . status == 200 ) {
var stream = WScript . CreateObject ( 'ADODB.Stream' ) ;
stream . Open ( ) ;
stream . Type = 1 ;
stream . Write ( xhr . ResponseBody ) ;
stream . Position = 0 ;
stream . SaveToFile ( savePath ) ;
stream . Close ( ) ;
} else {
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.' ) ;
}
}
var app = WScript . CreateObject ( 'Shell.Application' ) ;
2012-09-18 04:26:23 +08:00
var source = app . NameSpace ( savePath ) . Items ( ) ;
2012-06-09 09:04:08 +08:00
var target = app . NameSpace ( ROOT + '\\framework\\libs' ) ;
target . CopyHere ( source , 256 ) ;
// Move the jar into libs
2012-09-18 04:26:23 +08:00
fso . MoveFile ( ROOT + '\\framework\\libs\\commons-codec-1.7\\commons-codec-1.7.jar' , ROOT + '\\framework\\libs\\commons-codec-1.7.jar' ) ;
2012-06-09 09:04:08 +08:00
// Clean up
2012-09-18 00:44:47 +08:00
fso . DeleteFile ( ROOT + '\\framework\\libs\\commons-codec-1.7-bin.zip' ) ;
fso . DeleteFolder ( ROOT + '\\framework\\libs\\commons-codec-1.7' , true ) ;
2012-06-09 09:04:08 +08:00
}
}
2011-08-21 04:42:46 +08:00
var args = WScript . Arguments , PROJECT _PATH = "example" ,
2012-02-02 09:45:49 +08:00
PACKAGE = "org.apache.cordova.example" , ACTIVITY = "cordovaExample" ,
2011-08-21 04:42:46 +08:00
shell = WScript . CreateObject ( "WScript.Shell" ) ;
2012-05-18 01:59:38 +08:00
// working dir
var ROOT = WScript . ScriptFullName . split ( '\\bin\\create.js' ) . join ( '' ) ;
2011-08-21 04:42:46 +08:00
if ( args . Count ( ) == 3 ) {
PROJECT _PATH = args ( 0 ) ;
PACKAGE = args ( 1 ) ;
ACTIVITY = args ( 2 ) ;
}
2012-06-09 09:04:08 +08:00
if ( fso . FolderExists ( PROJECT _PATH ) ) {
WScript . Echo ( "Project already exists!" ) ;
WScript . Quit ( 1 ) ;
}
2011-08-21 04:42:46 +08:00
var PACKAGE _AS _PATH = PACKAGE . replace ( /\./g , '\\' ) ;
var ACTIVITY _PATH = PROJECT _PATH + '\\src\\' + PACKAGE _AS _PATH + '\\' + ACTIVITY + '.java' ;
var MANIFEST _PATH = PROJECT _PATH + '\\AndroidManifest.xml' ;
2012-06-21 10:08:18 +08:00
var TARGET = setTarget ( ) ;
2012-06-08 10:56:08 +08:00
var VERSION = read ( ROOT + '\\VERSION' ) . replace ( /\r\n/ , '' ) . replace ( /\n/ , '' ) ;
2011-08-21 04:42:46 +08:00
// create the project
2012-09-19 05:11:22 +08:00
WScript . Echo ( "Creating new android project..." ) ;
2011-08-21 04:42:46 +08:00
exec ( 'android.bat create project --target ' + TARGET + ' --path ' + PROJECT _PATH + ' --package ' + PACKAGE + ' --activity ' + ACTIVITY ) ;
2012-06-28 08:54:57 +08:00
// build from source. distro should have these files
if ( ! fso . FileExists ( ROOT + '\\cordova-' + VERSION + '.jar' ) &&
! fso . FileExists ( ROOT + '\\cordova-' + VERSION + '.js' ) ) {
2012-09-19 05:11:22 +08:00
WScript . Echo ( "Building jar and js files..." ) ;
2012-06-28 08:54:57 +08:00
// update the cordova framework project to a target that exists on this machine
exec ( 'android.bat update project --target ' + TARGET + ' --path ' + ROOT + '\\framework' ) ;
// pull down commons codec if necessary
downloadCommonsCodec ( ) ;
2012-09-18 04:26:23 +08:00
exec ( 'ant.bat -f \"' + ROOT + '\\framework\\build.xml\" jar' ) ;
2012-06-28 08:54:57 +08:00
}
2011-08-21 04:42:46 +08:00
// copy in the project template
2012-09-19 05:11:22 +08:00
WScript . Echo ( "Copying template files..." ) ;
2012-06-22 12:15:53 +08:00
exec ( '%comspec% /c xcopy ' + ROOT + '\\bin\\templates\\project\\res ' + PROJECT _PATH + '\\res\\ /E /Y' ) ;
exec ( '%comspec% /c xcopy ' + ROOT + '\\bin\\templates\\project\\assets ' + PROJECT _PATH + '\\assets\\ /E /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\project\\AndroidManifest.xml ' + PROJECT _PATH + '\\AndroidManifest.xml /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\project\\Activity.java ' + ACTIVITY _PATH + ' /Y' ) ;
2012-05-17 10:38:11 +08:00
2012-06-28 08:54:57 +08:00
// check if we have the source or the distro files
2012-09-19 05:11:22 +08:00
WScript . Echo ( "Copying js, jar & config.xml files..." ) ;
2012-06-28 08:54:57 +08:00
if ( fso . FolderExists ( ROOT + '\\framework' ) ) {
exec ( '%comspec% /c copy ' + ROOT + '\\framework\\assets\\www\\cordova-' + VERSION + '.js ' + PROJECT _PATH + '\\assets\\www\\cordova-' + VERSION + '.js /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\framework\\cordova-' + VERSION + '.jar ' + PROJECT _PATH + '\\libs\\cordova-' + VERSION + '.jar /Y' ) ;
fso . CreateFolder ( PROJECT _PATH + '\\res\\xml' ) ;
2012-08-25 03:14:58 +08:00
exec ( '%comspec% /c copy ' + ROOT + '\\framework\\res\\xml\\config.xml ' + PROJECT _PATH + '\\res\\xml\\config.xml /Y' ) ;
2012-06-28 08:54:57 +08:00
} else {
// copy in cordova.js
exec ( '%comspec% /c copy ' + ROOT + '\\cordova-' + VERSION + '.js ' + PROJECT _PATH + '\\assets\\www\\cordova-' + VERSION + '.js /Y' ) ;
// copy in cordova.jar
exec ( '%comspec% /c copy ' + ROOT + '\\cordova-' + VERSION + '.jar ' + PROJECT _PATH + '\\libs\\cordova-' + VERSION + '.jar /Y' ) ;
// copy in xml
fso . CreateFolder ( PROJECT _PATH + '\\res\\xml' ) ;
2012-08-25 04:40:23 +08:00
exec ( '%comspec% /c copy ' + ROOT + '\\xml\\config.xml ' + PROJECT _PATH + '\\res\\xml\\config.xml /Y' ) ;
2012-06-28 08:54:57 +08:00
}
2012-06-08 10:56:08 +08:00
2012-06-21 10:08:18 +08:00
// copy cordova scripts
fso . CreateFolder ( PROJECT _PATH + '\\cordova' ) ;
2012-09-19 05:11:22 +08:00
createAppInfoJar ( ) ;
WScript . Echo ( "Copying cordova command tools..." ) ;
2012-06-22 12:15:53 +08:00
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\appinfo.jar ' + PROJECT _PATH + '\\cordova\\appinfo.jar /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\cordova.js ' + PROJECT _PATH + '\\cordova\\cordova.js /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\cordova.bat ' + PROJECT _PATH + '\\cordova\\cordova.bat /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\clean.bat ' + PROJECT _PATH + '\\cordova\\clean.bat /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\debug.bat ' + PROJECT _PATH + '\\cordova\\debug.bat /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\log.bat ' + PROJECT _PATH + '\\cordova\\log.bat /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\emulate.bat ' + PROJECT _PATH + '\\cordova\\emulate.bat /Y' ) ;
exec ( '%comspec% /c copy ' + ROOT + '\\bin\\templates\\cordova\\BOOM.bat ' + PROJECT _PATH + '\\cordova\\BOOM.bat /Y' ) ;
2012-06-21 10:08:18 +08:00
2012-06-08 10:56:08 +08:00
// interpolate the activity name and package
2012-09-19 05:11:22 +08:00
WScript . Echo ( "Updating AndroidManifest.xml and Main Activity..." ) ;
2012-06-08 10:56:08 +08:00
replaceInFile ( ACTIVITY _PATH , /__ACTIVITY__/ , ACTIVITY ) ;
replaceInFile ( ACTIVITY _PATH , /__ID__/ , PACKAGE ) ;
replaceInFile ( MANIFEST _PATH , /__ACTIVITY__/ , ACTIVITY ) ;
replaceInFile ( MANIFEST _PATH , /__PACKAGE__/ , PACKAGE ) ;
2012-06-09 08:42:53 +08:00
cleanup ( ) ;