Add support for configuring hardware profile and running custom script.

This commit is contained in:
Yang Chen
2019-11-08 19:51:51 +11:00
parent 34df1eaf0e
commit 0f9b17b151
12 changed files with 150 additions and 81 deletions
@@ -1,17 +1,21 @@
import * as exec from '@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.
*/
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`;
export async function launchEmulator(apiLevel: number, target: string, arch: string, profile: string, headless: boolean, disableAnimations: boolean): Promise<void> {
// 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}"`);
if (profile.trim() !== '') {
console.log(`Creating AVD with custom profile ${profile}`);
await 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.`);
await 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.');
@@ -20,18 +24,26 @@ export async function launchEmulator(apiLevel: number, target: string, abi: stri
// wait for emulator to complete booting
await waitForDevice();
await exec.exec(`${adbPath} shell input keyevent 82`);
await exec.exec(`${ADB_PATH} 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`);
await exec.exec(`${ADB_PATH} shell settings put global window_animation_scale 0.0`);
await exec.exec(`${ADB_PATH} shell settings put global transition_animation_scale 0.0`);
await exec.exec(`${ADB_PATH} shell settings put global animator_duration_scale 0.0`);
}
}
// kill emulator
await exec.exec(`${adbPath} -s emulator-5554 emu kill`);
/**
* Kills the running emulator on the defaut port.
*/
export async function killEmulator(): Promise<void> {
try {
await exec.exec(`${ADB_PATH} -s emulator-5554 emu kill`);
} catch (error) {
console.log('No emulator running on port 5554');
}
}
/**
@@ -58,8 +70,8 @@ async function waitForDevice(): Promise<void> {
booted = true;
break;
}
} catch (e) {
console.error(e.message);
} catch (error) {
console.error(error.message);
}
if (attempts < maxAttemps) {
+4 -4
View File
@@ -1,6 +1,6 @@
export const MIN_API_LEVEL = 21;
export const VALID_TARGETS: Array<string> = ['default', 'google_apis'];
export const VALID_ABIS: Array<string> = ['x86', 'x86_64'];
export const VALID_ARCHS: Array<string> = ['x86', 'x86_64'];
export function checkApiLevel(apiLevel: string): void {
if (isNaN(Number(apiLevel)) || !Number.isInteger(Number(apiLevel))) {
@@ -17,9 +17,9 @@ export function checkTarget(target: string): void {
}
}
export function checkAbi(abi: string): void {
if (!VALID_ABIS.includes(abi)) {
throw new Error(`Value for input.abi '${abi}' is unknown. Supported options: ${VALID_ABIS}.`);
export function checkArch(arch: string): void {
if (!VALID_ARCHS.includes(arch)) {
throw new Error(`Value for input.arch '${arch}' is unknown. Supported options: ${VALID_ARCHS}.`);
}
}
+27 -12
View File
@@ -1,7 +1,8 @@
import * as core from '@actions/core';
import { installAndroidSdk } from './sdk-installer';
import { checkApiLevel, checkTarget, checkAbi, checkHeadless, checkDisableAnimations } from './input-validator';
import { launchEmulator } from './emulator-launcher';
import { checkApiLevel, checkTarget, checkArch, checkHeadless, checkDisableAnimations } from './input-validator';
import { launchEmulator, killEmulator } from './emulator-manager';
import * as exec from '@actions/exec';
async function run() {
try {
@@ -21,10 +22,14 @@ async function run() {
checkTarget(target);
console.log(`target: ${target}`);
// CPU / ABI of the system image
const abi = core.getInput('abi');
checkAbi(abi);
console.log(`cpu/abi: ${abi}`);
// CPU architecture of the system image
const arch = core.getInput('arch');
checkArch(arch);
console.log(`CPI architecture: ${arch}`);
// Hardware profile used for creating the AVD
const profile = core.getInput('profile');
console.log(`Hardware profile: ${profile}`);
// headless mode
const headlessInput = core.getInput('headless');
@@ -38,16 +43,26 @@ async function run() {
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);
// custom scrpt to run
const scriptInput = core.getInput('script', { required: true });
const commands = scriptInput.split(/\r?\n/);
// install SDK
await installAndroidSdk(apiLevel, target, abi);
await installAndroidSdk(apiLevel, target, arch);
// launch emulator
// TODO get from input (source list of all profiles)
const device = 'Nexus 6P';
await launchEmulator(apiLevel, target, abi, device, headless, disableAnimations);
// launch an emulator
await launchEmulator(apiLevel, target, arch, profile, headless, disableAnimations);
// TODO start emulator
// execute the custom script
commands.forEach(async command => {
await exec.exec(`${command}`);
});
// finally kill the emulator
await killEmulator();
} catch (error) {
// kill the emulator so the action can exit
await killEmulator();
core.setFailed(error.message);
}
}
+6 -4
View File
@@ -2,13 +2,14 @@ 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';
/**
* 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,
* and the system image for the chosen API level, cpu/abi, and target.
* and the system image for the chosen API level, CPU arch, and target.
*/
export async function installAndroidSdk(apiLevel: number, target: string, abi: string): Promise<void> {
export async function installAndroidSdk(apiLevel: number, target: string, arch: string): Promise<void> {
// download Android SDK if not already installed
if (fs.existsSync(`${process.env.ANDROID_HOME}/tools/bin/sdkmanager`)) {
console.log('Android SDK already installed.');
@@ -22,7 +23,8 @@ export async function installAndroidSdk(apiLevel: number, target: string, abi: s
console.log('Installing build tools, platform tools, platform and system image.');
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(`${sdkmangerPath} "build-tools;${BUILD_TOOLS_VERSION}"`);
await exec.exec(`${sdkmangerPath} platform-tools "platforms;android-${apiLevel}"`);
await exec.exec(`${sdkmangerPath} "system-images;android-${apiLevel};${target};${arch}"`);
await exec.exec(`bash -c \\"${sdkmangerPath} --update > /dev/null"`);
}