[android] build, run and logcat

This commit is contained in:
2019-08-23 14:37:36 +03:00
parent 8205a4cb0c
commit c248744c70
3 changed files with 71 additions and 36 deletions

View File

@@ -1,6 +1,9 @@
const fs = require('fs'); const fs = require('fs');
const path = require('path'); const path = require('path');
const exec = require('./exec'); const exec = require('./exec');
const run = require('../run/index');
const Command = require('../run/command');
const through = require('through2'); const through = require('through2');
const Sdk = require('./sdk'); const Sdk = require('./sdk');
const Env = require('./env'); const Env = require('./env');
@@ -10,12 +13,12 @@ class Android extends Sdk {
constructor(version) { constructor(version) {
super(Android.ID, version || Android.VERSION); super(Android.ID, version || Android.VERSION);
this.platform = 19; this.ndk_version = 'r15c';
} }
get prepared() { get prepared() {
try { try {
return fs.existsSync(`${this.path}/platform-tools/adb`); return fs.existsSync(`${this.path}/tools/bin/sdkmanager`);
} catch (e) { } catch (e) {
return false; return false;
} }
@@ -49,9 +52,10 @@ class Android extends Sdk {
} }
prepare_ndk() { prepare_ndk() {
const url = 'https://dl.google.com/android/repository/android-ndk-r15c-linux-x86_64.zip'; // ToDo: support windows
// ToDo: download and extract to `this.ndk_home` const url = `https://dl.google.com/android/repository/android-ndk-${this.ndk_version}-linux-x86_64.zip`;
return Promise.resolve(); this.log.d('download: *%s*', url);
return Sdk.Downloader.download(url, this.ndk_home);
} }
sdkmanager(packages) { sdkmanager(packages) {
@@ -69,14 +73,18 @@ class Android extends Sdk {
Env.set('NDK_HOME', this.ndk_home); Env.set('NDK_HOME', this.ndk_home);
} }
get adbBin() {
return path.join(this.path, 'platform-tools/adb');
}
adb(args) { adb(args) {
const adbBin = path.join(this.path, 'platform-tools/adb'); return exec('.', [this.adbBin].concat(args).join(' ')).then(data => {
return exec('.', [adbBin].concat(args).join(' ')).then(data => {
for (let line of data.stderr.split('\n')) { for (let line of data.stderr.split('\n')) {
if (line.indexOf('Error') > -1) { if (line.indexOf('Error') > -1) {
throw line; throw line;
} }
} }
return data;
}); });
} }
@@ -128,9 +136,25 @@ class Android extends Sdk {
start() { start() {
const self = this; const self = this;
return through.obj((file, enc, callback) => { return through.obj(function(file, enc, callback) {
const name = `${file.package}/${file.activity}`; 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());
}); });
} }
} }

View File

@@ -96,7 +96,8 @@ class HaxeBuilder extends Builder {
'tools', 'tools',
'platform-tools', 'platform-tools',
'build-tools;27.0.3', 'build-tools;27.0.3',
'platforms;android-19', //'platforms;android-19',
'platforms;android-26',
//'ndk-bundle', //'ndk-bundle',
'lldb;3.1', 'lldb;3.1',
'cmake;3.6.4111459', 'cmake;3.6.4111459',
@@ -353,11 +354,12 @@ class AndroidRunner extends Runner {
call() { call() {
const target = this.targetPath; const target = this.targetPath;
const filename = path.resolve(target, this.config.meta.filename+'-debug.apk'); const filename = path.resolve(target, this.config.meta.filename+'-debug.apk');
console.log(filename); return this.log(gulp.src(filename)
return gulp.src(filename)
.pipe(this.android.apk()) .pipe(this.android.apk())
.pipe(this.android.install()) .pipe(this.android.install())
.pipe(this.android.start()); .pipe(this.android.start())
.pipe(this.android.logcat())
);
} }
} }

View File

@@ -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 { class Sdk {
static set dir(value) { static set dir(value) {
Sdk._dir = value Sdk._dir = value
@@ -79,34 +109,13 @@ class Sdk {
if (this.prepared) { if (this.prepared) {
return Promise.resolve(); return Promise.resolve();
} else { } 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); this.log.d('download: *%s*', this.link);
let stream = got.stream(this.link); return Downloader.download(this.link, this.path, strip);
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);
} }
} }
} }
Sdk.Downloader = Downloader;
Sdk.System = System; Sdk.System = System;
module.exports = Sdk; module.exports = Sdk;