blob: fcb011e75d2593d709e8a6e921cf3da1f0b05dc3 [file] [log] [blame]
/*
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.
*/
var fso = WScript.CreateObject('Scripting.FileSystemObject');
var wscript_shell = WScript.CreateObject("WScript.Shell");
var args = WScript.Arguments;
// working dir
var ROOT = WScript.ScriptFullName.split('\\cordova\\lib\\deploy.js').join('');
// path to CordovaDeploy.exe
var CORDOVA_DEPLOY_EXE = '\\cordova\\lib\\CordovaDeploy\\CordovaDeploy\\bin\\Debug\\CordovaDeploy.exe';
// path to CordovaDeploy
var CORDOVA_DEPLOY = '\\cordova\\lib\\CordovaDeploy';
var BUILD_RELEASE = false;
// help function
function Usage() {
Log("");
Log("Usage: run [ --device | --emulator | --target=<id> ]");
Log(" --device : Deploys and runs the project on the connected device.");
Log(" --emulator : Deploys and runs the project on an emulator.");
Log(" --target=<id> : Deploys and runs the project on the specified target.")
Log("examples:");
Log(" run");
Log(" run --emulator");
Log(" run --device");
Log(" run --target=7988B8C3-3ADE-488d-BA3E-D052AC9DC710");
Log("");
}
// log to stdout or stderr
function Log(msg, error) {
if (error) {
WScript.StdErr.WriteLine(msg);
}
else {
WScript.StdOut.WriteLine(msg);
}
}
var ForReading = 1, ForWriting = 2, ForAppending = 8;
var TristateUseDefault = 2, TristateTrue = 1, TristateFalse = 0;
// executes a commmand in the shell
function exec(command) {
var oShell=wscript_shell.Exec(command);
while (oShell.Status == 0) {
WScript.sleep(100);
}
}
// executes a commmand in the shell
function exec_verbose(command) {
//Log("Command: " + command);
var oShell=wscript_shell.Exec(command);
while (oShell.Status == 0) {
//Wait a little bit so we're not super looping
WScript.sleep(100);
//Print any stdout output from the script
if (!oShell.StdOut.AtEndOfStream) {
var line = oShell.StdOut.ReadAll();
Log(line);
}
}
//Check to make sure our scripts did not encounter an error
if (!oShell.StdErr.AtEndOfStream) {
var line = oShell.StdErr.ReadAll();
Log(line, true);
WScript.Quit(1);
}
}
// returns the contents of a file
function read(filename) {
if (fso.FileExists(filename)) {
var f=fso.OpenTextFile(filename, 1,2);
var s=f.ReadAll();
f.Close();
return s;
}
else {
Log('Cannot read non-existant file : ' + filename, true);
WScript.Quit(1);
}
return null;
}
// builds the CordovaDeploy.exe if it does not already exist
function cordovaDeploy(path) {
if (fso.FileExists(path + CORDOVA_DEPLOY_EXE)) {
return;
}
Log('CordovaDeploy.exe not found, attempting to build CordovaDeploy.exe...');
// build CordovaDeploy.exe
if (fso.FolderExists(path + '\\cordova') && fso.FolderExists(path + CORDOVA_DEPLOY) &&
fso.FileExists(path + CORDOVA_DEPLOY + '\\CordovaDeploy.sln')) {
// delete any previously generated files
if (fso.FolderExists(path + CORDOVA_DEPLOY + '\\CordovaDeploy\\obj')) {
fso.DeleteFolder(path + CORDOVA_DEPLOY + '\\CordovaDeploy\\obj');
}
if (fso.FolderExists(path + CORDOVA_DEPLOY + '\\CordovaDeploy\\Bin')) {
fso.DeleteFolder(path + CORDOVA_DEPLOY + '\\CordovaDeploy\\Bin');
}
exec_verbose('msbuild ' + path + CORDOVA_DEPLOY + '\\CordovaDeploy.sln');
if (fso.FileExists(path + CORDOVA_DEPLOY_EXE)) {
Log('CordovaDeploy.exe compiled, SUCCESS.');
}
else {
Log('ERROR: MSBUILD FAILED TO COMPILE CordovaDeploy.exe', true);
WScript.Quit(1);
}
}
else {
Log('ERROR: CordovaDeploy.sln not found, unable to compile CordovaDeploy tool.', true);
WScript.Quit(1);
}
}
// builds and launches project on device
function device(path)
{
cordovaDeploy(path);
if (fso.FileExists(path + CORDOVA_DEPLOY_EXE)) {
Log('Deploying to device ...');
exec_verbose('%comspec% /c ' + path + CORDOVA_DEPLOY_EXE + ' ' + path + ' -d:0');
}
else
{
Log('Error: Failed to find CordovaDeploy.exe in ' + path, true);
Log('DEPLOY FAILED.', true);
WScript.Quit(1);
}
}
// builds and launches project on emulator
function emulator(path)
{
cordovaDeploy(path);
if (fso.FileExists(path + CORDOVA_DEPLOY_EXE)) {
Log('Deploying to emulator ...');
exec_verbose('%comspec% /c ' + path + CORDOVA_DEPLOY_EXE + ' ' + path + ' -d:1');
}
else
{
Log('Error: Failed to find CordovaDeploy.exe in ' + path, true);
Log('DEPLOY FAILED.', true);
WScript.Quit(1);
}
}
// builds and launches the project on the specified target
function target(path, device_id) {
cordovaDeploy(path);
if (fso.FileExists(path + CORDOVA_DEPLOY_EXE)) {
Log('Deploying to ' + device_id + ' ...');
var out = wscript_shell.Exec('cscript ' + path + '\\cordova\\lib\\target-list.js //nologo');
while(out.Status == 0) {
WScript.Sleep(100);
}
//Check to make sure our script did not encounter an error
if (!out.StdErr.AtEndOfStream) {
var line = out.StdErr.ReadAll();
Log('Error getting availible targets : ', true);
Log(line, true);
WScript.Quit(1);
}
else {
if (!out.StdOut.AtEndOfStream) {
var line = out.StdOut.ReadAll();
var targets = line.split('\r\n');
var check_id = new RegExp(device_id);
for (target in targets) {
if (targets[target].match(check_id)) {
//TODO: this only gets single digit index, account for device index of 10+?
var index = targets[target].substr(0,1);
exec_verbose(path + CORDOVA_DEPLOY_EXE + ' ' + path + ' -d:' + index);
return;
}
}
Log('Error : target ' + device_id + ' was not found.', true);
Log('DEPLOY FAILED.', true);
WScript.Quit(1);
}
else {
Log('Error : target-list.js Failed to find any devices', true);
Log('DEPLOY FAILED.', true);
WScript.Quit(1);
}
}
}
else {
Log('Error: Failed to find CordovaDeploy.exe in ' + path, true);
Log('DEPLOY FAILED.', true);
WScript.Quit(1);
}
}
function build(path) {
if (!BUILD_RELEASE) {
exec_verbose('%comspec% /c ' + ROOT + '\\cordova\\build --debug');
}
else {
exec_verbose('%comspec% /c ' + ROOT + '\\cordova\\build --release');
}
}
Log("");
if (args.Count() > 0) {
// support help flags
if (args(0) == "--help" || args(0) == "/?" ||
args(0) == "help" || args(0) == "-help" || args(0) == "/help") {
Usage();
WScript.Quit(1);
}
else if (args.Count() > 1) {
Log('Error: Too many arguments.', true);
Usage();
WScript.Quit(1);
}
else if (fso.FolderExists(ROOT)) {
if (args(0) == "--emulator" || args(0) == "-e") {
build(ROOT);
emulator(ROOT);
}
else if (args(0) == "--device" || args(0) == "-d") {
build(ROOT);
device(ROOT);
}
else if (args(0).substr(0,9) == "--target=") {
build(ROOT);
var device_id = args(0).split('--target=').join('');
target(ROOT, device_id);
}
else {
Log('Error: \"' + arg(0) + '\" is not recognized as a deploy option', true);
Usage();
WScript.Quit(1);
}
}
else {
Log('Error: Project directory not found,', true);
Usage();
WScript.Quit(1);
}
}
else {
Log('WARNING: [ --device | --emulator | --target=<id> ] not specified, defaulting to emulator...');
emulator(ROOT);
}