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]
|
|
|
|
*/
|
|
|
|
|
2013-04-17 01:34:14 +08:00
|
|
|
var args = WScript.Arguments, PROJECT_PATH="example",
|
|
|
|
PACKAGE="org.apache.cordova.example", ACTIVITY="cordovaExample",
|
|
|
|
shell=WScript.CreateObject("WScript.Shell"),
|
|
|
|
fso = WScript.CreateObject('Scripting.FileSystemObject');
|
|
|
|
|
|
|
|
function Usage() {
|
|
|
|
Log("Usage: create PathTONewProject [ PackageName AppName ]");
|
|
|
|
Log(" PathTONewProject : The path to where you wish to create the project");
|
|
|
|
Log(" PackageName : The package for the project (default is org.apache.cordova.example)")
|
|
|
|
Log(" AppName : The name of the application/activity (default is cordovaExample)");
|
|
|
|
Log("examples:");
|
|
|
|
Log(" create C:\\Users\\anonymous\\Desktop\\MyProject");
|
|
|
|
Log(" create C:\\Users\\anonymous\\Desktop\\MyProject io.Cordova.Example AnApp");
|
|
|
|
}
|
|
|
|
|
|
|
|
// logs messaged to stdout and stderr
|
|
|
|
function Log(msg, error) {
|
|
|
|
if (error) {
|
|
|
|
WScript.StdErr.WriteLine(msg);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
WScript.StdOut.WriteLine(msg);
|
|
|
|
}
|
|
|
|
}
|
2012-06-09 08:42:53 +08:00
|
|
|
|
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;
|
|
|
|
}
|
2013-01-31 06:57:49 +08:00
|
|
|
|
|
|
|
function checkTargets(targets) {
|
|
|
|
if(!targets) {
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("You do not have any android targets setup. Please create at least one target with the `android` command", true);
|
2013-01-31 06:57:49 +08:00
|
|
|
WScript.Quit(69);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2013-01-31 06:57:49 +08:00
|
|
|
checkTargets(targets);
|
2012-06-21 10:08:18 +08:00
|
|
|
return targets[targets.length - 1].replace(/id: /, ""); // TODO: give users the option to set their target
|
|
|
|
}
|
2012-09-29 09:18:11 +08:00
|
|
|
function setApiLevel() {
|
|
|
|
var targets = shell.Exec('android.bat list targets').StdOut.ReadAll().match(/API level:\s\d+/g);
|
2013-01-31 06:57:49 +08:00
|
|
|
checkTargets(targets);
|
2012-09-29 09:18:11 +08:00
|
|
|
return targets[targets.length - 1].replace(/API level: /, "");
|
|
|
|
}
|
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")) {
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("Creating appinfo.jar...");
|
2012-09-19 05:11:22 +08:00
|
|
|
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-05-18 01:59:38 +08:00
|
|
|
// working dir
|
|
|
|
var ROOT = WScript.ScriptFullName.split('\\bin\\create.js').join('');
|
2013-04-17 01:34:14 +08:00
|
|
|
if (args.Count() > 0) {
|
|
|
|
// support help flags
|
|
|
|
if (args(0) == "--help" || args(0) == "/?" ||
|
|
|
|
args(0) == "help" || args(0) == "-help" || args(0) == "/help" || args(0) == "-h") {
|
|
|
|
Usage();
|
|
|
|
WScript.Quit(2);
|
|
|
|
}
|
2011-08-21 04:42:46 +08:00
|
|
|
|
|
|
|
PROJECT_PATH=args(0);
|
2013-04-17 01:34:14 +08:00
|
|
|
if (args.Count() > 1) {
|
|
|
|
PACKAGE = args(1);
|
|
|
|
}
|
|
|
|
if (args.Count() > 2) {
|
|
|
|
ACTIVITY = args(2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Log("Error : No project path provided.");
|
|
|
|
Usage();
|
|
|
|
WScript.Quit(2);
|
2011-08-21 04:42:46 +08:00
|
|
|
}
|
|
|
|
|
2012-06-09 09:04:08 +08:00
|
|
|
if(fso.FolderExists(PROJECT_PATH)) {
|
2013-02-07 12:32:54 +08:00
|
|
|
Log("Project path already exists!", true);
|
2013-04-17 01:34:14 +08:00
|
|
|
WScript.Quit(2);
|
2012-06-09 09:04:08 +08:00
|
|
|
}
|
|
|
|
|
2011-08-21 04:42:46 +08:00
|
|
|
var PACKAGE_AS_PATH=PACKAGE.replace(/\./g, '\\');
|
2013-05-14 09:18:45 +08:00
|
|
|
var ACTIVITY_DIR=PROJECT_PATH + '\\src\\' + PACKAGE_AS_PATH;
|
|
|
|
var ACTIVITY_PATH=ACTIVITY_DIR+'\\'+ACTIVITY+'.java';
|
2011-08-21 04:42:46 +08:00
|
|
|
var MANIFEST_PATH=PROJECT_PATH+'\\AndroidManifest.xml';
|
2012-06-21 10:08:18 +08:00
|
|
|
var TARGET=setTarget();
|
2012-09-29 09:18:11 +08:00
|
|
|
var API_LEVEL=setApiLevel();
|
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
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("Creating new android project...");
|
2013-02-07 12:32:54 +08:00
|
|
|
exec('android.bat create project --target "'+TARGET+'" --path "'+PROJECT_PATH+'" --package "'+PACKAGE+'" --activity "'+ACTIVITY+'"');
|
2011-08-21 04:42:46 +08:00
|
|
|
|
2012-06-28 08:54:57 +08:00
|
|
|
// build from source. distro should have these files
|
|
|
|
if (!fso.FileExists(ROOT+'\\cordova-'+VERSION+'.jar') &&
|
2013-05-14 09:18:45 +08:00
|
|
|
!fso.FileExists(ROOT+'\\cordova.js')) {
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("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
|
2013-02-07 12:32:54 +08:00
|
|
|
exec('android.bat update project --target "'+TARGET+'" --path "'+ROOT+'\\framework"');
|
|
|
|
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
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("Copying template files...");
|
2013-02-07 12:32:54 +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');
|
2013-05-14 09:18:45 +08:00
|
|
|
exec('%comspec% /c mkdir "' + ACTIVITY_DIR + '"');
|
|
|
|
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
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("Copying js, jar & config.xml files...");
|
2012-06-28 08:54:57 +08:00
|
|
|
if(fso.FolderExists(ROOT + '\\framework')) {
|
2013-05-14 09:18:45 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\framework\\assets\\www\\cordova.js" "'+PROJECT_PATH+'\\assets\\www\\cordova.js" /Y');
|
2013-02-07 12:32:54 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\framework\\cordova-'+VERSION+'.jar" "'+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar" /Y');
|
2012-06-28 08:54:57 +08:00
|
|
|
fso.CreateFolder(PROJECT_PATH + '\\res\\xml');
|
2013-02-07 12:32:54 +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
|
2013-05-14 09:18:45 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\cordova.js" "'+PROJECT_PATH+'\\assets\\www\\cordova.js" /Y');
|
2012-06-28 08:54:57 +08:00
|
|
|
// copy in cordova.jar
|
2013-02-07 12:32:54 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\cordova-'+VERSION+'.jar" "'+PROJECT_PATH+'\\libs\\cordova-'+VERSION+'.jar" /Y');
|
2012-06-28 08:54:57 +08:00
|
|
|
// copy in xml
|
|
|
|
fso.CreateFolder(PROJECT_PATH + '\\res\\xml');
|
2013-02-07 12:32:54 +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');
|
2013-04-17 01:34:14 +08:00
|
|
|
fso.CreateFolder(PROJECT_PATH + '\\cordova\\lib');
|
2012-09-19 05:11:22 +08:00
|
|
|
createAppInfoJar();
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("Copying cordova command tools...");
|
2013-02-07 12:32:54 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\appinfo.jar" "' + PROJECT_PATH + '\\cordova\\appinfo.jar" /Y');
|
2013-05-14 09:18:45 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\cordova.js" "' + PROJECT_PATH + '\\cordova\\lib\\cordova.js" /Y');
|
2013-02-07 12:32:54 +08:00
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\install-device.bat" "' + PROJECT_PATH + '\\cordova\\lib\\install-device.bat" /Y');
|
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\install-emulator.bat" "' + PROJECT_PATH + '\\cordova\\lib\\install-emulator.bat" /Y');
|
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\list-emulator-images.bat" "' + PROJECT_PATH + '\\cordova\\lib\\list-emulator-images.bat" /Y');
|
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\list-devices.bat" "' + PROJECT_PATH + '\\cordova\\lib\\list-devices.bat" /Y');
|
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\list-started-emulators.bat" "' + PROJECT_PATH + '\\cordova\\lib\\list-started-emulators.bat" /Y');
|
|
|
|
exec('%comspec% /c copy "'+ROOT+'\\bin\\templates\\cordova\\lib\\start-emulator.bat" "' + PROJECT_PATH + '\\cordova\\lib\\start-emulator.bat" /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\\build.bat" "' + PROJECT_PATH + '\\cordova\\build.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\\run.bat" "' + PROJECT_PATH + '\\cordova\\run.bat" /Y');
|
2012-06-21 10:08:18 +08:00
|
|
|
|
2012-06-08 10:56:08 +08:00
|
|
|
// interpolate the activity name and package
|
2013-04-17 01:34:14 +08:00
|
|
|
Log("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-09-29 09:18:11 +08:00
|
|
|
replaceInFile(MANIFEST_PATH, /__APILEVEL__/, API_LEVEL);
|
2012-06-08 10:56:08 +08:00
|
|
|
|
2012-06-09 08:42:53 +08:00
|
|
|
cleanup();
|