Skip to content

Commit cfaad9a

Browse files
authored
feat: add building espresso server command via appium command (#858)
* 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
1 parent f5064a7 commit cfaad9a

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,12 @@ On top of standard Appium requirements Espresso driver also expects the followin
4242

4343
## Scripts
4444

45-
- `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.
45+
- `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.
46+
- `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:
47+
- `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.
48+
- `TEST_APP_PACKAGE` configures the target application to build the espresso server for.
49+
- `ESPRESSO_BUILD_CONFIG` is an absolute path to the [Espresso Build Config](#espresso-build-config) as JSON format file.
50+
- 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`
4651

4752
## Capabilities
4853

@@ -1259,4 +1264,3 @@ more details.
12591264
* Espresso server unit tests are located at `io.appium.espressoserver.test` and can be run in Android Studio
12601265
* NodeJS unit tests are run with `npm run test`
12611266
* 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)
1262-

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
],
4040
"mainClass": "EspressoDriver",
4141
"scripts": {
42-
"print-espresso-path": "./scripts/print-espresso-path.js"
42+
"print-espresso-path": "./scripts/print-espresso-path.js",
43+
"build-espresso.js": "./scripts/build-espresso.js"
4344
}
4445
},
4546
"main": "./build/index.js",

scripts/build-espresso.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
const _ = require('lodash');
2+
const path = require('path');
3+
const { logger, fs } = require('@appium/support');
4+
const { ServerBuilder } = require('../build/lib/server-builder.js');
5+
6+
const LOG = new logger.getLogger('EspressoBuild');
7+
8+
const ROOT_DIR = path.resolve(__dirname, '..');
9+
const ESPRESSO_SERVER_ROOT = path.join(ROOT_DIR, 'espresso-server');
10+
11+
const ESPRESSO_SERVER_BUILD = path.join(ESPRESSO_SERVER_ROOT, 'app', 'build');
12+
13+
async function buildEspressoServer () {
14+
15+
LOG.info(`Deleting the build directory ${ESPRESSO_SERVER_BUILD}`);
16+
17+
const opts = {
18+
serverPath: ESPRESSO_SERVER_ROOT,
19+
showGradleLog: !_.isEmpty(process.env.SHOW_GRADLE_LOG) && ['1', 'true'].includes(_.toLower(process.env.SHOW_GRADLE_LOG))
20+
};
21+
22+
if (process.env.TEST_APP_PACKAGE) {
23+
opts.testAppPackage = process.env.TEST_APP_PACKAGE;
24+
}
25+
26+
if (process.env.ESPRESSO_BUILD_CONFIG) {
27+
if (!(await fs.exists(process.env.ESPRESSO_BUILD_CONFIG))) {
28+
throw new Error(`'${process.env.ESPRESSO_BUILD_CONFIG}' did not exist. Please set the path as an absolute path.`);
29+
}
30+
try {
31+
const buildConfigurationStr = await fs.readFile(process.env.ESPRESSO_BUILD_CONFIG, 'utf8');
32+
opts.buildConfiguration = JSON.parse(buildConfigurationStr);
33+
LOG.info(`The espresso build config is ${JSON.stringify(opts.buildConfiguration)}`);
34+
} catch (e) {
35+
throw new Error(`Failed to parse the ${process.env.ESPRESSO_BUILD_CONFIG}. `
36+
`Please make sure that the JSON is valid format. Error: ${e}`);
37+
}
38+
}
39+
40+
const builder = new ServerBuilder(LOG, opts);
41+
try {
42+
await builder.build();
43+
} catch (e) {
44+
throw new Error(`Failed to build the espresso server. `
45+
`SHOW_GRADLE_LOG=true environment variable helps to check the gradle log. Error: ${e}`);
46+
}
47+
48+
const dstPath = path.resolve(ESPRESSO_SERVER_ROOT, 'app', 'build', 'outputs', 'apk', 'androidTest', 'debug', 'app-debug-androidTest.apk');
49+
if (await fs.exists(dstPath)) {
50+
LOG.info(`Full path to the server APK: ${dstPath}`);
51+
} else {
52+
LOG.info(`Full path to the server build folder: ${ESPRESSO_SERVER_BUILD}`);
53+
}
54+
}
55+
56+
(async () => await buildEspressoServer())();

0 commit comments

Comments
 (0)