View source
<?php
use Drush\Log\LogLevel;
function prebid_drush_help($section) {
switch ($section) {
case 'meta:prebid:title':
return dt('Prebid commands');
case 'meta:prebid:summary':
return dt('Build and download the prebid.js library.');
}
}
function prebid_drush_command() {
$items = array();
$items['prebid-clone'] = array(
'description' => 'Clone the Prebid.js library.',
'examples' => array(
'drush prebid-clone 1.23.0' => 'Clone the Prebid.js library from Github at tag 1.23.0',
),
'arguments' => array(
'tag' => 'The tag for the version you need.',
),
'required-arguments' => 1,
'callback' => 'drush_prebid_clone',
'aliases' => array(
'pc',
),
);
$items['prebid-build'] = array(
'description' => 'Build a custom version of the Prebid.js library.\\n
You will need node and npm installed to build the prebid.js file.',
'examples' => array(
'drush prebid-build ixBidAdapter,openxBidAdapter,rubiconBidAdapter' => 'Build a bundle with these bid adapters.',
),
'arguments' => array(
'adapters' => 'You must supply a comma separated list of adapters. e.g., ixBidAdapter,openxBidAdapter,rubiconBidAdapter',
),
'options' => array(
'dev' => 'Builds an un-minified version for debugging.',
),
'required-arguments' => 1,
'callback' => 'drush_prebid_build',
'aliases' => array(
'pb',
),
);
$items['prebid-add-library'] = array(
'description' => 'Move the prebid library to sites/all/libraries/',
'examples' => array(
'drush prebid-add-library' => 'Move the generated prebid.js file to libraries.',
),
'callback' => 'drush_prebid_add_library',
'aliases' => array(
'pal',
),
);
return $items;
}
function drush_prebid_clone($tag) {
$prebidjs_info = libraries_info('prebidjs');
$git_url = $prebidjs_info['git url'];
$name = 'Prebid.js';
if (empty($tag)) {
make_error('DOWNLOAD_ERROR', dt("You must provide a tag for the repository: @git_url.", array(
'@git_url' => $git_url,
)));
return FALSE;
}
$prebid_module_path = drupal_get_path('module', 'prebid');
drush_log(dt('The Prebid.js path: @path.', array(
'@path' => $prebid_module_path,
)), LogLevel::OK);
$final_path = getcwd() . '/' . $prebid_module_path . '/js/prebid';
$directory_created = drush_mkdir($final_path);
if (!$directory_created) {
make_error('DOWNLOAD_ERROR', dt("Couldn't create the @path directory.", array(
'@path' => $final_path,
)));
return FALSE;
}
$cache = !drush_get_option('no-cache', FALSE);
$checkout_after_clone = TRUE;
if ($cache && ($git_cache = drush_directory_cache('git'))) {
$project_cache = $git_cache . '/' . $name . '-' . md5($git_url);
if (!file_exists($project_cache)) {
$command = 'git clone --mirror';
if (drush_get_context('DRUSH_VERBOSE')) {
$command .= ' --verbose --progress';
}
$command .= ' %s %s';
drush_shell_cd_and_exec($git_cache, $command, $git_url, $project_cache);
}
else {
drush_shell_cd_and_exec($project_cache, 'git remote update');
}
$git_cache = $project_cache;
}
$tmp_location = drush_tempdir();
$command = 'git clone %s %s';
if (drush_get_context('DRUSH_VERBOSE')) {
$command .= ' --verbose --progress';
}
if ($cache) {
$command .= ' --reference ' . drush_escapeshellarg($git_cache);
}
if (!drush_shell_exec($command, $git_url, $tmp_location)) {
make_error('DOWNLOAD_ERROR', dt('Unable to clone @project from @url.', array(
'@project' => $name,
'@url' => $git_url,
)));
return FALSE;
}
drush_log(dt('@project cloned from @url.', array(
'@project' => $name,
'@url' => $git_url,
)), LogLevel::OK);
if ($checkout_after_clone) {
$cwd = getcwd();
chdir($tmp_location);
if (drush_shell_exec("git checkout %s", 'refs/tags/' . $tag)) {
drush_log(dt("Checked out tag: @tag.", array(
'@tag' => $tag,
)), LogLevel::OK);
}
else {
make_error('DOWNLOAD_ERROR', dt("Unable to check out tag @tag.", array(
'@tag' => $tag,
)));
}
chdir($cwd);
}
$clone_copied = drush_copy_dir($tmp_location, $final_path, FILE_EXISTS_OVERWRITE);
if (!$clone_copied) {
return $clone_copied;
}
drush_log(dt("Cloned to: @location", array(
'@location' => $final_path,
)), LogLevel::OK);
return dirname($final_path);
}
function drush_prebid_build($adapters) {
$library = drupal_get_path('module', 'prebid') . '/js/prebid';
$package = $library . '/package.json';
if (!file_exists($package)) {
make_error('DOWNLOAD_ERROR', dt("Couldn't find the Prebid.js library. Run 'drush help prebid-clone'."));
return FALSE;
}
if (drush_get_option('dev')) {
$command = 'npx gulp build-bundle-dev --modules=%s';
}
else {
$command = 'npx gulp build-bundle-prod --modules=%s';
}
drush_log(dt('Moving to the Prebid cloned directory'), LogLevel::OK);
chdir($library);
drush_log(dt('Running npm install...'), LogLevel::OK);
drush_shell_exec('npm install');
$actual_command = str_replace('%s', $adapters, $command);
drush_log(dt('Running: @command', array(
'@command' => $actual_command,
)), LogLevel::OK);
drush_shell_exec($command, $adapters);
}
function drush_prebid_add_library() {
$libraries_path = 'sites/all/libraries/prebidjs';
$library = drupal_get_path('module', 'prebid') . '/js/prebid';
$package = $library . '/package.json';
if (!file_exists($package)) {
make_error('DOWNLOAD_ERROR', dt("Couldn't find the Prebid.js library. Run 'drush help prebid-clone'."));
return FALSE;
}
if (!file_exists($libraries_path)) {
drush_mkdir($libraries_path);
}
$build_path = $library . '/build';
if (!file_exists($build_path)) {
make_error('DOWNLOAD_ERROR', dt("Couldn't find the Prebid.js library. Run 'drush help prebid-build'."));
return FALSE;
}
$dev = $build_path . '/dev';
if (file_exists($dev)) {
$file_path = $dev . '/prebid.js';
}
else {
$file_path = $build_path . '/dist/prebid.js';
}
$command = 'cp ' . $file_path . ' ' . $libraries_path . '/';
drush_log(dt('Copying the built prebid.js file to @libraries_path', array(
'@libraries_path' => $libraries_path,
)), LogLevel::OK);
drush_shell_exec($command);
}