Skip to content

Commit

Permalink
Merge pull request #59 from dlifshitz-maca/TIMOB-8944_01
Browse files Browse the repository at this point in the history
TIMOB-8943: BlackBerry: Add BlackBerry to drillbit scripts
  • Loading branch information
dlifshitz-maca committed May 30, 2012
2 parents 59cac4a + 061e311 commit 77f29b7
Show file tree
Hide file tree
Showing 4 changed files with 229 additions and 72 deletions.
109 changes: 72 additions & 37 deletions drillbit/modules/drillbit/0.1.0/blackberry.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ function BlackBerrySimulator(drillbit, blackberryNdk) {
ti.api.debug('Using javascript runtime: ' + this.runtime);

this.blackberryBuilder = ti.path.join(drillbit.mobileSdk, 'blackberry', 'builder.py');
this.blackberryDeviceManagement = ti.path.join(drillbit.mobileSdk, 'blackberry', 'devicemanagement.py');
};

BlackBerrySimulator.prototype.createDeviceManagementProcess = function(command, args) {
var procArgs =
[
this.blackberryDeviceManagement,
'-t', 'simulator', // FIXME: need the actual type
'-d', this.drillbit.testHarnessDir,
'-p', this.blackberryNdk,
command,
];

if (args) {
procArgs = procArgs.concat(args);
}
return this.drillbit.createPythonProcess(procArgs);
};

