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
+13 -6
View File
@@ -1,7 +1,8 @@
{
"env": {
"node": true,
"jest": true
"jest": true,
"es6": true
},
"extends": [
"eslint:recommended",
@@ -26,10 +27,16 @@
"prettier/prettier": [
"error",
{
"singleQuote": true,
"printWidth": 200
"singleQuote": true,
"printWidth": 200
}
],
"@typescript-eslint/explicit-function-return-type": "off"
],
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/no-use-before-define": [
"error",
{
"functions": false
}
]
}
}
}
+2 -1
View File
@@ -11,7 +11,7 @@ jobs:
timeout-minutes: 20
strategy:
matrix:
api-level: [21, 29]
api-level: [21, 23, 29]
steps:
- name: checkout
uses: actions/checkout@v1
@@ -32,3 +32,4 @@ jobs:
target: default
abi: x86
headless: true
disable-animations: true
+3 -2
View File
@@ -12,12 +12,13 @@ This action must be run on a **macOS** VM, e.g. `macOS-latest` or `macOS-10.14`.
## Configurations
| **Input** | **Required** | **Default** | **Description** |
|-------------|--------------|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| | **Required** | **Default** | **Description** |
|----------------------|--------------|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------|
| `api-level` | Required | N/A | API level of the platform system image - e.g. 23 for Android Marshmallow, 29 for Android 10. **Minimum API level supported is 21**. |
| `target` | Optional | `default` | Target of the system image - `default` or `google_apis`. |
| `abi` | Optional | `x86` | CPU / ABI of the system image - `x86` or `x86_64`. |
| `headless` | Optional | `true` | Whether to launch emulator without UI - `true` or `false`. When set to `true` this is equivalent to running the emulator with `emulator -no-window`. |
| `disable-animations` | Optional | `true` | Whether to disable animations - `true` or `false`. |
## Usage
+21
View File
@@ -97,3 +97,24 @@ describe('headless validator tests', () => {
expect(func2).not.toThrow();
});
});
describe('disable-animations validator tests', () => {
it('Throws if disable-animations is not a boolean', () => {
const func = () => {
validator.checkDisableAnimations('yes');
};
expect(func).toThrowError(`Input for input.disable-animations should be either 'true' or 'false'.`);
});
it('Validates successfully if disable-animations is either true or false', () => {
const func1 = () => {
validator.checkDisableAnimations('true');
};
expect(func1).not.toThrow();
const func2 = () => {
validator.checkDisableAnimations('false');
};
expect(func2).not.toThrow();
});
});
+3
View File
@@ -17,6 +17,9 @@ inputs:
headless:
description: 'whether to launch emulator in without UI - true or false'
default: 'true'
disable-animations:
description: 'whether to disable animations - true or false'
default: 'true'
runs:
using: 'node12'
main: 'lib/main.js'
+74 -3
View File
@@ -7,13 +7,84 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
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;
/**
* Creates and launches a new AVD instance with the specified configurations.
*/
function startEmulator() {
function launchEmulator(apiLevel, target, abi, device, headless, disableAnimations) {
return __awaiter(this, void 0, void 0, function* () {
// TODO
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.');
yield 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' : '';
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(`${adbPath} shell input keyevent 82`);
// disable animations
if (disableAnimations) {
console.log('Disabling animations.');
yield exec.exec(`${adbPath} shell settings put global window_animation_scale 0.0`);
yield exec.exec(`${adbPath} shell settings put global transition_animation_scale 0.0`);
yield exec.exec(`${adbPath} shell settings put global animator_duration_scale 0.0`);
}
// kill emulator
yield exec.exec(`${adbPath} -s emulator-5554 emu kill`);
});
}
exports.startEmulator = startEmulator;
exports.launchEmulator = launchEmulator;
/**
* 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 (e) {
console.error(e.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));
}
+6
View File
@@ -30,3 +30,9 @@ function checkHeadless(headless) {
}
}
exports.checkHeadless = checkHeadless;
function checkDisableAnimations(disableAnimations) {
if (disableAnimations !== 'true' && disableAnimations !== 'false') {
throw new Error(`Input for input.disable-animations should be either 'true' or 'false'.`);
}
}
exports.checkDisableAnimations = checkDisableAnimations;
+17 -5
View File
@@ -18,6 +18,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
const core = __importStar(require("@actions/core"));
const sdk_installer_1 = require("./sdk-installer");
const input_validator_1 = require("./input-validator");
const emulator_launcher_1 = require("./emulator-launcher");
function run() {
return __awaiter(this, void 0, void 0, function* () {
try {
@@ -26,8 +27,9 @@ function run() {
throw new Error('This action is expected to be run within a macOS virtual machine to enable hardware acceleration.');
}
// API level of the platform and system image
const apiLevel = core.getInput('api-level', { required: true });
input_validator_1.checkApiLevel(apiLevel);
const apiLevelInput = core.getInput('api-level', { required: true });
input_validator_1.checkApiLevel(apiLevelInput);
const apiLevel = Number(apiLevelInput);
console.log(`API level: ${apiLevel}`);
// target of the system image
const target = core.getInput('target');
@@ -38,11 +40,21 @@ function run() {
input_validator_1.checkAbi(abi);
console.log(`cpu/abi: ${abi}`);
// headless mode
const headless = core.getInput('headless');
input_validator_1.checkHeadless(headless);
const headlessInput = core.getInput('headless');
input_validator_1.checkHeadless(headlessInput);
const headless = headlessInput === 'true';
console.log(`headless mode: ${headless}`);
// disable animations
const disableAnimationsInput = core.getInput('disable-animations');
input_validator_1.checkDisableAnimations(disableAnimationsInput);
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);
// install SDK
yield sdk_installer_1.installAndroidSdk(Number(apiLevel), target, abi);
yield sdk_installer_1.installAndroidSdk(apiLevel, target, abi);
// launch emulator
// TODO get from input (source list of all profiles)
const device = 'Nexus 6P';
yield emulator_launcher_1.launchEmulator(apiLevel, target, abi, device, headless, disableAnimations);
// TODO start emulator
}
catch (error) {
+5 -6
View File
@@ -18,7 +18,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
const exec = __importStar(require("@actions/exec"));
const tc = __importStar(require("@actions/tool-cache"));
const fs = __importStar(require("fs"));
const BUILD_TOOLS_VERSION = '29.0.2';
const SDK_URL = 'https://dl.google.com/android/repository/sdk-tools-darwin-4333796.zip';
/**
* Downloads and installs the Android SDK for the macOS platform, including SDK platform for the chosen API level, latest build tools, platform tools, Android Emulator,
@@ -37,11 +36,11 @@ function installAndroidSdk(apiLevel, target, abi) {
}
// install specific SDK tools
console.log('Installing build tools, platform tools, platform and system image.');
yield exec.exec(`echo "y" | ${process.env.ANDROID_HOME}/tools/bin/sdkmanager --licenses > /dev/null`);
yield exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "build-tools;${BUILD_TOOLS_VERSION}"`);
yield exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "platform-tools"`);
yield exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "platforms;android-${apiLevel}"`);
yield exec.exec(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager "system-images;android-${apiLevel};${target};${abi}"`);
const sdkmangerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`;
yield exec.exec(`echo "y" | ${sdkmangerPath} --licenses > /dev/null`);
yield exec.exec(`${sdkmangerPath} tools platform-tools "platforms;android-${apiLevel}"`);
yield exec.exec(`${sdkmangerPath} "system-images;android-${apiLevel};${target};${abi}"`);
yield exec.exec(`bash -c \\"${sdkmangerPath} --update > /dev/null"`);
});
}
exports.installAndroidSdk = installAndroidSdk;
+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"`);
}