|
| 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