Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choose Target and use it for logging & uninstall #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,6 @@ Just builds the project, without running the tests.
cordova-paramedic --platform ios --plugin cordova-plugin-inappbrowser --justbuild
```

####--device (optional)

Tests must be run on connected device instead of emulator.

```
cordova-paramedic --platform ios --plugin cordova-plugin-inappbrowser --device
```

####--externalServerUrl (optional)

Useful when testing on real device (`--device` parameter) so that tests results from device could be posted back to paramedic server.
Expand Down Expand Up @@ -180,4 +172,3 @@ You can also use cordova-paramedic as a module directly :
paramedic.run(config);
```


68 changes: 68 additions & 0 deletions lib/ParamedicAppUninstall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
var shelljs = require('shelljs');
var path = require('path');
var fs = require("fs");
var logger = require('./utils').logger;
var util = require('./utils').utilities;

function ParamedicAppUninstall(appPath, platform) {
this.appPath = appPath;
this.platform = platform;
}

ParamedicAppUninstall.prototype.uninstallApp = function(targetObj, app) {
if(!targetObj || !targetObj.target)
return;

switch(this.platform) {
case util.ANDROID:
this.uninstallAppAndroid(targetObj, app);
break;
case util.IOS:
this.uninstallAppIOS(targetObj, app);
break;
case util.WINDOWS:
this.uninstallAppWindows(targetObj, app);
break;
default:
break;
}
}

ParamedicAppUninstall.prototype.uninstallAppAndroid = function(targetObj, app) {
var uninstallCommand = "adb -s " + targetObj.target + " uninstall " + app;
this.executeUninstallCommand(uninstallCommand);
}

ParamedicAppUninstall.prototype.uninstallAppWindows = function(targetObj, app) {
var platformPath = path.join(this.appPath, "platforms", "windows");
var packageJSPath = path.join(platformPath, "cordova", "lib", "package.js");
var programFilesPath = process.env["ProgramFiles(x86)"] || process.env["ProgramFiles"];
var appDeployPath = path.join(programFilesPath, "Microsoft SDKs",
"Windows Phone", "v8.1", "Tools", "AppDeploy", "AppDeployCmd.exe");
appDeployPath = '"' + appDeployPath + '"';

if (fs.existsSync(packageJSPath)) {
var packageJS = require(packageJSPath);
var appId = packageJS.getAppId(platformPath);
var uninstallCommand = appDeployPath + " /uninstall " + appId + " /targetdevice:" + targetObj.target;
this.executeUninstallCommand(uninstallCommand);
}
return;
}

ParamedicAppUninstall.prototype.uninstallAppIOS = function(targetObj, app) {
var uninstallCommand = "xcrun simctl uninstall " + targetObj.simId + " uninstall " + app;
this.executeUninstallCommand(uninstallCommand);
}

ParamedicAppUninstall.prototype.executeUninstallCommand = function(uninstallCommand) {
logger.info("cordova-paramedic: Running command: " + uninstallCommand);
var uninstallResult = shelljs.exec(uninstallCommand, {silent: false, async: false});
if (uninstallResult.code > 0) {
logger.error("Failed to uninstall the app" );
logger.error("Error code: " + uninstallResult.code);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be great to also log the stderr in this case to see the cause of the failure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed the silent to false. This will give the result either way

}
return;
}

