265 lines
7.2 KiB
TypeScript
Executable File
265 lines
7.2 KiB
TypeScript
Executable File
import fs from "fs";
|
|
import os from "os";
|
|
import path from "path";
|
|
import through from "through2";
|
|
import env from "../common/env";
|
|
import exec from "../common/exec";
|
|
import { HandlerWriter } from "../common/tail";
|
|
import { Sdk } from "../core/sdk";
|
|
import { Command } from "../run/command";
|
|
|
|
export class AndroidNdk extends Sdk {
|
|
static ID = "android-ndk";
|
|
|
|
static VERSION_R15C = "r15c";
|
|
static VERSION_R21E = "r21e";
|
|
static VERSION = AndroidNdk.VERSION_R21E;
|
|
|
|
url = "https://dl.google.com/android/repository/android-ndk-<%= version %>-<%= os_name_long %>-x86_64.zip";
|
|
|
|
constructor(version?: string) {
|
|
super(AndroidNdk.ID, version || AndroidNdk.VERSION);
|
|
}
|
|
|
|
get prepared(): boolean {
|
|
try {
|
|
return fs.existsSync(`${this.path}/build/ndk-build`);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
export class Android extends Sdk {
|
|
static ID = "android";
|
|
|
|
static VERSION_7302050 = "7302050";
|
|
static VERSION = Android.VERSION_7302050;
|
|
|
|
private ndk: AndroidNdk;
|
|
|
|
url = "https://dl.google.com/android/repository/commandlinetools-<%= os_name %>-<%= version %>_latest.zip";
|
|
|
|
protected strip = 0;
|
|
|
|
constructor(version?: string, ndkVersion?: string) {
|
|
super(Android.ID, version || Android.VERSION);
|
|
this.ndk = new AndroidNdk(ndkVersion);
|
|
}
|
|
|
|
get prepared(): boolean {
|
|
try {
|
|
return fs.existsSync(this.sdkmanager_bin);
|
|
} catch (e) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
get android_home(): string {
|
|
return this.path;
|
|
}
|
|
|
|
get ndk_home(): string {
|
|
return this.ndk.path;
|
|
}
|
|
|
|
get sdkmanager_bin(): string {
|
|
return path.join(this.path, "cmdline-tools/bin/sdkmanager");
|
|
}
|
|
|
|
prepare(): Promise<any> {
|
|
const result = [];
|
|
result.push(super.prepare());
|
|
result.push(this.ndk.prepare());
|
|
return Promise.all(result);
|
|
}
|
|
|
|
sdkmanager(packages: string[]) {
|
|
this.log.i("sdkmanager: *%s*", packages.join(", "));
|
|
const installedFile = path.join(this.path, ".installed");
|
|
const installed = new Set(
|
|
fs.existsSync(installedFile) ? fs.readFileSync(installedFile, { encoding: "utf8" }).split("\n") : [],
|
|
);
|
|
const install = new Set(packages);
|
|
for (const key of installed) {
|
|
if (install.has(key)) {
|
|
install.delete(key);
|
|
}
|
|
}
|
|
if (install.size === 0) {
|
|
return Promise.resolve();
|
|
}
|
|
const sdkmanagerBin = this.sdkmanager_bin;
|
|
if (fs.existsSync(sdkmanagerBin)) {
|
|
fs.chmodSync(sdkmanagerBin, 0o755);
|
|
}
|
|
const yes = '(while sleep 3; do echo "y"; done)';
|
|
const command = [yes, "|", sdkmanagerBin]
|
|
.concat(Array.from(install).map((name) => `"${name}"`))
|
|
.concat([`--sdk_root="${this.path}"`])
|
|
.join(" ");
|
|
return exec(".", command).then(() => {
|
|
for (const key of install) {
|
|
installed.add(key);
|
|
}
|
|
fs.writeFileSync(installedFile, Array.from(installed).join("\n"), { encoding: "utf8" });
|
|
});
|
|
}
|
|
|
|
activate() {
|
|
env.set("ANDROID_HOME", this.android_home);
|
|
env.set("ANDROID_NDK_HOME", this.ndk_home);
|
|
env.set("NDK_HOME", this.ndk_home);
|
|
}
|
|
|
|
get adbBin(): string {
|
|
return path.join(this.path, "platform-tools/adb");
|
|
}
|
|
|
|
adb(args: any) {
|
|
return exec(".", [this.adbBin].concat(args).join(" ")).then((data: any) => {
|
|
for (let line of data.stderr.split("\n")) {
|
|
if (line.indexOf("Error") > -1) {
|
|
throw line;
|
|
}
|
|
}
|
|
return data;
|
|
});
|
|
}
|
|
|
|
buildTool(name: string) {
|
|
let buildToolsVersion = ""; // TODO: default build-tools version value
|
|
fs.readdirSync(path.join(this.path, "build-tools")).forEach((file) => {
|
|
buildToolsVersion = file;
|
|
});
|
|
return path.join(this.path, "build-tools", buildToolsVersion, name);
|
|
}
|
|
|
|
aapt(args: any) {
|
|
const aaptBin = this.buildTool("aapt");
|
|
return exec(".", [aaptBin].concat(args).join(" "));
|
|
}
|
|
|
|
apk() {
|
|
const self = this;
|
|
return through.obj(function (file: any, enc: string, callback: any) {
|
|
self.aapt(["l", "-a", file.path]).then((data: any) => {
|
|
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();
|
|
});
|
|
});
|
|
}
|
|
|
|
_installApk(filename: string, pack: string) {
|
|
this.log.i("install *%s*", filename);
|
|
return this.adb(["install", "-r", "-d", filename])
|
|
.then(() => {
|
|
return false;
|
|
})
|
|
.catch((error) => {
|
|
if (error.includes("INSTALL_FAILED_UPDATE_INCOMPATIBLE")) {
|
|
this.log.w("!INSTALL FAILED UPDATE INCOMPATIBLE!");
|
|
return true;
|
|
} else {
|
|
throw error;
|
|
}
|
|
})
|
|
.then((reinstall) => {
|
|
if (reinstall) {
|
|
this.log.i("uninstall *%s*", pack);
|
|
return this.adb(["uninstall", pack]).then(() => {
|
|
this.log.i("install *%s*", filename);
|
|
return this.adb(["install", "-r", "-d", filename]);
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
install() {
|
|
const self = this;
|
|
return through.obj(function (file: any, enc: string, callback: any) {
|
|
self._installApk(file.path, file.package).then(() => {
|
|
// ToDo: kludge: delay before start
|
|
setTimeout(() => {
|
|
this.push(file);
|
|
callback();
|
|
}, 500);
|
|
});
|
|
});
|
|
}
|
|
|
|
start() {
|
|
const self = this;
|
|
return through.obj(function (file: any, enc: string, callback: any) {
|
|
const name = `${file.package}/${file.activity}`;
|
|
self.log.i("start *%s*", name);
|
|
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: any, enc: string, callback: any) {
|
|
const cmd = `${self.adbBin} logcat --pid ${file.pid}`;
|
|
const command = new Command(cmd).exec();
|
|
this.push(command);
|
|
command.contents.pipe(
|
|
new HandlerWriter((line: string) => {
|
|
if (line.includes("SDL") && (line.includes("onDestroy()") || line.includes("onStop()"))) {
|
|
callback();
|
|
throw "Exit"; // ToDo:
|
|
}
|
|
}),
|
|
);
|
|
});
|
|
}
|
|
|
|
sign(keystore: string, keypass: string) {
|
|
const self = this;
|
|
return through.obj(function (file: any, enc: string, callback: any) {
|
|
self.log.i("sign *%s*", file.path);
|
|
const filename = path.join(os.tmpdir(), "tmp.apk");
|
|
fs.writeFileSync(filename, file.contents);
|
|
const cmd = [
|
|
self.buildTool("apksigner"),
|
|
"sign",
|
|
"--ks",
|
|
keystore,
|
|
"--ks-pass",
|
|
`pass:${keypass}`,
|
|
filename,
|
|
].join(" ");
|
|
exec(".", cmd).then(() => {
|
|
file.contents = fs.readFileSync(filename);
|
|
this.push(file);
|
|
callback();
|
|
});
|
|
});
|
|
}
|
|
}
|