build-webdriveragent.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. const path = require('path');
  2. const { asyncify } = require('asyncbox');
  3. const { logger, fs } = require('@appium/support');
  4. const { exec } = require('teen_process');
  5. const xcode = require('appium-xcode');
  6. const LOG = new logger.getLogger('WDABuild');
  7. const ROOT_DIR = path.resolve(__dirname, '..');
  8. const DERIVED_DATA_PATH = `${ROOT_DIR}/wdaBuild`;
  9. const WDA_BUNDLE = 'WebDriverAgentRunner-Runner.app';
  10. const WDA_BUNDLE_PATH = path.join(DERIVED_DATA_PATH, 'Build', 'Products', 'Debug-iphonesimulator');
  11. const WDA_BUNDLE_TV = 'WebDriverAgentRunner_tvOS-Runner.app';
  12. const WDA_BUNDLE_TV_PATH = path.join(DERIVED_DATA_PATH, 'Build', 'Products', 'Debug-appletvsimulator');
  13. const TARGETS = ['runner', 'tv_runner'];
  14. const SDKS = ['sim', 'tv_sim'];
  15. async function buildWebDriverAgent (xcodeVersion) {
  16. const target = process.env.TARGET;
  17. const sdk = process.env.SDK;
  18. if (!TARGETS.includes(target)) {
  19. throw Error(`Please set TARGETS environment variable from the supported targets ${JSON.stringify(TARGETS)}`);
  20. }
  21. if (!SDKS.includes(sdk)) {
  22. throw Error(`Please set SDK environment variable from the supported SDKs ${JSON.stringify(SDKS)}`);
  23. }
  24. LOG.info(`Cleaning ${DERIVED_DATA_PATH} if exists`);
  25. try {
  26. await exec('xcodebuild', ['clean', '-derivedDataPath', DERIVED_DATA_PATH, '-scheme', 'WebDriverAgentRunner'], {
  27. cwd: ROOT_DIR
  28. });
  29. } catch (ign) {}
  30. // Get Xcode version
  31. xcodeVersion = xcodeVersion || await xcode.getVersion();
  32. LOG.info(`Building WebDriverAgent for iOS using Xcode version '${xcodeVersion}'`);
  33. // Clean and build
  34. try {
  35. await exec('/bin/bash', ['./Scripts/build.sh'], {
  36. env: {TARGET: target, SDK: sdk, DERIVED_DATA_PATH},
  37. cwd: ROOT_DIR
  38. });
  39. } catch (e) {
  40. LOG.error(`===FAILED TO BUILD FOR ${xcodeVersion}`);
  41. LOG.error(e.stderr);
  42. throw e;
  43. }
  44. const isTv = target === 'tv_runner';
  45. const bundle = isTv ? WDA_BUNDLE_TV : WDA_BUNDLE;
  46. const bundle_path = isTv ? WDA_BUNDLE_TV_PATH : WDA_BUNDLE_PATH;
  47. const zipName = `WebDriverAgentRunner-Runner-${sdk}-${xcodeVersion}.zip`;
  48. LOG.info(`Creating ${zipName} which includes ${bundle}`);
  49. const appBundleZipPath = path.join(ROOT_DIR, zipName);
  50. await fs.rimraf(appBundleZipPath);
  51. LOG.info(`Created './${zipName}'`);
  52. try {
  53. await exec('xattr', ['-cr', bundle], {cwd: bundle_path});
  54. await exec('zip', ['-qr', appBundleZipPath, bundle], {cwd: bundle_path});
  55. } catch (e) {
  56. LOG.error(`===FAILED TO ZIP ARCHIVE`);
  57. LOG.error(e.stderr);
  58. throw e;
  59. }
  60. LOG.info(`Zip bundled at "${appBundleZipPath}"`);
  61. }
  62. if (require.main === module) {
  63. asyncify(buildWebDriverAgent);
  64. }
  65. module.exports = buildWebDriverAgent;