From 411cd10ea93700d1d771a836a2b489d63d4dc43a Mon Sep 17 00:00:00 2001 From: Nils Reichardt Date: Wed, 26 Oct 2022 17:33:13 +0200 Subject: [PATCH] Pre Emulator Launch Script (#247) * Add `pre-emulator-launch-script` to `action.yml` * Add `pre-emulator-launch-script` in `main.yml` * Implement pre emulator launch script * Try to use `pushd` * print working directory * Add `README` * Run `npm run build` * Fix `working-directory` * Run `npm run build` * Run pre emulator launch in group * Run `npm run build` * Update README.md Co-authored-by: Yang * Add type "pre-emulator-launch-script" Co-authored-by: Yang --- .github/workflows/main.yml | 3 +++ README.md | 3 ++- action-types.yml | 2 ++ action.yml | 2 ++ lib/main.js | 24 ++++++++++++++++++++++++ src/main.ts | 25 +++++++++++++++++++++++++ 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 1a00bb6fb..59e4a7e01 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -114,6 +114,9 @@ jobs: disable-animations: true working-directory: ./test-fixture/ channel: canary + pre-emulator-launch-script: | + echo "Running pre emulator launch script. Printing the working directory now:" + pwd script: | echo $GITHUB_REPOSITORY adb devices diff --git a/README.md b/README.md index c7d3a8f17..a19e3a8e7 100644 --- a/README.md +++ b/README.md @@ -155,11 +155,12 @@ jobs: | `disable-linux-hw-accel` | Optional | `auto` | Whether to disable hardware acceleration on Linux machines - `true`, `false` or `auto`.| | `enable-hw-keyboard` | Optional | `false` | Whether to enable hardware keyboard - `true` or `false`. | | `emulator-build` | Optional | N/A | Build number of a specific version of the emulator binary to use e.g. `6061023` for emulator v29.3.0.0. | -| `working-directory` | Optional | `./` | A custom working directory - e.g. `./android` if your root Gradle project is under the `./android` sub-directory within your repository. | +| `working-directory` | Optional | `./` | A custom working directory - e.g. `./android` if your root Gradle project is under the `./android` sub-directory within your repository. Will be used for `script` & `pre-emulator-launch-script`. | | `ndk` | Optional | N/A | Version of NDK to install - e.g. `21.0.6113669` | | `cmake` | Optional | N/A | Version of CMake to install - e.g. `3.10.2.4988404` | | `channel` | Optional | stable | Channel to download the SDK components from - `stable`, `beta`, `dev`, `canary` | | `script` | Required | N/A | Custom script to run - e.g. to run Android instrumented tests on the emulator: `./gradlew connectedCheck` | +| `pre-emulator-launch-script` | Optional | N/A | Custom script to run after creating the AVD and before launching the emulator - e.g. `./adjust-emulator-configs.sh` | Default `emulator-options`: `-no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim`. diff --git a/action-types.yml b/action-types.yml index 6299ab960..1c1d4617c 100644 --- a/action-types.yml +++ b/action-types.yml @@ -62,3 +62,5 @@ inputs: - canary script: type: string + pre-emulator-launch-script: + type: string diff --git a/action.yml b/action.yml index 0bc89f62d..a26d05c3e 100644 --- a/action.yml +++ b/action.yml @@ -62,6 +62,8 @@ inputs: script: description: 'custom script to run - e.g. `./gradlew connectedCheck`' required: true + pre-emulator-launch-script: + description: 'custom script to run which will run before launch the emulator - e.g. `./adjust-emulator-configs.sh`' runs: using: 'node16' main: 'lib/main.js' diff --git a/lib/main.js b/lib/main.js index 1ccc06d02..0cf9cb2ee 100644 --- a/lib/main.js +++ b/lib/main.js @@ -159,9 +159,33 @@ function run() { scripts.forEach((script) => __awaiter(this, void 0, void 0, function* () { console.log(`${script}`); })); + // custom pre emulator launch script + const preEmulatorLaunchScriptInput = core.getInput('pre-emulator-launch-script'); + const preEmulatorLaunchScripts = !preEmulatorLaunchScriptInput ? undefined : script_parser_1.parseScript(preEmulatorLaunchScriptInput); + console.log(`Pre emulator launch script:`); + preEmulatorLaunchScripts === null || preEmulatorLaunchScripts === void 0 ? void 0 : preEmulatorLaunchScripts.forEach((script) => __awaiter(this, void 0, void 0, function* () { + console.log(`${script}`); + })); console.log(`::endgroup::`); // install SDK yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, channelId, emulatorBuild, ndkVersion, cmakeVersion); + // execute pre emulator launch script if set + if (preEmulatorLaunchScripts !== undefined) { + console.log(`::group::Run pre emulator launch script`); + try { + for (const preEmulatorLaunchScript of preEmulatorLaunchScripts) { + // use array form to avoid various quote escaping problems + // caused by exec(`sh -c "${preEmulatorLaunchScript}"`) + yield exec.exec('sh', ['-c', preEmulatorLaunchScript], { + cwd: workingDirectory + }); + } + } + catch (error) { + core.setFailed(error.message); + } + console.log(`::endgroup::`); + } // launch an emulator yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, cores, ramSize, heapSize, sdcardPathOrSize, diskSize, avdName, forceAvdCreation, emulatorOptions, disableAnimations, disableSpellchecker, disableLinuxHardwareAcceleration, enableHardwareKeyboard); // execute the custom script diff --git a/src/main.ts b/src/main.ts index 63d34c6b8..32a5ed080 100644 --- a/src/main.ts +++ b/src/main.ts @@ -163,11 +163,36 @@ async function run() { scripts.forEach(async (script: string) => { console.log(`${script}`); }); + + // custom pre emulator launch script + const preEmulatorLaunchScriptInput = core.getInput('pre-emulator-launch-script'); + const preEmulatorLaunchScripts = !preEmulatorLaunchScriptInput ? undefined : parseScript(preEmulatorLaunchScriptInput); + console.log(`Pre emulator launch script:`); + preEmulatorLaunchScripts?.forEach(async (script: string) => { + console.log(`${script}`); + }); console.log(`::endgroup::`); // install SDK await installAndroidSdk(apiLevel, target, arch, channelId, emulatorBuild, ndkVersion, cmakeVersion); + // execute pre emulator launch script if set + if (preEmulatorLaunchScripts !== undefined) { + console.log(`::group::Run pre emulator launch script`); + try { + for (const preEmulatorLaunchScript of preEmulatorLaunchScripts) { + // use array form to avoid various quote escaping problems + // caused by exec(`sh -c "${preEmulatorLaunchScript}"`) + await exec.exec('sh', ['-c', preEmulatorLaunchScript], { + cwd: workingDirectory + }); + } + } catch (error) { + core.setFailed(error.message); + } + console.log(`::endgroup::`); + } + // launch an emulator await launchEmulator( apiLevel,