Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pinning emulator build #14

Merged
merged 3 commits into from
Dec 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
target: google_apis
arch: x86
profile: Nexus 6
emulator-options: -no-window -no-snapshot -noaudio -no-boot-anim -camera-back none
emulator-options: -no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim -camera-back none
disable-animations: true
script: |
./gradlew help
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,9 @@ jobs:
| `target` | Optional | `default` | Target of the system image - `default` or `google_apis`. |
| `arch` | Optional | `x86` | CPU architecture of the system image - `x86` or `x86_64`. |
| `profile` | Optional | N/A | Hardware profile used for creating the AVD - e.g. `Nexus 6`. For a list of all profiles available, run `$ANDROID_HOME/tools/bin/avdmanager list` and refer to the results under "Available Android Virtual Devices". |
| `emulator-options` | Optional | See below | Command-line options used when launching the emulator (replacing all default options) - e.g. `-no-snapshot -camera-back emulated`. |
| `emulator-options` | Optional | See below | Command-line options used when launching the emulator (replacing all default options) - e.g. `-no-window -no-snapshot -camera-back emulated`. |
| `disable-animations` | Optional | `true` | Whether to disable animations - `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. |
| `script` | Required | N/A | Custom script to run - e.g. to run Android instrumented tests on the emulator: `./gradlew connectedCheck` |

Default `emulator-options`: `-no-window -no-snapshot -noaudio -no-boot-anim`.
Default `emulator-options`: `-no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim`.
23 changes: 23 additions & 0 deletions __tests__/input-validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,26 @@ describe('disable-animations validator tests', () => {
expect(func2).not.toThrow();
});
});

describe('emulator-build validator tests', () => {
it('Throws if emulator-build is not a number', () => {
const func = () => {
validator.checkEmulatorBuild('abc123');
};
expect(func).toThrowError(`Unexpected emulator build: 'abc123'.`);
});

it('Throws if emulator-build is not an integer', () => {
const func = () => {
validator.checkEmulatorBuild('123.123');
};
expect(func).toThrowError(`Unexpected emulator build: '123.123'.`);
});

it('Validates successfully with valid emulator-build', () => {
const func = () => {
validator.checkEmulatorBuild('6061023');
};
expect(func).not.toThrow();
});
});
6 changes: 4 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@ inputs:
profile:
description: 'Hardware profile used for creating the AVD - e.g. `Nexus 6`.'
emulator-options:
description: 'command-line options used when launching the emulator - e.g. `-no-snapshot -camera-back emulated`.'
default: '-no-window -no-snapshot -noaudio -no-boot-anim'
description: 'command-line options used when launching the emulator - e.g. `-no-window -no-snapshot -camera-back emulated`.'
default: '-no-window -gpu swiftshader_indirect -no-snapshot -noaudio -no-boot-anim'
disable-animations:
description: 'whether to disable animations - true or false'
default: 'true'
emulator-build:
description: 'build number of a specific version of the emulator binary to use e.g. `6061023` for emulator v29.3.0.0.'
script:
description: 'custom script to run - e.g. `./gradlew connectedCheck`'
required: true
Expand Down
6 changes: 6 additions & 0 deletions lib/input-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ function checkDisableAnimations(disableAnimations) {
}
}
exports.checkDisableAnimations = checkDisableAnimations;
function checkEmulatorBuild(emulatorBuild) {
if (isNaN(Number(emulatorBuild)) || !Number.isInteger(Number(emulatorBuild))) {
throw new Error(`Unexpected emulator build: '${emulatorBuild}'.`);
}
}
exports.checkEmulatorBuild = checkEmulatorBuild;
11 changes: 9 additions & 2 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,23 @@ function run() {
input_validator_1.checkDisableAnimations(disableAnimationsInput);
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);
// emulator build
const emulatorBuildInput = core.getInput('emulator-build');
if (emulatorBuildInput) {
input_validator_1.checkEmulatorBuild(emulatorBuildInput);
console.log(`using emulator build: ${emulatorBuildInput}`);
}
const emulatorBuild = !emulatorBuildInput ? undefined : emulatorBuildInput;
// custom script to run
const scriptInput = core.getInput('script', { required: true });
const scripts = script_parser_1.parseScript(scriptInput);
console.log(`Script:`);
scripts.forEach((script) => __awaiter(this, void 0, void 0, function* () {
console.log(`${script}`);
}));
// install SDK
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch, emulatorBuild);
try {
// install SDK
yield sdk_installer_1.installAndroidSdk(apiLevel, target, arch);
// launch an emulator
yield emulator_manager_1.launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disableAnimations);
}
Expand Down
17 changes: 14 additions & 3 deletions lib/sdk-installer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,22 @@ const BUILD_TOOLS_VERSION = '29.0.2';
* Installs & updates 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 arch, and target.
*/
function installAndroidSdk(apiLevel, target, arch) {
function installAndroidSdk(apiLevel, target, arch, emulatorBuild) {
return __awaiter(this, void 0, void 0, function* () {
const sdkmangerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`;
console.log('Installing latest build tools, platform tools, platform, and emulator.');
yield exec.exec(`sh -c \\"${sdkmangerPath} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' emulator > /dev/null"`);
console.log('Installing latest build tools, platform tools, and platform.');
yield exec.exec(`sh -c \\"${sdkmangerPath} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`);
if (emulatorBuild) {
console.log(`Installing emulator build ${emulatorBuild}.`);
yield exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-darwin-${emulatorBuild}.zip`);
yield exec.exec(`rm -rf ${process.env.ANDROID_HOME}/emulator`);
yield exec.exec(`unzip -q emulator.zip -d ${process.env.ANDROID_HOME}`);
yield exec.exec(`rm -f emulator.zip`);
}
else {
console.log('Installing latest emulator.');
yield exec.exec(`sh -c \\"${sdkmangerPath} --install emulator > /dev/null"`);
}
console.log('Installing system images.');
yield exec.exec(`sh -c \\"${sdkmangerPath} --install 'system-images;android-${apiLevel};${target};${arch}' > /dev/null"`);
});
Expand Down
6 changes: 6 additions & 0 deletions src/input-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,9 @@ export function checkDisableAnimations(disableAnimations: string): void {
throw new Error(`Input for input.disable-animations should be either 'true' or 'false'.`);
}
}

export function checkEmulatorBuild(emulatorBuild: string): void {
if (isNaN(Number(emulatorBuild)) || !Number.isInteger(Number(emulatorBuild))) {
throw new Error(`Unexpected emulator build: '${emulatorBuild}'.`);
}
}
16 changes: 12 additions & 4 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as core from '@actions/core';
import { installAndroidSdk } from './sdk-installer';
import { checkApiLevel, checkTarget, checkArch, checkDisableAnimations } from './input-validator';
import { checkApiLevel, checkTarget, checkArch, checkDisableAnimations, checkEmulatorBuild } from './input-validator';
import { launchEmulator, killEmulator } from './emulator-manager';
import * as exec from '@actions/exec';
import { parseScript } from './script-parser';
Expand Down Expand Up @@ -42,6 +42,14 @@ async function run() {
const disableAnimations = disableAnimationsInput === 'true';
console.log(`disable animations: ${disableAnimations}`);

// emulator build
const emulatorBuildInput = core.getInput('emulator-build');
if (emulatorBuildInput) {
checkEmulatorBuild(emulatorBuildInput);
console.log(`using emulator build: ${emulatorBuildInput}`);
}
const emulatorBuild = !emulatorBuildInput ? undefined : emulatorBuildInput;

// custom script to run
const scriptInput = core.getInput('script', { required: true });
const scripts = parseScript(scriptInput);
Expand All @@ -50,10 +58,10 @@ async function run() {
console.log(`${script}`);
});

try {
// install SDK
await installAndroidSdk(apiLevel, target, arch);
// install SDK
await installAndroidSdk(apiLevel, target, arch, emulatorBuild);

try {
// launch an emulator
await launchEmulator(apiLevel, target, arch, profile, emulatorOptions, disableAnimations);
} catch (error) {
Expand Down
16 changes: 13 additions & 3 deletions src/sdk-installer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,20 @@ const BUILD_TOOLS_VERSION = '29.0.2';
* Installs & updates 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 arch, and target.
*/
export async function installAndroidSdk(apiLevel: number, target: string, arch: string): Promise<void> {
export async function installAndroidSdk(apiLevel: number, target: string, arch: string, emulatorBuild?: string): Promise<void> {
const sdkmangerPath = `${process.env.ANDROID_HOME}/tools/bin/sdkmanager`;
console.log('Installing latest build tools, platform tools, platform, and emulator.');
await exec.exec(`sh -c \\"${sdkmangerPath} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' emulator > /dev/null"`);
console.log('Installing latest build tools, platform tools, and platform.');
await exec.exec(`sh -c \\"${sdkmangerPath} --install 'build-tools;${BUILD_TOOLS_VERSION}' platform-tools 'platforms;android-${apiLevel}' > /dev/null"`);
if (emulatorBuild) {
console.log(`Installing emulator build ${emulatorBuild}.`);
await exec.exec(`curl -fo emulator.zip https://dl.google.com/android/repository/emulator-darwin-${emulatorBuild}.zip`);
await exec.exec(`rm -rf ${process.env.ANDROID_HOME}/emulator`);
await exec.exec(`unzip -q emulator.zip -d ${process.env.ANDROID_HOME}`);
await exec.exec(`rm -f emulator.zip`);
} else {
console.log('Installing latest emulator.');
await exec.exec(`sh -c \\"${sdkmangerPath} --install emulator > /dev/null"`);
}
console.log('Installing system images.');
await exec.exec(`sh -c \\"${sdkmangerPath} --install 'system-images;android-${apiLevel};${target};${arch}' > /dev/null"`);
}