Skip to content

Commit

Permalink
chore: simplify Script/build-webdriveragent.js (#647)
Browse files Browse the repository at this point in the history
* chore: simplify Script/build-webdriveragent.js

* rename the file name

* chore: use UPPER_CASE for const

* use native zip

* use xcodebuild clean
  • Loading branch information
KazuCocoa authored Dec 30, 2022
1 parent 6118c3f commit 81dab6c
Showing 1 changed file with 33 additions and 68 deletions.
101 changes: 33 additions & 68 deletions Scripts/build-webdriveragent.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,53 @@
const path = require('path');
const os = require('os');
const { asyncify } = require('asyncbox');
const { logger, fs, mkdirp, zip } = require('@appium/support');
const { logger, fs } = require('@appium/support');
const { exec } = require('teen_process');
const xcode = require('appium-xcode');

const log = new logger.getLogger('WDABuild');
const rootDir = path.resolve(__dirname, '..');
const LOG = new logger.getLogger('WDABuild');
const ROOT_DIR = path.resolve(__dirname, '..');
const DERIVED_DATA_PATH = `${ROOT_DIR}/wdaBuild`;
const WDA_BUNDLE = 'WebDriverAgentRunner-Runner.app';
const WDA_BUNDLE_PATH = path.join(DERIVED_DATA_PATH, 'Build', 'Products', 'Debug-iphonesimulator');

async function buildWebDriverAgent (xcodeVersion) {
LOG.info(`Cleaning ${DERIVED_DATA_PATH} if exists`);
try {
await exec('xcodebuild', ['clean', '-derivedDataPath', DERIVED_DATA_PATH, '-scheme', 'WebDriverAgentRunner'], {
cwd: ROOT_DIR
});
} catch (ign) {}

// Get Xcode version
xcodeVersion = xcodeVersion || await xcode.getVersion();
log.info(`Building bundle for Xcode version '${xcodeVersion}'`);

// Clear WebDriverAgent from derived data
const derivedDataPath = path.resolve(os.homedir(), 'Library', 'Developer',
'Xcode', 'DerivedData');
log.info(`Clearing contents of '${derivedDataPath}/WebDriverAgent-*'`);
for (const wdaPath of
await fs.glob('WebDriverAgent-*', {cwd: derivedDataPath, absolute: true})
) {
log.info(`Deleting existing WDA: '${wdaPath}'`);
await fs.rimraf(wdaPath);
}
LOG.info(`Building WebDriverAgent for iOS using Xcode version '${xcodeVersion}'`);

// Clean and build
log.info('Running ./Scripts/build.sh');
let env = {TARGET: 'runner', SDK: 'sim'};
try {
await exec('/bin/bash', ['./Scripts/build.sh'], {env, cwd: rootDir});
await exec('/bin/bash', ['./Scripts/build.sh'], {
env: {TARGET: 'runner', SDK: 'sim', DERIVED_DATA_PATH},
cwd: ROOT_DIR
});
} catch (e) {
log.error(`===FAILED TO BUILD FOR ${xcodeVersion}`);
log.error(e.stdout);
log.error(e.stderr);
log.error(e.message);
LOG.error(`===FAILED TO BUILD FOR ${xcodeVersion}`);
LOG.error(e.stderr);
throw e;
}

// Create bundles folder
const pathToBundles = path.resolve(rootDir, 'bundles');
await mkdirp(pathToBundles);

// Start creating zip
const uncompressedDir = path.resolve(rootDir, 'uncompressed');
await fs.rimraf(uncompressedDir);
await mkdirp(uncompressedDir);
log.info('Creating zip');

// Move contents of the root to folder called "uncompressed"
await exec('rsync', [
'-av', '.', uncompressedDir,
'--exclude', 'node_modules',
'--exclude', 'build',
'--exclude', 'ci-jobs',
'--exclude', 'lib',
'--exclude', 'test',
'--exclude', 'bundles',
'--exclude', 'azure-templates',
], {cwd: rootDir});

// Move DerivedData/WebDriverAgent-* from Library to "uncompressed" folder
const wdaPath = (await fs.glob(`${derivedDataPath}/WebDriverAgent-*`))[0];
await mkdirp(path.resolve(uncompressedDir, 'DerivedData'));
await fs.rename(wdaPath, path.resolve(uncompressedDir, 'DerivedData', 'WebDriverAgent'));

// Compress the "uncompressed" bundle as a Zip
const pathToZip = path.resolve(pathToBundles, `webdriveragent-xcode_${xcodeVersion}.zip`);
await zip.toArchive(
pathToZip, {cwd: path.join(rootDir, 'uncompressed')}
);
log.info(`Zip bundled at "${pathToZip}"`);

// Now just zip the .app and place it in the root directory
// This zip file will be published to NPM
const wdaAppBundle = 'WebDriverAgentRunner-Runner.app';
const appBundlePath = path.join(uncompressedDir, 'DerivedData', 'WebDriverAgent',
'Build', 'Products', 'Debug-iphonesimulator', wdaAppBundle);
const appBundleZipPath = path.join(rootDir, `${wdaAppBundle}.zip`);
const zipName = `WebDriverAgentRunner-Runner-Sim-${xcodeVersion}.zip`;
LOG.info(`Creating ${zipName} which includes ${WDA_BUNDLE}`);
const appBundleZipPath = path.join(ROOT_DIR, zipName);
await fs.rimraf(appBundleZipPath);
log.info(`Created './${wdaAppBundle}.zip'`);
await zip.toArchive(appBundleZipPath, {cwd: appBundlePath});
log.info(`Zip bundled at "${appBundleZipPath}"`);
// Clean up the uncompressed directory
await fs.rimraf(uncompressedDir);
LOG.info(`Created './${zipName}'`);
try {
await exec('xattr', ['-cr', WDA_BUNDLE], {cwd: WDA_BUNDLE_PATH});
await exec('zip', ['-qr', appBundleZipPath, WDA_BUNDLE], {cwd: WDA_BUNDLE_PATH});
} catch (e) {
LOG.error(`===FAILED TO ZIP ARCHIVE`);
LOG.error(e.stderr);
throw e;
}
LOG.info(`Zip bundled at "${appBundleZipPath}"`);
}

if (require.main === module) {
Expand Down

0 comments on commit 81dab6c

Please sign in to comment.