Add emulator-launcher.

This commit is contained in:
Yang Chen
2019-11-07 15:26:16 +11:00
parent f5529b9551
commit 34df1eaf0e
13 changed files with 247 additions and 38 deletions
+72 -2
View File
@@ -1,6 +1,76 @@
import * as exec from '@actions/exec';
const EMULATOR_BOOT_TIMEOUT_SECONDS = 120;
/**
* Creates and launches a new AVD instance with the specified configurations.
*/
export async function startEmulator(): Promise<void> {
// TODO
export async function launchEmulator(apiLevel: number, target: string, abi: string, device: string, headless: boolean, disableAnimations: boolean): Promise<void> {
const avdmangerPath = `${process.env.ANDROID_HOME}/tools/bin/avdmanager`;
const adbPath = `${process.env.ANDROID_HOME}/platform-tools/adb`;
// create a new AVD
console.log('Creating AVD.');
await exec.exec(`${avdmangerPath} create avd -n test --force --abi "${target}/${abi}" --package "system-images;android-${apiLevel};${target};${abi}" --device "${device}"`);
// start emulator
console.log('Starting emulator.');
const noWindow = headless ? '-no-window' : '';
await 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
await waitForDevice();
await exec.exec(`${adbPath} shell input keyevent 82`);
// disable animations
if (disableAnimations) {
console.log('Disabling animations.');
await exec.exec(`${adbPath} shell settings put global window_animation_scale 0.0`);
await exec.exec(`${adbPath} shell settings put global transition_animation_scale 0.0`);
await exec.exec(`${adbPath} shell settings put global animator_duration_scale 0.0`);
}
// kill emulator
await exec.exec(`${adbPath} -s emulator-5554 emu kill`);
}
/**
* Wait for emulator to boot.
*/
async function waitForDevice(): Promise<void> {
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 = '';
await exec.exec(`${adbPath} shell getprop sys.boot_completed`, [], {
listeners: {
stdout: (data: Buffer) => {
result += data.toString();
}
}
});
if (result.trim() === '1') {
console.log('Emulator booted.');
booted = true;
break;
}
} catch (e) {
console.error(e.message);
}
if (attempts < maxAttemps) {
await delay(retryInterval * 1000);
} else {
throw new Error(`Timeout waiting for emulator to boot.`);
}
attempts++;
}
}
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
+6
View File
@@ -28,3 +28,9 @@ export function checkHeadless(headless: string): void {
throw new Error(`Input for input.headless should be either 'true' or 'false'.`);
}
}
export function checkDisableAnimations(disableAnimations: string): void {
if (disableAnimations !== 'true' && disableAnimations !== 'false') {
throw new Error(`Input for input.disable-animations should be either 'true' or 'false'.`);
}
}
+20 -7
View File
@@ -1,7 +1,7 @@
import * as core from '@actions/core';
import { InputOptions } from '@actions/core/lib/core';
import { installAndroidSdk } from './sdk-installer';
import { checkApiLevel, checkTarget, checkAbi, checkHeadless } from './input-validator';
import { checkApiLevel, checkTarget, checkAbi, checkHeadless, checkDisableAnimations } from './input-validator';
import { launchEmulator } from './emulator-launcher';
async function run() {
try {
@@ -11,8 +11,9 @@ async function run() {
}
// API level of the platform and system image
const apiLevel = core.getInput('api-level', { required: true } as InputOptions);
checkApiLevel(apiLevel);
const apiLevelInput = core.getInput('api-level', { required: true });
checkApiLevel(apiLevelInput);
const apiLevel = Number(apiLevelInput);
console.log(`API level: ${apiLevel}`);
// target of the system image
@@ -26,12 +27,24 @@ async function run() {
console.log(`cpu/abi: ${abi}`);
// headless mode
const headless = core.getInput('headless');
checkHeadless(headless);
const headlessInput = core.getInput('headless');
checkHeadless(headlessInput);
const headless = headlessInput === 'true';
console.log(`headless mode: ${headless}`);
// disable animations
const disableAnimationsInput = core.getInput('disable-animations');
checkDisableAnimations(disableAnimationsInput);
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);
// install SDK
await installAndroidSdk(Number(apiLevel), target, abi);
await installAndroidSdk(apiLevel, target, abi);
// launch emulator
// TODO get from input (source list of all profiles)
const device = 'Nexus 6P';
await launchEmulator(apiLevel, target, abi, device, headless, disableAnimations);
// TODO start emulator
} catch (error) {
+5 -6
View File
@@ -2,7 +2,6 @@ import * as exec from '@actions/exec';
import * as tc from '@actions/tool-cache';
import * as fs from 'fs';
const BUILD_TOOLS_VERSION = '29.0.2';
const SDK_URL = 'https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip';
/**
@@ -21,9 +20,9 @@ export async function installAndroidSdk(apiLevel: number, target: string, abi: s
// install specific SDK tools
console.log('Installing build tools, platform tools, platform and system image.');
await exec.exec(`echo "y" | ${process.env.ANDROID_HOME}/tools/bin/sdkmanager --licenses > /dev/null`);
await exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "build-tools;${BUILD_TOOLS_VERSION}"`);
await exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "platform-tools"`);
await exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "platforms;android-${apiLevel}"`);
await exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "system-images;android-${apiLevel};${target};${abi}"`);
const sdkmangerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`;
await exec.exec(`echo "y" | ${sdkmangerPath} --licenses > /dev/null`);
await exec.exec(`${sdkmangerPath} tools platform-tools "platforms;android-${apiLevel}"`);
await exec.exec(`${sdkmangerPath} "system-images;android-${apiLevel};${target};${abi}"`);
await exec.exec(`bash -c \\"${sdkmangerPath} --update > /dev/null"`);
}