BlackBerrySimulator.prototype.createTestHarnessBuilderProcess = function(command, args) {
Expand All @@ -37,16 +54,23 @@ BlackBerrySimulator.prototype.createTestHarnessBuilderProcess = function(command
return this.drillbit.createPythonProcess(builderArgs);
};

BlackBerrySimulator.prototype.getTestHarnessPID = function() {
// TODO Mac
ti.api.error("Not implemented: getTestHarnessPID");
return null;
};

BlackBerrySimulator.prototype.isTestHarnessRunning = function() {
// TODO Mac
ti.api.error("Not implemented: isTestHarnessRunning");
return true;
var retVal = false;
var command = 'isAppRunning';
var process = this.createDeviceManagementProcess(command);

var self = this;
process.setOnReadLine(function(data) {
var lines = data.split("\n");
lines.forEach(function(line) {
if (line.indexOf("result::") === 0) {
var trueString = "true";
retVal = line.indexOf(trueString, line.length - trueString.length) !== -1;
}
});
});
process();
return retVal;
};

BlackBerrySimulator.prototype.isEmulatorRunning = function() {
Expand All @@ -71,17 +95,16 @@ BlackBerrySimulator.prototype.run = function(readLineCb) {

var emulatorRunning = this.isEmulatorRunning();
if (emulatorRunning || this.device.indexOf('emulator') != 0) {
// TODO Mac
blackberrySimulatorProcess = this.createDeviceManagementProcess('log');
this.testHarnessRunning = this.isTestHarnessRunning();
} else {
// launch the (si|e)mulator async
// TODO Mac
}

/*
TODO Mac
blackberrySimulatorProcess.setOnReadLine(readLineCb);
blackberrySimulatorProcess.launch();
*/

if (this.device == 'emulator' && !emulatorRunning) {
// TODO Mac
Expand All @@ -101,30 +124,35 @@ BlackBerrySimulator.prototype.removeTestJS = function(testScript) {
};

BlackBerrySimulator.prototype.fillTestTemplateData = function(data) {
// TODO Mac
ti.api.error("Not implemented: fillTestTemplateData");
// nothing to do
}

BlackBerrySimulator.prototype.pushTestJS = function(testScript) {
var tempDir = ti.fs.createTempDirectory();
var testJS = ti.fs.getFile(tempDir.nativePath(), "test.js");
var stream = testJS.open(ti.fs.MODE_WRITE);
stream.write(testScript);
stream.close();
var testJSFile = ti.fs.createTempFile();
testJSFile.write(testScript);
var command = 'putFile';
// TODO Mac: check
var deviceFile = 'app/native/assets/test.js';
var commandArgs = [testJSFile.nativePath(), deviceFile];
var process = this.createDeviceManagementProcess(command, commandArgs);

// TODO Mac
var self = this;
process.setOnReadLine(function(data) {
var lines = data.split("\n");
lines.forEach(function(line) {
self.drillbit.frontendDo('process_data', line);
});
});
process.setOnExit(function(e) {
if (process.getExitCode() !== 0) {
ti.api.error("[BlackBerry] pushTestJS failed");
}
});
process();
};

BlackBerrySimulator.prototype.stageSDK = function(sdkTimestamp) {
// TODO Mac
ti.api.error("Not implemented: stageSDK");
return null;
};

BlackBerrySimulator.prototype.testHarnessNeedsBuild = function(stagedFiles) {
// TODO Mac
ti.api.error("Not implemented: testHarnessNeedsBuild");
return false;
return [];
};

BlackBerrySimulator.prototype.installTestHarness = function(launch, suite) {
Expand All @@ -137,8 +165,7 @@ BlackBerrySimulator.prototype.installTestHarness = function(launch, suite) {
BlackBerrySimulator.prototype.launchTestHarness = function(suite) {
// TODO Mac: need to separate install from launch
var command = 'run';
var commandArgs = [];
var process = this.createTestHarnessBuilderProcess(command, commandArgs);
var process = this.createTestHarnessBuilderProcess(command);

var self = this;
process.setOnReadLine(function(data) {
Expand All @@ -159,8 +186,18 @@ BlackBerrySimulator.prototype.launchTestHarness = function(suite) {
};

BlackBerrySimulator.prototype.killTestHarness = function() {
// TODO Mac
ti.api.error("Not implemented: killTestHarness");
var retVal = false;
var command = 'terminateApp';
var process = this.createDeviceManagementProcess(command);

var self = this;
process.setOnReadLine(function(data) {
var lines = data.split("\n");
lines.forEach(function(line) {
self.drillbit.frontendDo('process_data', line);
});
});
process();
this.testHarnessRunning = false;
};

Expand All @@ -170,11 +207,9 @@ BlackBerrySimulator.prototype.runTestHarness = function(suite, stagedFiles) {
// FIXME: for now just force it
forceBuild = true;

if (!this.testHarnessRunning || this.needsBuild || this.testHarnessNeedsBuild(stagedFiles) || forceBuild) {
if (!this.testHarnessRunning || this.needsBuild || forceBuild) {
var command = 'build';
var commandArgs = [];

var process = this.createTestHarnessBuilderProcess(command, commandArgs);
var process = this.createTestHarnessBuilderProcess(command);
this.drillbit.frontendDo('building_test_harness', suite.name, 'blackberry');

var self = this;
Expand Down
82 changes: 50 additions & 32 deletions support/blackberry/blackberryndk.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class Device:

class BlackberryNDK:
def __init__(self, blackberryNdk, log = None):
if platform.system() == 'Windows':
self.packagerProgram = 'blackberry-nativepackager.bat'
self.deployProgram = 'blackberry-deploy.bat'
else:
self.packagerProgram = 'blackberry-nativepackager'
self.deployProgram = 'blackberry-deploy'

self.log = log
self.blackberryNdk = self._findNdk(blackberryNdk)
if self.blackberryNdk is None:
Expand Down Expand Up @@ -119,22 +126,26 @@ def _findQde(self):
return qde
return None

def _run(self, command):
def _run(self, command, echoCommand = True):
assert type(command) is list
try:
# if no log, don't output
if self.log == None:
if platform.system() == 'Windows':
logfile = 'NUL'
else:
logfile = '/dev/null'
logfile = os.devnull
else:
logfile = self.log.getLogfile()
self.log.info('Command: ' + ' '.join(command))
with open(logfile, 'a') as f:
# Need this write() or else subprocess will overwrite for some reason
f.write('\n')
subprocess.check_call(command, stdout = f, stderr = f)
if echoCommand:
self.log.info('Command: ' + ' '.join(command))
# if no logfile, output to stdout
if logfile == None:
subprocess.check_call(command)
return 0
else:
with open(logfile, 'a') as f:
# Need this write() or else subprocess will overwrite for some reason
f.write('\n')
subprocess.check_call(command, stdout = f, stderr = f)
return 0
except subprocess.CalledProcessError, cpe:
print >>sys.stderr, cpe, cpe.output
return cpe.returncode
Expand All @@ -161,7 +172,7 @@ def build(self, project, cpu):
os.chdir(oldPath)
return retCode

def package(self, package, appFile, projectName):
def package(self, package, appFile, projectName, type):
# TODO Mac: Copy all needed resources to assets (images, sounds,
# etc.). For now copy app.js and content of Resources/blackberry to assets
buildDir = os.path.abspath(os.path.join(appFile, '..', '..', '..'))
Expand All @@ -172,19 +183,29 @@ def package(self, package, appFile, projectName):
shutil.copytree(os.path.join(projectDir, 'Resources', 'blackberry'), assetsDir)
shutil.copy2(os.path.join(projectDir, 'Resources', 'app.js'), assetsDir)

if platform.system() == 'Windows':
packager = 'blackberry-nativepackager.bat'
else:
packager = 'blackberry-nativepackager'
command = [packager, '-package', package, 'bar-descriptor.xml', '-e', appFile, projectName, 'assets']
command = [self.packagerProgram, '-package', package, 'bar-descriptor.xml', '-e', appFile, projectName, 'assets']
if type != 'deploy':
command.append('-devMode')
return self._run(command)

def deploy(self, deviceIP, package):
if platform.system() == 'Windows':
deploy = 'blackberry-deploy.bat'
else:
deploy = 'blackberry-deploy'
command = [deploy, '-installApp', '-launchApp', '-device', deviceIP, '-package', package]
command = [self.deployProgram, '-installApp', '-launchApp', '-device', deviceIP, '-package', package]
return self._run(command)

def terminateApp(self, deviceIP, package):
command = [self.deployProgram, '-terminateApp', '-device', deviceIP, '-package', package]
return self._run(command)

def isAppRunning(self, deviceIP, package):
command = [self.deployProgram, '-isAppRunning', '-device', deviceIP, '-package', package]
return self._run(command, echoCommand = False)

def getFile(self, deviceIP, package, hostFile, deviceFile):
command = [self.deployProgram, '-getFile', deviceFile, hostFile, '-device', deviceIP, '-package', package]
return self._run(command)

def putFile(self, deviceIP, package, hostFile, deviceFile):
command = [self.deployProgram, '-putFile', hostFile, deviceFile, '-device', deviceIP, '-package', package]
return self._run(command)

def buildTibb(self, tibbPath, buildType):
Expand Down Expand Up @@ -246,9 +267,10 @@ def __runUnitTests():
passed = os.path.exists(os.path.join(workspace, '.metadata'))
assert passed

with UnitTest('Test build project (Simulator)..'):
variant = 'Simulator-Debug'
ndk.build(project, variant)
with UnitTest('Test build project (x86)..'):
cpu = 'x86'
ndk.build(project, cpu)
assert os.path.exists(os.path.join(project, 'x86', 'o', projectName))
assert os.path.exists(os.path.join(project, 'x86', 'o-g', projectName))

# TODO Mac: Complete the following unit tests
Expand All @@ -261,15 +283,11 @@ def __runUnitTests():
# with UnitTest('Test deploy project to simulator (hard-coded ip)..'):
# ndk.deploy('192.168.135.129', )
#
with UnitTest('Test build project (Device-Debug)..'):
variant = 'Device-Debug'
ndk.build(project, variant)
assert os.path.exists(os.path.join(project, 'arm', 'o.le-v7-g', projectName))

with UnitTest('Test build project (Device-Release)..'):
variant = 'Device-Release'
ndk.build(project, variant)
with UnitTest('Test build project (arm)..'):
cpu = 'arm'
ndk.build(project, cpu)
assert os.path.exists(os.path.join(project, 'arm', 'o.le-v7', projectName))
assert os.path.exists(os.path.join(project, 'arm', 'o.le-v7-g', projectName))

shutil.rmtree(workspace)

Expand Down
7 changes: 4 additions & 3 deletions support/blackberry/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
from blackberryndk import BlackberryNDK

class Builder(object):
_type2variantCpu = {'simulator' : ('o-g', 'x86'),
type2variantCpu = {'simulator' : ('o-g', 'x86'),
'device' : ('o.le-v7-g', 'arm'),
'deploy' : ('o.le-v7', 'arm')}

def __init__(self, project_dir, type, ndk):
self.top_dir = project_dir.rstrip(os.sep)
(self.variant, self.cpu) = Builder._type2variantCpu[type]
self.type = type
(self.variant, self.cpu) = Builder.type2variantCpu[type]
self.ndk = ndk
project_tiappxml = os.path.join(self.top_dir, 'tiapp.xml')
tiappxml = TiAppXML(project_tiappxml)
Expand All @@ -52,7 +53,7 @@ def run(self):
# TODO Mac: See if we can reasonably launch the simulator from here and fetch the ip address
barPath = os.path.join(self.buildDir, self.cpu, self.variant, '%s.bar' % self.name)
savePath = os.path.join(self.buildDir, self.cpu, self.variant, self.name)
retCode = self.ndk.package(barPath, savePath, self.name)
retCode = self.ndk.package(barPath, savePath, self.name, self.type)
if retCode != 0:
return retCode
retCode = self.ndk.deploy('192.168.226.132', barPath)
Expand Down

0 comments on commit 77f29b7

Please sign in to comment.