From c248744c70d4df21171c2cd33b7888efdb16a383 Mon Sep 17 00:00:00 2001 From: shmyga Date: Fri, 23 Aug 2019 14:37:36 +0300 Subject: [PATCH] [android] build, run and logcat --- haxetool/android.js | 42 ++++++++++++++++++++++++++-------- haxetool/project.js | 10 +++++---- haxetool/sdk.js | 55 ++++++++++++++++++++++++++------------------- 3 files changed, 71 insertions(+), 36 deletions(-) diff --git a/haxetool/android.js b/haxetool/android.js index f8c0400..30fc735 100644 --- a/haxetool/android.js +++ b/haxetool/android.js @@ -1,6 +1,9 @@ const fs = require('fs'); const path = require('path'); const exec = require('./exec'); +const run = require('../run/index'); +const Command = require('../run/command'); + const through = require('through2'); const Sdk = require('./sdk'); const Env = require('./env'); @@ -10,12 +13,12 @@ class Android extends Sdk { constructor(version) { super(Android.ID, version || Android.VERSION); - this.platform = 19; + this.ndk_version = 'r15c'; } get prepared() { try { - return fs.existsSync(`${this.path}/platform-tools/adb`); + return fs.existsSync(`${this.path}/tools/bin/sdkmanager`); } catch (e) { return false; } @@ -49,9 +52,10 @@ class Android extends Sdk { } prepare_ndk() { - const url = 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip'; - // ToDo: download and extract to `this.ndk_home` - return Promise.resolve(); + // ToDo: support windows + const url = `https://dl.google.com/android/repository/android-ndk-${this.ndk_version}-linux-x86_64.zip`; + this.log.d('download: *%s*', url); + return Sdk.Downloader.download(url, this.ndk_home); } sdkmanager(packages) { @@ -69,14 +73,18 @@ class Android extends Sdk { Env.set('NDK_HOME', this.ndk_home); } + get adbBin() { + return path.join(this.path, 'platform-tools/adb'); + } + adb(args) { - const adbBin = path.join(this.path, 'platform-tools/adb'); - return exec('.', [adbBin].concat(args).join(' ')).then(data => { + return exec('.', [this.adbBin].concat(args).join(' ')).then(data => { for (let line of data.stderr.split('\n')) { if (line.indexOf('Error') > -1) { throw line; } } + return data; }); } @@ -128,9 +136,25 @@ class Android extends Sdk { start() { const self = this; - return through.obj((file, enc, callback) => { + return through.obj(function(file, enc, callback) { const name = `${file.package}/${file.activity}`; - self.adb(['shell', 'am', 'start', '-n', name]).then(() => callback()); + self.adb(['shell', 'am', 'start', '-n', name]).then(() => { + setTimeout(() => { + self.adb(['shell', `"ps | grep ${file.package}"`]).then(result => { + file.pid = result.stdout.split(/\s+/)[1]; + this.push(file); + callback(); + }); + }, 1000); + }); + }); + } + + logcat() { + const self = this; + return through.obj(function(file, enc, callback) { + const cmd = `${self.adbBin} logcat --pid ${file.pid}`; + this.push(new Command(cmd).exec()); }); } } diff --git a/haxetool/project.js b/haxetool/project.js index dca4d13..28b156d 100644 --- a/haxetool/project.js +++ b/haxetool/project.js @@ -96,7 +96,8 @@ class HaxeBuilder extends Builder { 'tools', 'platform-tools', 'build-tools;27.0.3', - 'platforms;android-19', + //'platforms;android-19', + 'platforms;android-26', //'ndk-bundle', 'lldb;3.1', 'cmake;3.6.4111459', @@ -353,11 +354,12 @@ class AndroidRunner extends Runner { call() { const target = this.targetPath; const filename = path.resolve(target, this.config.meta.filename+'-debug.apk'); - console.log(filename); - return gulp.src(filename) + return this.log(gulp.src(filename) .pipe(this.android.apk()) .pipe(this.android.install()) - .pipe(this.android.start()); + .pipe(this.android.start()) + .pipe(this.android.logcat()) + ); } } diff --git a/haxetool/sdk.js b/haxetool/sdk.js index f6b926e..0b4e36f 100755 --- a/haxetool/sdk.js +++ b/haxetool/sdk.js @@ -35,6 +35,36 @@ class System { } +class Downloader { + + static download(url, dest, strip = 1) { + fse.ensureDirSync(dest); + const bar = new ProgressBar(`download [:bar] :percent :etas`, {width: 40, total: 1000, clear: true}); + let stream = got.stream(url); + stream = stream.on('downloadProgress', (p) => bar.update(p.percent)); + if (url.endsWith('.zip')) { + stream = stream.pipe(unzip.Parse()).on('entry', (entry) => { + const filePath = entry.path.split('/').slice(strip).join(path.sep); + if (filePath.length > 0) { + if (entry.type === 'Directory') { + fse.ensureDirSync(path.join(dest, path.normalize(filePath))); + } else if (entry.type === 'File') { + entry.pipe(fs.createWriteStream(path.join(dest, path.normalize(filePath)))); + } + } else { + entry.autodrain(); + } + }); + } else if (url.endsWith('tar.gz')) { + stream = stream.pipe(tar.x({C: dest, strip: strip})); + } else { + stream = stream.pipe(fs.createWriteStream(path.join(dest, url.split('/').pop()))); + } + return ps.wait(stream); + } +} + + class Sdk { static set dir(value) { Sdk._dir = value @@ -79,34 +109,13 @@ class Sdk { if (this.prepared) { return Promise.resolve(); } else { - fse.ensureDirSync(this.path); - const bar = new ProgressBar(`${this.tag} [:bar] :percent :etas`, {width: 40, total: 1000, clear: true}); this.log.d('download: *%s*', this.link); - let stream = got.stream(this.link); - stream = stream.on('downloadProgress', (p) => bar.update(p.percent)); - if (this.link.endsWith('.zip')) { - stream = stream.pipe(unzip.Parse()).on('entry', (entry) => { - const filePath = entry.path.split('/').slice(strip).join(path.sep); - if (filePath.length > 0) { - if (entry.type === 'Directory') { - fse.ensureDirSync(path.join(this.path, path.normalize(filePath))); - } else if (entry.type === 'File') { - entry.pipe(fs.createWriteStream(path.join(this.path, path.normalize(filePath)))); - } - } else { - entry.autodrain(); - } - }); - } else if (this.link.endsWith('tar.gz')) { - stream = stream.pipe(tar.x({C: this.path, strip: strip})); - } else { - stream = stream.pipe(fs.createWriteStream(path.join(this.path, this.link.split('/').pop()))); - } - return ps.wait(stream); + return Downloader.download(this.link, this.path, strip); } } } +Sdk.Downloader = Downloader; Sdk.System = System; module.exports = Sdk;