You are here

function drush_prebid_clone in Doubleclick for Publishers (DFP) 7.2

Checks out a git repository to the specified download location.

Parameters

@tag: The tag that will be checked out from the repository.

Return value

mixed The download location on success, FALSE otherwise.

1 string reference to 'drush_prebid_clone'
prebid_drush_command in prebid/prebid.drush.inc
Implementation of hook_drush_command().

File

prebid/prebid.drush.inc, line 76
prebid.drush.inc

Code

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);
}