module.exports = ParamedicAppUninstall;
2 changes: 1 addition & 1 deletion lib/ParamedicConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ParamedicConfig.parseFromArguments = function (argv) {
return new ParamedicConfig({
platform: argv.platform,
action: !!argv.justbuild ? 'build' : 'run',
args: (!!argv.browserify ? '--browserify ' : '') + (!!argv.device ? '--device' : ''),
args: (!!argv.browserify ? '--browserify ' : ''),
plugins: Array.isArray(argv.plugin) ? argv.plugin : [argv.plugin],
useTunnel: !!argv.useTunnel,
verbose: !!argv.verbose,
Expand Down
3 changes: 2 additions & 1 deletion lib/ParamedicKill.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ ParamedicKill.prototype.tasksOnPlatform = function (platformName) {
var tasks = [];
switch (platformName) {
case util.WINDOWS:
tasks = ["WWAHost.exe", "Xde.exe"];
// tasks = ["WWAHost.exe", "Xde.exe"];
tasks = ["WWAHost.exe"];
break;
case util.IOS:
tasks = ["Simulator", "iOS Simulator"];
Expand Down
9 changes: 5 additions & 4 deletions lib/ParamedicLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,23 @@ var util = require('./utils').utilities;
var logger = require('./utils').logger;


function ParamedicLog(platform, appPath, outputDir){
function ParamedicLog(platform, appPath, outputDir, targetObj){
this.platform = platform;
this.appPath = appPath;
this.outputDir = outputDir;
this.targetObj = targetObj;
}

ParamedicLog.prototype.logIOS = function (appPath) {
var simId = util.getSimId();
var simId = this.targetObj.simId;

if (simId) {
// Now we can print out the log file
var logPath = path.join(path.homedir(), "Library", "Logs", "CoreSimulator", simId, "system.log");
var logCommand = "cat " + logPath;
this.generateLogs(logCommand);
} else {
logger.error("Failed to find ID of mobilespec simulator");
logger.error("Failed to find ID of simulator");
}
}

Expand All @@ -43,7 +44,7 @@ ParamedicLog.prototype.logWindows = function (appPath, logMins) {
}

ParamedicLog.prototype.logAndroid = function (){
var logCommand = "adb logcat -d -v time";
var logCommand = "adb -s " + this.targetObj.target + " logcat -d -v time";

var numDevices = util.countAndroidDevices();
if (numDevices != 1) {
Expand Down
108 changes: 108 additions & 0 deletions lib/ParamedicTargetChooser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
var Q = require('q');
var shelljs = require('shelljs');
var path = require("path-extra");
var logger = require('./utils').logger;
var util = require('./utils').utilities;
var spawn = require('child_process').spawn;
var ParamedicKill = require('./ParamedicKill');

var ANDROID_RETRY_TIMES = 3;
var ANDROID_TIME_OUT = 300000; //5 Minutes

function ParamedicTargetChooser(appPath, platform) {
this.appPath = appPath;
this.platform = platform;
}

ParamedicTargetChooser.prototype.chooseTarget = function(emulator) {
var targetObj = "";
switch(this.platform) {
case util.ANDROID:
targetObj = this.chooseTargetForAndroid(emulator);
break;
case util.IOS:
targetObj = this.chooseTargetForIOS(emulator);
break;
case util.WINDOWS:
targetObj = this.chooseTargetForWindows(emulator);
break;
default:
break;
}
return targetObj;
}

ParamedicTargetChooser.prototype.chooseTargetForAndroid = function(emulator) {
logger.info("cordova-paramedic: Choosing Target for Android");
var self = this;
return this.startAnAndroidEmulator().then(function(emulatorId){
var obj = {};
obj.target = emulatorId;
return obj;
});
}

ParamedicTargetChooser.prototype.startAnAndroidEmulator = function() {
logger.info("cordova-paramedic: Starting an Android emulator");

var emuPath = path.join(this.appPath, "platforms", "android", "cordova", "lib", "emulator");
var emulator = require(emuPath);

var tryStart = function(numberTriesRemaining) {
return emulator.start(null, ANDROID_TIME_OUT)
.then(function(emulatorId) {
if (emulatorId) {
return emulatorId;
} else if (numberTriesRemaining > 0) {
var paramedicKill = new ParamedicKill(util.ANDROID);
paramedicKill.kill();
return tryStart(numberTriesRemaining - 1);
} else {
logger.error("cordova-paramedic: Could not start an android emulator");
return null;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: should we log an error if we run out of tries?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

}
});
};

// Check if the emulator has already been started
return emulator.list_started()
.then(function(started) {
if (started && started.length > 0) {
return started[0];
} else {
return tryStart(ANDROID_RETRY_TIMES);
}
});
}

ParamedicTargetChooser.prototype.chooseTargetForWindows = function(emulator) {
logger.info("cordova-paramedic: Choosing Target for Windows");
var windowsCommand = "cordova run --list --emulator";

logger.info("cordova-paramedic: Running command: " + windowsCommand);
var devicesResult = shelljs.exec(windowsCommand, {silent: true, async: false});
if (devicesResult.code > 0) {
logger.error("Failed to get the list of devices for windows");
return Q({target: undefined});
}

var lines = devicesResult.output.split(/\n/);
if(lines.length <= 1) {
logger.error("No devices/emulators available for windows");
return Q({target: undefined});
}

return Q({target: lines[1].split('. ')[0].trim()});
}

ParamedicTargetChooser.prototype.chooseTargetForIOS = function(emulator) {
logger.info("cordova-paramedic: Choosing Target for iOS");
var simulatorModelId = util.getSimulatorModelId();
var split = simulatorModelId.split(", ");
var device = split[0].trim();

var simId = util.getSimulatorId(simulatorModelId);
return Q({target: device, simId: simId});
}

module.exports = ParamedicTargetChooser;
5 changes: 3 additions & 2 deletions lib/ParamediciOSPermissions.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ var util = require('./utils').utilities;

var TCC_FOLDER_PERMISSION = 0755;

function ParamediciOSPermissions(appName, tccDb) {
function ParamediciOSPermissions(appName, tccDb, targetObj) {
this.appName = appName;
this.tccDb = tccDb;
this.targetObj = targetObj;
}

ParamediciOSPermissions.prototype.updatePermissions = function(serviceList){
var simulatorsFolder = util.getSimulatorsFolder();
var simId = util.getSimId();
var simId = this.targetObj.simId;
logger.info('Sim Id is: ' + simId);
var destinationTCCFile = path.join(simulatorsFolder, simId, '/data/Library/TCC/TCC.db');

Expand Down
Loading