You are here

prebid.drush.inc in Doubleclick for Publishers (DFP) 7.2

File

prebid/prebid.drush.inc
View source
<?php

/**
 * @file prebid.drush.inc
 *
 */
use Drush\Log\LogLevel;

/**
 * Implementation of hook_drush_help().
 */
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.');
  }
}

/**
 * Implementation of hook_drush_command().
 */
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;
}

/**
 * Checks out a git repository to the specified download location.
 *
 * @param @tag
 *   The tag that will be checked out from the repository.
 *
 * @return mixed
 *   The download location on success, FALSE otherwise.
 */
function drush_prebid_clone($tag) {
  $prebidjs_info = libraries_info('prebidjs');
  $git_url = $prebidjs_info['git url'];
  $name = 'Prebid.js';

  // Bail early if no tag is provided.
  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);

    // Set up a new cache, if it doesn't exist.
    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 {

      // Update the --mirror clone.
      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);
  }

  // Before we can checkout anything, we need to clone the repository.
  // The placeholders, %s, will be replaced with the arguments passed after
  // $command.
  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) {

    // Get the current directory (so we can move back later).
    $cwd = getcwd();

    // Change into the working copy of the cloned repo.
    chdir($tmp_location);

    // At this time we only allow a tag.
    // This becomes: 'git checkout refs/tags/[TAG]'.
    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,
      )));
    }

    // Move back to last current directory (first line).
    chdir($cwd);
  }

  // Move the directory into the final resting location.
  $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);
}

/**
 * Builds the prebid.js file with all specified adapters.
 *
 * @param @adapters
 *   Comma separated list of bid adapters that should be included in the
 *   bundled prebid.js file.
 *
 * @return mixed
 *   The download location on success, FALSE otherwise.
 */
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');

  // Show the actual command.
  $actual_command = str_replace('%s', $adapters, $command);
  drush_log(dt('Running: @command', array(
    '@command' => $actual_command,
  )), LogLevel::OK);
  drush_shell_exec($command, $adapters);
}

/**
 * Moves the prebid.js file to libraries.
 */
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);
}

Functions

Namesort descending Description
drush_prebid_add_library Moves the prebid.js file to libraries.
drush_prebid_build Builds the prebid.js file with all specified adapters.
drush_prebid_clone Checks out a git repository to the specified download location.
prebid_drush_command Implementation of hook_drush_command().
prebid_drush_help Implementation of hook_drush_help().