From 85a8369ec14ba0319a24df5b8728f5857332ade0 Mon Sep 17 00:00:00 2001 From: Michael Kaye <1917473+michaelkaye@users.noreply.github.com> Date: Wed, 16 Feb 2022 11:39:05 +0000 Subject: [PATCH] Add GHA commands to group logs --- src/emulator-manager.ts | 108 +++++++++++++++++++++------------------- src/main.ts | 2 + src/sdk-installer.ts | 83 +++++++++++++++--------------- 3 files changed, 104 insertions(+), 89 deletions(-) diff --git a/src/emulator-manager.ts b/src/emulator-manager.ts index 9a2938e8e..6ee896141 100644 --- a/src/emulator-manager.ts +++ b/src/emulator-manager.ts @@ -23,67 +23,72 @@ export async function launchEmulator( disableLinuxHardwareAcceleration: boolean, enableHardwareKeyboard: boolean ): Promise { - // create a new AVD if AVD directory does not already exist or forceAvdCreation is true - const avdPath = `${process.env.ANDROID_AVD_HOME}/${avdName}.avd`; - if (!fs.existsSync(avdPath) || forceAvdCreation) { - const profileOption = profile.trim() !== '' ? `--device '${profile}'` : ''; - const sdcardPathOrSizeOption = sdcardPathOrSize.trim() !== '' ? `--sdcard '${sdcardPathOrSize}'` : ''; - console.log(`Creating AVD.`); - await exec.exec( - `sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}' ${profileOption} ${sdcardPathOrSizeOption}"` - ); - } + try { + console.log(`::group::Launch Emulator`); + // create a new AVD if AVD directory does not already exist or forceAvdCreation is true + const avdPath = `${process.env.ANDROID_AVD_HOME}/${avdName}.avd`; + if (!fs.existsSync(avdPath) || forceAvdCreation) { + const profileOption = profile.trim() !== '' ? `--device '${profile}'` : ''; + const sdcardPathOrSizeOption = sdcardPathOrSize.trim() !== '' ? `--sdcard '${sdcardPathOrSize}'` : ''; + console.log(`Creating AVD.`); + await exec.exec( + `sh -c \\"echo no | avdmanager create avd --force -n "${avdName}" --abi '${target}/${arch}' --package 'system-images;android-${apiLevel};${target};${arch}' ${profileOption} ${sdcardPathOrSizeOption}"` + ); + } - if (cores) { - await exec.exec(`sh -c \\"printf 'hw.cpu.ncore=${cores}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); - } + if (cores) { + await exec.exec(`sh -c \\"printf 'hw.cpu.ncore=${cores}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); + } - if (ramSize) { - await exec.exec(`sh -c \\"printf 'hw.ramSize=${ramSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); - } + if (ramSize) { + await exec.exec(`sh -c \\"printf 'hw.ramSize=${ramSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); + } - if (enableHardwareKeyboard) { - await exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); - } + if (enableHardwareKeyboard) { + await exec.exec(`sh -c \\"printf 'hw.keyboard=yes\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); + } - if (diskSize) { - await exec.exec(`sh -c \\"printf 'disk.dataPartition.size=${diskSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); - } + if (diskSize) { + await exec.exec(`sh -c \\"printf 'disk.dataPartition.size=${diskSize}\n' >> ${process.env.ANDROID_AVD_HOME}/"${avdName}".avd"/config.ini`); + } - //turn off hardware acceleration on Linux - if (process.platform === 'linux' && disableLinuxHardwareAcceleration) { - console.log('Disabling Linux hardware acceleration.'); - emulatorOptions += ' -accel off'; - } + //turn off hardware acceleration on Linux + if (process.platform === 'linux' && disableLinuxHardwareAcceleration) { + console.log('Disabling Linux hardware acceleration.'); + emulatorOptions += ' -accel off'; + } - // start emulator - console.log('Starting emulator.'); + // start emulator + console.log('Starting emulator.'); - await exec.exec(`sh -c \\"${process.env.ANDROID_SDK_ROOT}/emulator/emulator -avd "${avdName}" ${emulatorOptions} &"`, [], { - listeners: { - stderr: (data: Buffer) => { - if (data.toString().includes('invalid command-line parameter')) { - throw new Error(data.toString()); + await exec.exec(`sh -c \\"${process.env.ANDROID_SDK_ROOT}/emulator/emulator -avd "${avdName}" ${emulatorOptions} &"`, [], { + listeners: { + stderr: (data: Buffer) => { + if (data.toString().includes('invalid command-line parameter')) { + throw new Error(data.toString()); + } } } - } - }); + }); - // wait for emulator to complete booting - await waitForDevice(); - await exec.exec(`adb shell input keyevent 82`); + // wait for emulator to complete booting + await waitForDevice(); + await exec.exec(`adb shell input keyevent 82`); - if (disableAnimations) { - console.log('Disabling animations.'); - await exec.exec(`adb shell settings put global window_animation_scale 0.0`); - await exec.exec(`adb shell settings put global transition_animation_scale 0.0`); - await exec.exec(`adb shell settings put global animator_duration_scale 0.0`); - } - if (disableSpellChecker) { - await exec.exec(`adb shell settings put secure spell_checker_enabled 0`); - } - if (enableHardwareKeyboard) { - await exec.exec(`adb shell settings put secure show_ime_with_hard_keyboard 0`); + if (disableAnimations) { + console.log('Disabling animations.'); + await exec.exec(`adb shell settings put global window_animation_scale 0.0`); + await exec.exec(`adb shell settings put global transition_animation_scale 0.0`); + await exec.exec(`adb shell settings put global animator_duration_scale 0.0`); + } + if (disableSpellChecker) { + await exec.exec(`adb shell settings put secure spell_checker_enabled 0`); + } + if (enableHardwareKeyboard) { + await exec.exec(`adb shell settings put secure show_ime_with_hard_keyboard 0`); + } + } finally { + console.log(`::endgroup::`); } } @@ -92,9 +97,12 @@ export async function launchEmulator( */ export async function killEmulator(): Promise { try { + console.log(`::group::Terminate Emulator`); await exec.exec(`adb -s emulator-5554 emu kill`); } catch (error) { console.log(error.message); + } finally { + console.log(`::endgroup::`); } } diff --git a/src/main.ts b/src/main.ts index 596917df1..325d1f14e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -20,6 +20,7 @@ import { getChannelId } from './channel-id-mapper'; async function run() { try { + console.log(`::group::Configure emulator`); // only support running on macOS or Linux if (process.platform !== 'darwin') { if (process.platform === 'linux') { @@ -148,6 +149,7 @@ async function run() { scripts.forEach(async (script: string) => { console.log(`${script}`); }); + console.log(`::endgroup::`); // install SDK await installAndroidSdk(apiLevel, target, arch, channelId, emulatorBuild, ndkVersion, cmakeVersion); diff --git a/src/sdk-installer.ts b/src/sdk-installer.ts index 6a81efc43..8778829ef 100644 --- a/src/sdk-installer.ts +++ b/src/sdk-installer.ts @@ -13,54 +13,59 @@ const CMDLINE_TOOLS_URL_LINUX = 'https://dl.google.com/android/repository/comman * and the system image for the chosen API level, CPU arch, and target. */ export async function installAndroidSdk(apiLevel: number, target: string, arch: string, channelId: number, emulatorBuild?: string, ndkVersion?: string, cmakeVersion?: string): Promise { - const isOnMac = process.platform === 'darwin'; + try { + console.log(`::group::Install Android SDK`); + const isOnMac = process.platform === 'darwin'; - if (!isOnMac) { - await exec.exec(`sh -c \\"sudo chown $USER:$USER ${process.env.ANDROID_SDK_ROOT} -R`); - } + if (!isOnMac) { + await exec.exec(`sh -c \\"sudo chown $USER:$USER ${process.env.ANDROID_SDK_ROOT} -R`); + } - const cmdlineToolsPath = `${process.env.ANDROID_SDK_ROOT}/cmdline-tools`; - if (!fs.existsSync(cmdlineToolsPath)) { - console.log('Installing new cmdline-tools.'); - const sdkUrl = isOnMac ? CMDLINE_TOOLS_URL_MAC : CMDLINE_TOOLS_URL_LINUX; - const downloadPath = await tc.downloadTool(sdkUrl); - await tc.extractZip(downloadPath, cmdlineToolsPath); - await io.mv(`${cmdlineToolsPath}/cmdline-tools`, `${cmdlineToolsPath}/latest`); - } + const cmdlineToolsPath = `${process.env.ANDROID_SDK_ROOT}/cmdline-tools`; + if (!fs.existsSync(cmdlineToolsPath)) { + console.log('Installing new cmdline-tools.'); + const sdkUrl = isOnMac ? CMDLINE_TOOLS_URL_MAC : CMDLINE_TOOLS_URL_LINUX; + const downloadPath = await tc.downloadTool(sdkUrl); + await tc.extractZip(downloadPath, cmdlineToolsPath); + await io.mv(`${cmdlineToolsPath}/cmdline-tools`, `${cmdlineToolsPath}/latest`); + } - // add paths for commandline-tools and platform-tools - core.addPath(`${cmdlineToolsPath}/latest:${cmdlineToolsPath}/latest/bin:${process.env.ANDROID_SDK_ROOT}/platform-tools`); + // add paths for commandline-tools and platform-tools + core.addPath(`${cmdlineToolsPath}/latest:${cmdlineToolsPath}/latest/bin:${process.env.ANDROID_SDK_ROOT}/platform-tools`); - // set standard AVD path - core.exportVariable('ANDROID_AVD_HOME', `${process.env.HOME}/.android/avd`); + // set standard AVD path + core.exportVariable('ANDROID_AVD_HOME', `${process.env.HOME}/.android/avd`); - // accept all Android SDK licenses - await exec.exec(`sh -c \\"yes | sdkmanager --licenses > /dev/null"`); + // accept all Android SDK licenses + await exec.exec(`sh -c \\"yes | sdkmanager --licenses > /dev/null"`); - console.log('Installing latest build tools, platform tools, and platform.'); + console.log('Installing latest build tools, platform tools, and platform.'); - await exec.exec(`sh -c \\"sdkmanager --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`); + await exec.exec(`sh -c \\"sdkmanager --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`); - console.log('Installing latest emulator.'); - await exec.exec(`sh -c \\"sdkmanager --install emulator --channel=${channelId} > /dev/null"`); + console.log('Installing latest emulator.'); + await exec.exec(`sh -c \\"sdkmanager --install emulator --channel=${channelId} > /dev/null"`); - if (emulatorBuild) { - console.log(`Installing emulator build ${emulatorBuild}.`); - // TODO find out the correct download URLs for all build ids - const downloadUrlSuffix = Number(emulatorBuild.charAt(0)) > 6 ? `_x64-${emulatorBuild}` : `-${emulatorBuild}`; - await exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-${isOnMac ? 'darwin' : 'linux'}${downloadUrlSuffix}.zip`); - await exec.exec(`unzip -o -q emulator.zip -d ${process.env.ANDROID_SDK_ROOT}`); - await io.rmRF('emulator.zip'); - } - console.log('Installing system images.'); - await exec.exec(`sh -c \\"sdkmanager --install 'system-images;android-${apiLevel};${target};${arch}' --channel=${channelId} > /dev/null"`); + if (emulatorBuild) { + console.log(`Installing emulator build ${emulatorBuild}.`); + // TODO find out the correct download URLs for all build ids + const downloadUrlSuffix = Number(emulatorBuild.charAt(0)) > 6 ? `_x64-${emulatorBuild}` : `-${emulatorBuild}`; + await exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-${isOnMac ? 'darwin' : 'linux'}${downloadUrlSuffix}.zip`); + await exec.exec(`unzip -o -q emulator.zip -d ${process.env.ANDROID_SDK_ROOT}`); + await io.rmRF('emulator.zip'); + } + console.log('Installing system images.'); + await exec.exec(`sh -c \\"sdkmanager --install 'system-images;android-${apiLevel};${target};${arch}' --channel=${channelId} > /dev/null"`); - if (ndkVersion) { - console.log(`Installing NDK ${ndkVersion}.`); - await exec.exec(`sh -c \\"sdkmanager --install 'ndk;${ndkVersion}' --channel=${channelId} > /dev/null"`); - } - if (cmakeVersion) { - console.log(`Installing CMake ${cmakeVersion}.`); - await exec.exec(`sh -c \\"sdkmanager --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`); + if (ndkVersion) { + console.log(`Installing NDK ${ndkVersion}.`); + await exec.exec(`sh -c \\"sdkmanager --install 'ndk;${ndkVersion}' --channel=${channelId} > /dev/null"`); + } + if (cmakeVersion) { + console.log(`Installing CMake ${cmakeVersion}.`); + await exec.exec(`sh -c \\"sdkmanager --install 'cmake;${cmakeVersion}' --channel=${channelId} > /dev/null"`); + } + } finally { + console.log(`::endgroup::`); } }