Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify Script/build-webdriveragent.js #647

Merged
merged 6 commits into from
Dec 30, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'], {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll extend this to tvOS later as another pr

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);
mykola-mokhnach marked this conversation as resolved.
Show resolved Hide resolved
throw e;
}
LOG.info(`Zip bundled at "${appBundleZipPath}"`);
}

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