-
-
Notifications
You must be signed in to change notification settings - Fork 207
/
Copy pathemulator-manager.js
108 lines (108 loc) · 4.56 KB
/
emulator-manager.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const exec = __importStar(require("@actions/exec"));
const EMULATOR_BOOT_TIMEOUT_SECONDS = 120;
const AVD_MANAGER_PATH = `${process.env.ANDROID_HOME}/tools/bin/avdmanager`;
const ADB_PATH = `${process.env.ANDROID_HOME}/platform-tools/adb`;
/**
* Creates and launches a new AVD instance with the specified configurations.
*/
function launchEmulator(apiLevel, target, arch, profile, headless, disableAnimations) {
return __awaiter(this, void 0, void 0, function* () {
// create a new AVD
if (profile.trim() !== '') {
console.log(`Creating AVD with custom profile ${profile}`);
yield exec.exec(`${AVD_MANAGER_PATH} create avd --force -n test --abi "${target}/${arch}" --package "system-images;android-${apiLevel};${target};${arch}" --device "${profile}"`);
}
else {
console.log(`Creating AVD without custom profile.`);
yield exec.exec(`bash -c \\"echo no | ${AVD_MANAGER_PATH} create avd --force -n test --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}'`);
}
// start emulator
console.log('Starting emulator.');
const noWindow = headless ? '-no-window' : '';
yield exec.exec(`bash -c \\"${process.env.ANDROID_HOME}/emulator/emulator -avd test ${noWindow} -no-snapshot -noaudio -no-boot-anim &"`);
// wait for emulator to complete booting
yield waitForDevice();
yield exec.exec(`${ADB_PATH} shell input keyevent 82`);
// disable animations
if (disableAnimations) {
console.log('Disabling animations.');
yield exec.exec(`${ADB_PATH} shell settings put global window_animation_scale 0.0`);
yield exec.exec(`${ADB_PATH} shell settings put global transition_animation_scale 0.0`);
yield exec.exec(`${ADB_PATH} shell settings put global animator_duration_scale 0.0`);
}
});
}
exports.launchEmulator = launchEmulator;
/**
* Kills the running emulator on the defaut port.
*/
function killEmulator() {
return __awaiter(this, void 0, void 0, function* () {
try {
yield exec.exec(`${ADB_PATH} -s emulator-5554 emu kill`);
}
catch (error) {
console.log('No emulator running on port 5554');
}
});
}
exports.killEmulator = killEmulator;
/**
* Wait for emulator to boot.
*/
function waitForDevice() {
return __awaiter(this, void 0, void 0, function* () {
const adbPath = `${process.env.ANDROID_HOME}/platform-tools/adb`;
let booted = false;
let attempts = 0;
const retryInterval = 2; // retry every 2 seconds
const maxAttemps = EMULATOR_BOOT_TIMEOUT_SECONDS / 2;
while (!booted) {
try {
let result = '';
yield exec.exec(`${adbPath} shell getprop sys.boot_completed`, [], {
listeners: {
stdout: (data) => {
result += data.toString();
}
}
});
if (result.trim() === '1') {
console.log('Emulator booted.');
booted = true;
break;
}
}
catch (error) {
console.error(error.message);
}
if (attempts < maxAttemps) {
yield delay(retryInterval * 1000);
}
else {
throw new Error(`Timeout waiting for emulator to boot.`);
}
attempts++;
}
});
}
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}