I made a mistake when I was testing the pre-pm-enable hook - I was using a module that did not have any requirements. When I tried to make a pre-enable hook for git_deploy, I discovered that Drush is checking the requirements before the extension has a chance to download its dependencies. :(

The attached patch fixes this problem. With this patch applied, the following git_deploy Drush extension works:

git_deploy.drush.inc:

function drush_git_deploy_pre_pm_enable() {
  // Get the list of modules being enabled; only download dependencies if our module name appears in the list
  $modules = drush_get_context('PM_ENABLE_MODULES');
  if (in_array('git_deploy', $modules) && !drush_get_option('skip')) {
    $path = drush_get_context('DRUSH_DRUPAL_ROOT');
    if (module_exists('libraries')) {
      $path .= '/' . libraries_get_path('glip');
    }
    else {
      $path .= '/'. drupal_get_path('module', 'git_deploy');
      $path = realpath($path . '/../..') . '/libraries/glip';
    }
    if (is_dir($path . '/lib')) {
      drush_log(dt("Found glip at !path; download not necessary.", array('!path' => $path)), 'ok');
    }
    else {
      drush_log(dt("Checking out glip to !path.", array('!path' => $path)), 'ok');
      drush_mkdir($path);
      drush_shell_cd_and_exec($path . '/..', 'git clone git://github.com/halstead/glip.git');
      drush_shell_cd_and_exec($path, 'git checkout 1.1');
    }
  }
}
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

moshe weitzman’s picture

Not so sure about this. It is basically lying to move this requirements validation to the main callback. Maybe the policy needs to implement _init() hook or something.

greg.1.anderson’s picture

Fair enough. I moved the extension-sorting and commandfile-enabling to pm_enable_init, and put the requirements check back in pm_enable_validate. Note, however, that the drush extension in the module being enabled is not eligible to be called until the _validate stage, because its .drush.inc file is not loaded until pm_enable_init adds it. Commandfiles added in one phase are not called until the start of the next phase, so the module will now need to implement the _validate hook. To make sure that the module that is being enabled will get to run its _validate hook before pm_enable_validate is called to check requirements, I added a high-priority mode to _drush_add_commandfiles. This causes the newly-added files to be placed at the start of the commandfile list, so they will get first shot at the next phase. The attached patch also updates the docs in drush.api.php to show the new, working recipe to download requirements in advance.

The example shown in #0 still works as written if you rename the function from drush_git_deploy_pre_pm_enable to drush_git_deploy_pm_enable_validate.

greg.1.anderson’s picture

Here's the patch.

greg.1.anderson’s picture

... and again with whitespace error fixed.

greg.1.anderson’s picture

The prioritized commandfile feature is not sitting well with me. I think that one of the following alternate solutions would be better:

A. Add a "hook_pre_validate" for all Drush commands. Maybe only pm-enable needs it.

B. Have pm_enable_validate call a pre-requirements hook just before it checks the requirements for the modules being enabled.

"B" was the original suggestion, and I think it's best.

moshe weitzman’s picture

I could go for either approach, but I think A could be genuinely useful in a number of situations where commandfiles have to alter args/options before validation starts.

moshe weitzman’s picture

Assigned: Unassigned » greg.1.anderson
Category: bug » task
Status: Needs review » Active

I just committed 4bb725c which adds a hook_pre_validate as per A in #5.

Assigning to Greg so he can change any code in Drush or git_deploy thats needs changing.

greg.1.anderson’s picture

Title: Drush pre-pm-enable hook does not work right » Update git_deploy and other contrib modules use hook_pre_validate to download dependencies

Setting title for my future reference.

greg.1.anderson’s picture

Version: » 8.x-6.x-dev
Status: Active » Fixed

git_deploy 1.x stopped doing requirements checks, and implemented the automatic download in drush_hook_post_pm_enable, just like devel. In git_deploy 2.x, the git executable is used, removing the need for the library altogether.

I submitted #2082149: Automatically download jquery.imgareaselect on `drush pm-enable media_crop` to media_crop. Going to go ahead and mark this as 'fixed'; there are certainly other contrib modules in the world that could use this, but there's no point trying to track them here.

greg.1.anderson’s picture

Status: Fixed » Closed (won't fix)
Issue tags: +Needs migration

This issue was marked closed (won't fix) because Drush has moved to Github.

If desired, you may copy this task to our Github project and then post a link here to the new issue. Please also change the status of this issue to closed (duplicate).

Please ask support questions on Drupal Answers.