Files
gulp-haxetool/haxetool/android.js
2018-05-08 18:05:29 +03:00

128 lines
3.8 KiB
JavaScript

const fs = require('fs');
const exec = require('./exec');
const through = require('through2');
const Sdk = require('./sdk');
const Env = require('./env');
class Android extends Sdk {
constructor(version) {
super(Android.ID, version || Android.VERSION);
}
get prepared() {
try {
return fs.existsSync(`${this.path}/platform-tools/adb`);
} catch (e) {
return false;
}
}
get link() {
const system = Sdk.System.isWindows ? 'windows' : Sdk.System.isLinux ? 'linux' : null;
if (!system) throw 'Unsupported system';
//return `https://dl.google.com/android/repository/sdk-tools-${system}-${this.version}.zip`;
return `https://dl.google.com/android/repository/tools_r${this.version}-${system}.zip`;
}
prepare() {
return this.prepared ? Promise.resolve() : super.prepare(0).then(() => {
return this.sdkmanager([
'ndk-bundle',
'tools',
'platform-tools',
'build-tools;27.0.3',
'platforms;android-27',
]);
});
}
sdkmanager(packages) {
const androidBin = `${this.path}/tools/bin/sdkmanager`;
if (fs.existsSync(androidBin)) {
fs.chmodSync(androidBin, 0o755);
}
const yes = '(while sleep 3; do echo "y"; done)';
return exec('.', [yes, '|', androidBin].concat(packages.map(name => `"${name}"`)).join(' '));
}
activate() {
Env.set('ANDROID_HOME', this.path);
}
adb(args) {
const adbBin = `${this.path}/platform-tools/adb`;
return exec('.', [adbBin].concat(args).join(' ')).then(data => {
for (let line of data.stderr.split('\n')) {
if (line.indexOf('Error') > -1) {
throw line;
}
}
});
}
aapt(args) {
let buildToolsVersion = null;
fs.readdirSync(`${this.path}/build-tools`).forEach(file => {
buildToolsVersion = file;
});
const aaptBin = `${this.path}/build-tools/${buildToolsVersion}/aapt`;
return exec('.', [aaptBin].concat(args).join(' '));
}
apk() {
const self = this;
return through.obj(function(file, enc, callback) {
self.aapt(['l', '-a', file.path]).then(data => {
let activity = false;
for (let line of data.stdout.split('\n')) {
if (line.indexOf('package') > -1) {
const value = /"(.*?)"/.exec(line);
if (value) file.package = value[1]
}
if (line.indexOf('activity') > -1) {
activity = true;
}
if (activity && line.indexOf('name') > -1) {
const value = /"(.*?)"/.exec(line);
if (value) {
file.activity = value[1];
activity = false;
}
}
}
this.push(file);
callback();
});
});
}
install() {
const self = this;
return through.obj(function(file, enc, callback) {
self.adb(['install', '-r', file.path]).then(() => {
this.push(file);
callback();
});
});
}
start() {
const self = this;
return through.obj((file, enc, callback) => {
const name = `${file.package}/${file.activity}`;
self.adb(['shell', 'am', 'start', '-n', name]).then(() => callback());
});
}
}
Android.ID = 'android';
Android.VERSION_25_2_3 = '25.2.3';
Android.VERSION_3859397 = '3859397';
Android.VERSION = Android.VERSION_25_2_3;
module.exports = Android;