diff --git a/.eslintrc.json b/.eslintrc.json index 94be9750..fbda3dec 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -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 + } + ] } -} +} \ No newline at end of file diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 4e9c7944..0de44e03 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -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 diff --git a/README.md b/README.md index 50611f8a..11641c57 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/__tests__/input-validator.test.ts b/__tests__/input-validator.test.ts index 0cac0f06..ddfaa917 100644 --- a/__tests__/input-validator.test.ts +++ b/__tests__/input-validator.test.ts @@ -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(); + }); +}); diff --git a/action.yml b/action.yml index 582ebdaf..7fba3f88 100644 --- a/action.yml +++ b/action.yml @@ -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' diff --git a/lib/emulator-launcher.js b/lib/emulator-launcher.js index e0ebcbf1..b5f0bb9e 100644 --- a/lib/emulator-launcher.js +++ b/lib/emulator-launcher.js @@ -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)); +} diff --git a/lib/input-validator.js b/lib/input-validator.js index 98bfd751..4b5c52f8 100644 --- a/lib/input-validator.js +++ b/lib/input-validator.js @@ -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; diff --git a/lib/main.js b/lib/main.js index b9cd281a..89d6b551 100644 --- a/lib/main.js +++ b/lib/main.js @@ -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) { diff --git a/lib/sdk-installer.js b/lib/sdk-installer.js index 338dfe39..5455a86b 100644 --- a/lib/sdk-installer.js +++ b/lib/sdk-installer.js @@ -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; diff --git a/src/emulator-launcher.ts b/src/emulator-launcher.ts index 735225c1..bad092f0 100644 --- a/src/emulator-launcher.ts +++ b/src/emulator-launcher.ts @@ -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 { - // TODO +export async function launchEmulator(apiLevel: number, target: string, abi: string, device: string, headless: boolean, disableAnimations: boolean): Promise { + 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 { + 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)); } diff --git a/src/input-validator.ts b/src/input-validator.ts index b8b2e3aa..635663d8 100644 --- a/src/input-validator.ts +++ b/src/input-validator.ts @@ -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'.`); + } +} diff --git a/src/main.ts b/src/main.ts index 31365679..d73dd0bb 100644 --- a/src/main.ts +++ b/src/main.ts @@ -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) { diff --git a/src/sdk-installer.ts b/src/sdk-installer.ts index 0a673394..c7a60a33 100644 --- a/src/sdk-installer.ts +++ b/src/sdk-installer.ts @@ -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"`); }