refactor: rewrite on typescript
This commit is contained in:
Executable
+257
@@ -0,0 +1,257 @@
|
||||
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 { System } from "../common/system";
|
||||
import { StringWritable } from "../common/tail";
|
||||
import { Downloader, Sdk } from "../core/sdk";
|
||||
import { Command } from "../run/command";
|
||||
|
||||
export class Android extends Sdk {
|
||||
static ID = "android";
|
||||
|
||||
static VERSION_7302050 = "7302050";
|
||||
static VERSION = Android.VERSION_7302050;
|
||||
|
||||
static NDK_VERSION_R15C = "r15c";
|
||||
static NDK_VERSION = Android.NDK_VERSION_R15C;
|
||||
|
||||
private ndk_version: string;
|
||||
private repository: string;
|
||||
|
||||
constructor(version?: string, ndkVersion?: string) {
|
||||
super(Android.ID, version || Android.VERSION);
|
||||
this.ndk_version = ndkVersion || Android.NDK_VERSION;
|
||||
this.repository = "https://dl.google.com/android/repository";
|
||||
}
|
||||
|
||||
get prepared(): boolean {
|
||||
try {
|
||||
return fs.existsSync(this.sdkmanager_bin);
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
get link(): string {
|
||||
return `${this.repository}/commandlinetools-${System.os}-${this.version}_latest.zip`;
|
||||
}
|
||||
|
||||
get android_home(): string {
|
||||
return this.path;
|
||||
}
|
||||
|
||||
get ndk_home(): string {
|
||||
return path.join(this.path, "ndk-bundle");
|
||||
}
|
||||
|
||||
get sdkmanager_bin(): string {
|
||||
return path.join(this.path, "cmdline-tools/bin/sdkmanager");
|
||||
}
|
||||
|
||||
prepare(): Promise<any> {
|
||||
const result = [];
|
||||
result.push(super.prepare(0));
|
||||
if (!fs.existsSync(path.join(this.ndk_home))) {
|
||||
result.push(this.prepare_ndk());
|
||||
}
|
||||
return Promise.all(result);
|
||||
}
|
||||
|
||||
prepare_ndk(): Promise<any> {
|
||||
// ToDo: support windows
|
||||
const url = `${this.repository}/android-ndk-${this.ndk_version}-${System.os}-x86_64.zip`;
|
||||
this.log.d("download: *%s*", url);
|
||||
return Downloader.download(url, this.ndk_home, 1, true);
|
||||
}
|
||||
|
||||
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 StringWritable((line) => {
|
||||
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();
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user