{"size": 1892, "ext": "js", "max_stars_repo_path": "ci-jobs/scripts/build-webdriveragents.js", "max_stars_repo_name": "chunqian/WebDriverAgent", "max_stars_repo_stars_event_min_datetime": "2022-01-21T09:19:12.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-09T06:39:26.000Z", "max_issues_repo_path": "ci-jobs/scripts/build-webdriveragents.js", "max_issues_repo_name": "SonicCloudOrg/sonic-plugin-wda", "max_issues_repo_issues_event_min_datetime": "2022-01-31T11:34:26.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-10T12:38:13.000Z", "max_forks_repo_path": "ci-jobs/scripts/build-webdriveragents.js", "max_forks_repo_name": "SonicCloudOrg/sonic-plugin-wda", "max_forks_repo_forks_event_min_datetime": "2021-12-24T04:28:43.000Z", "max_forks_repo_forks_event_max_datetime": "2022-01-05T06:56:22.000Z"}
{"max_stars_count": 2, "max_issues_count": 3, "max_forks_count": 2, "avg_line_length": 32.6206896552, "max_line_length": 96, "alphanum_fraction": 0.6569767442}
const buildWebDriverAgent = require('./build-webdriveragent');
const { asyncify } = require('asyncbox');
const { fs, logger } = require('@appium/support');
const { exec } = require('teen_process');
const path = require('path');

const log = new logger.getLogger('WDABuild');

async function buildAndUploadWebDriverAgents () {
  // Get all xcode paths from /Applications/
  const xcodePaths = (await fs.readdir('/Applications/'))
    .filter((file) => file.toLowerCase().startsWith('xcode_'));

  // Determine which xcodes need to be skipped
  let excludedXcodeArr = (process.env.EXCLUDE_XCODE || '').replace(/\s/g, '').split(',');
  log.info(`Will skip xcode versions: '${excludedXcodeArr}'`);

  for (let xcodePath of xcodePaths) {
    if (xcodePath.includes('beta')) {
      log.info(`Skipping beta Xcode '${xcodePath}'`);
      continue;
    }

    // Skip if .0 because redundant (example: skip 11.4.0 because it already does 11.4)
    const [, , patch] = xcodePath.split('.');
    if (patch === '0') {
      log.info(`Skipping xcode '${xcodePath}'`);
      continue;
    }

    // Build webdriveragent for this xcode version
    log.info(`Running xcode-select for '${xcodePath}'`);
    await exec('sudo', ['xcode-select', '-s', `/Applications/${xcodePath}/Contents/Developer`]);
    const xcodeVersion = path.parse(xcodePath).name.split('_', 2)[1];

    if (excludedXcodeArr.includes(xcodeVersion)) {
      log.info(`Skipping xcode version '${xcodeVersion}'`);
      continue;
    }

    log.info('Building webdriveragent for xcode version', xcodeVersion);
    try {
      await buildWebDriverAgent(xcodeVersion);
    } catch (e) {
      log.error(`Skipping build for '${xcodeVersion} due to error: ${e}'`);
    }
  }

  // Divider log line
  log.info('\n');
}

if (require.main === module) {
  asyncify(buildAndUploadWebDriverAgents);
}

module.exports = buildAndUploadWebDriverAgents;
