-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsdk-manager.ts
87 lines (74 loc) · 3.06 KB
/
sdk-manager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import * as core from "@actions/core";
import * as exec from "@actions/exec";
import * as fs from "fs";
import path from "path";
import { parseSDKManagerOutput, AndroidPackageInfo } from "./sdk-manager-parser";
import { splitByEOL } from "./utils";
export class SDKManager {
private sdkManagerPath: string;
constructor(private androidHome: string) {
this.sdkManagerPath = path.join(androidHome, "tools", "bin", "sdkmanager");
}
public async install(packageInfo: AndroidPackageInfo): Promise<void> {
core.startGroup("Trying to download package via sdkmanager...");
await this.run([packageInfo.name], true);
core.endGroup();
if (!this.isPackageInstalled(packageInfo)) {
const localPackagePath = this.getPackagePath(packageInfo);
throw new Error(`Package '${packageInfo.name}' was not installed properly. '${localPackagePath}' folder is empty and doesn't exist`);
}
}
public async getAllPackagesInfo(): Promise<AndroidPackageInfo[]> {
const stdout = await this.run(["--list"], false);
const parsedPackages = parseSDKManagerOutput(stdout);
if (core.isDebug()) {
core.debug("Available packages:");
parsedPackages.forEach(p => core.debug(JSON.stringify(p)));
}
return parsedPackages;
}
public getPackagePath(packageInfo: AndroidPackageInfo): string {
const relativePath = packageInfo.name.replace(/;/g, "/");
return path.join(this.androidHome, relativePath);
}
public isPackageInstalled(packageInfo: AndroidPackageInfo): boolean {
const packagePath = this.getPackagePath(packageInfo);
if (!fs.existsSync(packagePath)) {
return false;
}
return fs.readdirSync(packagePath).length > 0;
}
private async run(args: string[], printOutputInDebug: boolean): Promise<string> {
let stdout = "";
let previousPrintedLine = "";
const outputListener = (data: Buffer): void => {
const line = data.toString();
stdout += line;
if (printOutputInDebug) {
splitByEOL(line).map(s => s.trim()).filter(Boolean).forEach(s => {
if (previousPrintedLine !== s) {
core.debug(s);
previousPrintedLine = s;
}
});
}
};
const options: exec.ExecOptions = {
silent: true,
ignoreReturnCode: true,
failOnStdErr: false,
listeners: {
stdout: outputListener,
stderr: outputListener,
},
input: Buffer.from("y") // accept license
};
const commandString = `${this.sdkManagerPath} ${args.join(" ")}`;
console.log(`[command]${commandString}`);
const exitCode = await exec.exec(`"${this.sdkManagerPath}"`, args, options);
if (exitCode !== 0) {
throw new Error(`'${commandString}' has finished with exit code '${exitCode}'`);
}
return stdout;
}
}