Separate device functionality into plugins.

This commit is contained in:
Simo Kinnunen
2014-03-14 20:18:53 +09:00
parent d61a70fda5
commit 3d20b06f7f
24 changed files with 1277 additions and 700 deletions

View File

@@ -0,0 +1,83 @@
var util = require('util')
var syrup = require('syrup')
var ProtoBuf = require('protobufjs')
var semver = require('semver')
var pathutil = require('../../../util/pathutil')
var streamutil = require('../../../util/streamutil')
var logger = require('../../../util/logger')
module.exports = syrup()
.dependency(require('../plugins/adb'))
.define(function(options, adb) {
var log = logger.createLogger('device:resources:inputagent')
var resource = {
requiredVersion: '~0.1.2'
, pkg: 'jp.co.cyberagent.stf.input.agent'
, main: 'jp.co.cyberagent.stf.input.agent.InputAgent'
, apk: pathutil.vendor('InputAgent/InputAgent.apk')
, proto: ProtoBuf.loadProtoFile(
pathutil.vendor('InputAgent/proto/agent.proto')
).build().jp.co.cyberagent.stf.input.agent.proto
}
function getPath() {
return adb.shell(options.serial, ['pm', 'path', resource.pkg])
.then(function(out) {
return streamutil.findLine(out, (/^package:/))
.then(function(line) {
return line.substr(8)
})
})
}
function install() {
log.info('Checking whether we need to install InputAgent.apk')
return getPath()
.then(function(installedPath) {
log.info('Running version check')
return adb.shell(options.serial, util.format(
"export CLASSPATH='%s';"
+ " exec app_process /system/bin '%s' --version"
, installedPath
, resource.main
))
.then(function(out) {
return streamutil.readAll(out)
.timeout(10000)
.then(function(buffer) {
var version = buffer.toString()
if (semver.satisfies(version, resource.requiredVersion)) {
return installedPath
}
else {
throw new Error(util.format(
'Incompatible version %s'
, version
))
}
})
})
})
.catch(function(err) {
log.info('Installing InputAgent.apk')
return adb.install(options.serial, resource.apk)
.then(function() {
return getPath()
})
})
}
return install()
.then(function(path) {
log.info('InputAgent.apk up to date')
return {
path: path
, pkg: resource.pkg
, main: resource.main
, proto: resource.proto
}
})
})

View File

@@ -0,0 +1,85 @@
var util = require('util')
var Promise = require('bluebird')
var syrup = require('syrup')
var logger = require('../../../util/logger')
var pathutil = require('../../../util/pathutil')
var devutil = require('../../../util/devutil')
var streamutil = require('../../../util/streamutil')
module.exports = syrup()
.dependency(require('../plugins/adb'))
.dependency(require('../plugins/identity'))
.define(function(options, adb, identity) {
var log = logger.createLogger('device:resources:remote')
var resources = {
bin: {
src: pathutil.vendor(util.format(
'remote/libs/%s/remote', identity.abi))
, dest: '/data/local/tmp/remote'
, comm: 'remote'
, mode: 0755
}
, lib: {
src: pathutil.vendor(util.format(
'remote/external/android-%d/remote_external.so', identity.sdk))
, dest: '/data/local/tmp/remote_external.so'
, mode: 0755
}
}
function installResource(res) {
return adb.push(options.serial, res.src, res.dest, res.mode)
.then(function(transfer) {
return new Promise(function(resolve, reject) {
transfer.on('error', reject)
transfer.on('end', resolve)
})
})
.return(res)
}
function ensureNotBusy(res) {
return adb.shell(options.serial, [res.dest, '--help'])
.then(function(out) {
return streamutil.findLine(out, (/text file busy/i))
.then(function(line) {
log.info('Binary is busy, will retry')
return Promise.delay(100)
})
.then(function() {
return ensureNotBusy(res)
})
.catch(streamutil.NoSuchLineError, function() {
return res
})
})
}
function installAll() {
return Promise.all([
installResource(resources.bin).then(ensureNotBusy)
, installResource(resources.lib)
])
}
function stop() {
return devutil.killProcsByComm(
adb
, options.serial
, resources.bin.comm
, resources.bin.dest
)
}
return stop()
.then(installAll)
.then(function() {
return {
bin: resources.bin.dest
, lib: resources.lib.dest
}
})
})