Skip to content

Commit

Permalink
feat: add building espresso server command via appium command (#858)
Browse files Browse the repository at this point in the history
* add espresso build command

* rename

* add TEST_APP_PACKAGE

* accept Espresso Build Config

* tweak console

* remove npm run

* tweak the readme

* enable gradle log only then the given env was 1 or true, tweak logs

* tweak
  • Loading branch information
KazuCocoa authored Mar 22, 2023
1 parent f5064a7 commit cfaad9a
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 3 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,12 @@ On top of standard Appium requirements Espresso driver also expects the followin

## Scripts

- `appium driver run print-espresso-path` prints the path to the Appium Espresso server root. You can modify the gradle file directly if [Espresso Build Config](#espresso-build-config) was not sufficient.
- `appium driver run espresso print-espresso-path` prints the path to the Appium Espresso server root. You can modify the gradle file directly if [Espresso Build Config](#espresso-build-config) was not sufficient.
- `appium driver run espresso build-espresso` builds the espresso server since driver version 2.18.0. It helps building the espresso server outside of the Appium process. Available environment variables are below:
- `SHOW_GRADLE_LOG` configures if the command shows the gradle task logs. `true` or `1` sets it as enabled, but others set it as disabled. Defaults to disabled.
- `TEST_APP_PACKAGE` configures the target application to build the espresso server for.
- `ESPRESSO_BUILD_CONFIG` is an absolute path to the [Espresso Build Config](#espresso-build-config) as JSON format file.
- e.g. `SHOW_GRADLE_LOG=true TEST_APP_PACKAGE=your.test.pkg ESPRESSO_BUILD_CONFIG=/path/to/the/config.json appium driver run build-espresso`

## Capabilities

Expand Down Expand Up @@ -1259,4 +1264,3 @@ more details.
* Espresso server unit tests are located at `io.appium.espressoserver.test` and can be run in Android Studio
* NodeJS unit tests are run with `npm run test`
* End-to-end tests are run with `npm run e2e-test` (remember to run `npm run build` before running this command so that it has up-to-date Espresso Server and NodeJS code)

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
],
"mainClass": "EspressoDriver",
"scripts": {
"print-espresso-path": "./scripts/print-espresso-path.js"
"print-espresso-path": "./scripts/print-espresso-path.js",
"build-espresso.js": "./scripts/build-espresso.js"
}
},
"main": "./build/index.js",
Expand Down
56 changes: 56 additions & 0 deletions scripts/build-espresso.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const _ = require('lodash');
const path = require('path');
const { logger, fs } = require('@appium/support');
const { ServerBuilder } = require('../build/lib/server-builder.js');

const LOG = new logger.getLogger('EspressoBuild');

const ROOT_DIR = path.resolve(__dirname, '..');
const ESPRESSO_SERVER_ROOT = path.join(ROOT_DIR, 'espresso-server');

const ESPRESSO_SERVER_BUILD = path.join(ESPRESSO_SERVER_ROOT, 'app', 'build');

async function buildEspressoServer () {

LOG.info(`Deleting the build directory ${ESPRESSO_SERVER_BUILD}`);

const opts = {
serverPath: ESPRESSO_SERVER_ROOT,
showGradleLog: !_.isEmpty(process.env.SHOW_GRADLE_LOG) && ['1', 'true'].includes(_.toLower(process.env.SHOW_GRADLE_LOG))
};

if (process.env.TEST_APP_PACKAGE) {
opts.testAppPackage = process.env.TEST_APP_PACKAGE;
}

if (process.env.ESPRESSO_BUILD_CONFIG) {
if (!(await fs.exists(process.env.ESPRESSO_BUILD_CONFIG))) {
throw new Error(`'${process.env.ESPRESSO_BUILD_CONFIG}' did not exist. Please set the path as an absolute path.`);
}
try {
const buildConfigurationStr = await fs.readFile(process.env.ESPRESSO_BUILD_CONFIG, 'utf8');
opts.buildConfiguration = JSON.parse(buildConfigurationStr);
LOG.info(`The espresso build config is ${JSON.stringify(opts.buildConfiguration)}`);
} catch (e) {
throw new Error(`Failed to parse the ${process.env.ESPRESSO_BUILD_CONFIG}. `
`Please make sure that the JSON is valid format. Error: ${e}`);
}
}

const builder = new ServerBuilder(LOG, opts);
try {
await builder.build();
} catch (e) {
throw new Error(`Failed to build the espresso server. `
`SHOW_GRADLE_LOG=true environment variable helps to check the gradle log. Error: ${e}`);
}

const dstPath = path.resolve(ESPRESSO_SERVER_ROOT, 'app', 'build', 'outputs', 'apk', 'androidTest', 'debug', 'app-debug-androidTest.apk');
if (await fs.exists(dstPath)) {
LOG.info(`Full path to the server APK: ${dstPath}`);
} else {
LOG.info(`Full path to the server build folder: ${ESPRESSO_SERVER_BUILD}`);
}
}

(async () => await buildEspressoServer())();

0 comments on commit cfaad9a

Please sign in to comment.