Skip to content

Commit

Permalink
Add option to enable reading and saving logcat output
Browse files Browse the repository at this point in the history
  • Loading branch information
AfzalivE committed Feb 25, 2021
1 parent 87a8f95 commit e980520
Show file tree
Hide file tree
Showing 9 changed files with 107 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ jobs:
| `disable-autofill` | Optional | `false` | Whether to disable autofill - `true` or `false`. |
| `longpress-timeout` | Optional | 500 | Longpress timeout in milliseconds. |
| `enable-hw-keyboard` | Optional | `false` | Whether to enable the hw keyboard and disable soft keyboard - `true` or `false`. |
| `enable-logcat` | Optional | `false` | Whether to read and save logcat output to `artifacts/logcat.log` |
| `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. |
| `ndk` | Optional | N/A | Version of NDK to install - e.g. `21.0.6113669` |
Expand Down
21 changes: 21 additions & 0 deletions __tests__/input-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,27 @@ describe('enable-hw-keyboard validator tests', () => {
});
});

describe('enable-logcat validator tests', () => {
it('Throws if enable-logcat is not a boolean', () => {
const func = () => {
validator.checkEnableLogcat('yes');
};
expect(func).toThrowError(`Input for input.enable-logcat should be either 'true' or 'false'.`);
});

it('Validates successfully if enable-logcat is either true or false', () => {
const func1 = () => {
validator.checkEnableLogcat('true');
};
expect(func1).not.toThrow();

const func2 = () => {
validator.checkEnableLogcat('false');
};
expect(func2).not.toThrow();
});
});

describe('longpress-timeout validator tests', () => {
it('Throws if longpress-timeout is not a number', () => {
const func = () => {
Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ inputs:
enable-hw-keyboard:
description: Enable hw keyboard and disable soft keyboard - `true` or `false`.
default: 'false'
enable-logcat:
description: Enable reading and saving logcat output to `artifacts/logcat.log`.
default: 'false'
emulator-build:
description: 'build number of a specific version of the emulator binary to use - e.g. `6061023` for emulator v29.3.0.0'
working-directory:
Expand Down
28 changes: 27 additions & 1 deletion lib/emulator-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.killEmulator = exports.launchEmulator = void 0;
const exec = __importStar(require("@actions/exec"));
const EMULATOR_BOOT_TIMEOUT_SECONDS = 600;
let ENABLE_LOGCAT = false;
/**
* Creates and launches a new AVD instance with the specified configurations.
*/
function launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellChecker, disableAutofill, longPressTimeout, enableHwKeyboard) {
function launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellChecker, disableAutofill, longPressTimeout, enableHwKeyboard, enableLogcat) {
return __awaiter(this, void 0, void 0, function* () {
// create a new AVD
const profileOption = profile.trim() !== '' ? `--device '${profile}'` : '';
Expand Down Expand Up @@ -84,6 +85,10 @@ function launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize
if (enableHwKeyboard) {
yield exec.exec(`adb shell settings put secure show_ime_with_hard_keyboard 0`);
}
if (enableLogcat) {
ENABLE_LOGCAT = enableLogcat;
yield startLogcat();
}
});
}
exports.launchEmulator = launchEmulator;
Expand All @@ -93,6 +98,9 @@ exports.launchEmulator = launchEmulator;
function killEmulator() {
return __awaiter(this, void 0, void 0, function* () {
try {
if (ENABLE_LOGCAT) {
yield stopLogcat();
}
yield exec.exec(`adb -s emulator-5554 emu kill`);
}
catch (error) {
Expand Down Expand Up @@ -142,3 +150,21 @@ function waitForDevice() {
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function startLogcat() {
return __awaiter(this, void 0, void 0, function* () {
console.log('Starting logcat read process');
yield exec.exec(`mkdir -p artifacts`);
try {
yield exec.exec(`sh -c \\"adb logcat -v time > artifacts/logcat.log &"`);
}
catch (error) {
console.log(error.message);
}
});
}
function stopLogcat() {
return __awaiter(this, void 0, void 0, function* () {
console.log('Stopping logcat read process');
yield exec.exec(`sh -c "jobs -p | xargs kill"`);
});
}
8 changes: 7 additions & 1 deletion lib/input-validator.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.checkEmulatorBuild = exports.checkLongPressTimeout = exports.checkEnableHwKeyboard = exports.checkDisableAutofill = exports.checkDisableSpellchecker = exports.checkDisableAnimations = exports.checkArch = exports.checkTarget = exports.checkApiLevel = exports.VALID_ARCHS = exports.VALID_TARGETS = exports.MIN_API_LEVEL = void 0;
exports.checkEmulatorBuild = exports.checkLongPressTimeout = exports.checkEnableLogcat = exports.checkEnableHwKeyboard = exports.checkDisableAutofill = exports.checkDisableSpellchecker = exports.checkDisableAnimations = exports.checkArch = exports.checkTarget = exports.checkApiLevel = exports.VALID_ARCHS = exports.VALID_TARGETS = exports.MIN_API_LEVEL = void 0;
exports.MIN_API_LEVEL = 15;
exports.VALID_TARGETS = ['default', 'google_apis', 'google_apis_playstore'];
exports.VALID_ARCHS = ['x86', 'x86_64'];
Expand Down Expand Up @@ -49,6 +49,12 @@ function checkEnableHwKeyboard(enableHwKeyboard) {
}
}
exports.checkEnableHwKeyboard = checkEnableHwKeyboard;
function checkEnableLogcat(enableLogcat) {
if (!isValidBoolean(enableLogcat)) {
throw new Error(`Input for input.enable-logcat should be either 'true' or 'false'.`);
}
}
exports.checkEnableLogcat = checkEnableLogcat;
function checkLongPressTimeout(timeout) {
if (isNaN(Number(timeout)) || !Number.isInteger(Number(timeout))) {
throw new Error(`Unexpected longpress-timeout: '${timeout}'.`);
Expand Down
7 changes: 6 additions & 1 deletion lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ function run() {
input_validator_1.checkEnableHwKeyboard(enableHwKeyboardInput);
const enableHwKeyboard = enableHwKeyboardInput === 'true';
console.log(`enable hw keyboard: ${enableHwKeyboard}`);
// enable logcat
const enableLogcatInput = core.getInput('enable-logcat');
input_validator_1.checkEnableLogcat(enableLogcatInput);
const enableLogcat = enableLogcatInput === 'true';
console.log(`enable logcat: ${enableLogcat}`);
// disable spellchecker
const disableSpellcheckerInput = core.getInput('disable-spellchecker');
input_validator_1.checkDisableSpellchecker(disableSpellcheckerInput);
Expand Down Expand Up @@ -135,7 +140,7 @@ function run() {
// install SDK
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild, ndkVersion, cmakeVersion);
// launch an emulator
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellchecker, disableAutofill, longPressTimeout, enableHwKeyboard);
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, cores, sdcardPathOrSize, avdName, emulatorOptions, disableAnimations, disableSpellchecker, disableAutofill, longPressTimeout, enableHwKeyboard, enableLogcat);
// execute the custom script
try {
// move to custom working directory if set
Expand Down
27 changes: 26 additions & 1 deletion src/emulator-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as exec from '@actions/exec';

const EMULATOR_BOOT_TIMEOUT_SECONDS = 600;
let ENABLE_LOGCAT = false;

/**
* Creates and launches a new AVD instance with the specified configurations.
Expand All @@ -18,7 +19,8 @@ export async function launchEmulator(
disableSpellChecker: boolean,
disableAutofill: boolean,
longPressTimeout: number,
enableHwKeyboard: boolean
enableHwKeyboard: boolean,
enableLogcat: boolean
): Promise<void> {
// create a new AVD
const profileOption = profile.trim() !== '' ? `--device '${profile}'` : '';
Expand Down Expand Up @@ -77,13 +79,21 @@ export async function launchEmulator(
if (enableHwKeyboard) {
await exec.exec(`adb shell settings put secure show_ime_with_hard_keyboard 0`);
}

if (enableLogcat) {
ENABLE_LOGCAT = enableLogcat;
await startLogcat();
}
}

/**
* Kills the running emulator on the defaut port.
*/
export async function killEmulator(): Promise<void> {
try {
if (ENABLE_LOGCAT) {
await stopLogcat();
}
await exec.exec(`adb -s emulator-5554 emu kill`);
} catch (error) {
console.log(error.message);
Expand Down Expand Up @@ -129,3 +139,18 @@ async function waitForDevice(): Promise<void> {
function delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}

async function startLogcat(): Promise<void> {
console.log('Starting logcat read process');
await exec.exec(`mkdir -p artifacts`);
try {
await exec.exec(`sh -c \\"adb logcat -v time > artifacts/logcat.log &"`);
} catch (error) {
console.log(error.message);
}
}

async function stopLogcat(): Promise<void> {
console.log('Stopping logcat read process');
await exec.exec(`sh -c "jobs -p | xargs kill"`);
}
6 changes: 6 additions & 0 deletions src/input-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ export function checkEnableHwKeyboard(enableHwKeyboard: string): void {
}
}

export function checkEnableLogcat(enableLogcat: string): void {
if (!isValidBoolean(enableLogcat)) {
throw new Error(`Input for input.enable-logcat should be either 'true' or 'false'.`);
}
}

export function checkLongPressTimeout(timeout: string): void {
if (isNaN(Number(timeout)) || !Number.isInteger(Number(timeout))) {
throw new Error(`Unexpected longpress-timeout: '${timeout}'.`);
Expand Down
12 changes: 10 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import {
checkDisableSpellchecker,
checkDisableAutofill,
checkLongPressTimeout,
checkEnableHwKeyboard
checkEnableHwKeyboard,
checkEnableLogcat
} from './input-validator';
import { launchEmulator, killEmulator } from './emulator-manager';
import * as exec from '@actions/exec';
Expand Down Expand Up @@ -77,6 +78,12 @@ async function run() {
const enableHwKeyboard = enableHwKeyboardInput === 'true';
console.log(`enable hw keyboard: ${enableHwKeyboard}`);

// enable logcat
const enableLogcatInput = core.getInput('enable-logcat');
checkEnableLogcat(enableLogcatInput);
const enableLogcat = enableLogcatInput === 'true';
console.log(`enable logcat: ${enableLogcat}`);

// disable spellchecker
const disableSpellcheckerInput = core.getInput('disable-spellchecker');
checkDisableSpellchecker(disableSpellcheckerInput);
Expand Down Expand Up @@ -149,7 +156,8 @@ async function run() {
disableSpellchecker,
disableAutofill,
longPressTimeout,
enableHwKeyboard
enableHwKeyboard,
enableLogcat
);

// execute the custom script
Expand Down

0 comments on commit e980520

Please sign in to comment.