From e23fc698a260d0c64c6c443b2c51fe28340c715c Mon Sep 17 00:00:00 2001 From: adelphes Date: Wed, 1 Feb 2017 11:04:40 +0000 Subject: [PATCH] auto-start ADB if it's not running --- src/adbclient.js | 23 +++++++++++++++++++++++ src/debugMain.js | 17 ++++++++++++++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/src/adbclient.js b/src/adbclient.js index f2202d9..76b7277 100644 --- a/src/adbclient.js +++ b/src/adbclient.js @@ -87,6 +87,29 @@ ADBClient.prototype = { }); }, + test_adb_connection : function(o) { + var x = {o:o||{},deferred:$.Deferred()}; + this.proxy_connect() + .then(function() { + return this.dexcmd('cn'); + }) + .then(function(data) { + this.fd = data; + return this.dexcmd('dc', this.fd); + }) + .then(function() { + return this.proxy_disconnect(); + }) + .then(function() { + x.deferred.resolveWith(x.o.ths||this, [null, x.o.extra]); + }) + .fail(function(err) { + // if we fail, still resolve the deferred, passing the error + x.deferred.resolveWith(x.o.ths||this, [err, x.o.extra]); + }); + return x.deferred; + }, + list_devices : function(o) { var x = {o:o||{},deferred:$.Deferred()}; this.proxy_connect() diff --git a/src/debugMain.js b/src/debugMain.js index 9e2f287..2dbc794 100644 --- a/src/debugMain.js +++ b/src/debugMain.js @@ -307,8 +307,23 @@ class AndroidDebugSession extends DebugSession { if (!launchActivity) if (!(launchActivity = this.apk_file_info.launcher)) return fail_launch('No valid launch activity found in AndroidManifest.xml or launch.json'); - return this.findSuitableDevice(args.targetDevice); + + return new ADBClient().test_adb_connection() + .then(err => { + // if adb is not running, see if we can start it ourselves using ANDROID_HOME (and a sensible port number) + var adbport = ws_proxy.adbport; + if (err && process.env.ANDROID_HOME && typeof adbport === 'number' && adbport > 0 && adbport < 65536) { + var adbpath = path.join(process.env.ANDROID_HOME, 'platform-tools', /^win/.test(process.platform)?'adb.exe':'adb'); + var adbargs = ['-P',''+adbport,'start-server']; + try { + this.LOG([adbpath, ...adbargs].join(' ')); + var stdout = require('child_process').execFileSync(adbpath, adbargs, {cwd:process.env.ANDROID_HOME, encoding:'utf8'}); + this.LOG(stdout); + } catch (ex) {} // if we fail, it doesn't matter - the device query will fail and the user will have to work it out themselves + } + }) }) + .then(() => this.findSuitableDevice(args.targetDevice)) .then(device => { this._device = device; this._device.adbclient = new ADBClient(this._device.serial);