Example #1
0
 /**
  * Prints release notes for given projects.
  *
  * @param string $version
  *   Version of the release to get notes.
  * @param bool $print_status
  *   Whether to print a informative note.
  * @param string $tmpfile
  *   If provided, a file that contains contents to show before the
  *   release notes.
  */
 function getReleaseNotes($version = NULL, $print_status = TRUE, $tmpfile = NULL)
 {
     $project_name = $this->parsed['short_name'];
     if (!isset($tmpfile)) {
         $tmpfile = drush_tempnam('rln-' . $project_name . '.');
     }
     // Select versions to show.
     $versions = array();
     if (!is_null($version)) {
         $versions[] = $version;
     } else {
         // If requested project is installed,
         // show release notes for the installed version and all newer versions.
         if (isset($this->parsed['recommended'], $this->parsed['installed'])) {
             $releases = array_reverse($this->parsed['releases']);
             foreach ($releases as $version => $release) {
                 if ($release['date'] >= $this->parsed['releases'][$this->parsed['installed']]['date']) {
                     $release += array('version_extra' => '');
                     $this->parsed['releases'][$this->parsed['installed']] += array('version_extra' => '');
                     if ($release['version_extra'] == 'dev' && $this->parsed['releases'][$this->parsed['installed']]['version_extra'] != 'dev') {
                         continue;
                     }
                     $versions[] = $version;
                 }
             }
         } else {
             // Project is not installed and user did not specify a version,
             // so show the release notes for the recommended version.
             $versions[] = $this->parsed['recommended'];
         }
     }
     foreach ($versions as $version) {
         if (!isset($this->parsed['releases'][$version]['release_link'])) {
             drush_log(dt("Project !project does not have release notes for version !version.", array('!project' => $project_name, '!version' => $version)), LogLevel::WARNING);
             continue;
         }
         // Download the release node page and get the html as xml to explore it.
         $release_link = $this->parsed['releases'][$version]['release_link'];
         $filename = drush_download_file($release_link, drush_tempnam($project_name));
         @($dom = \DOMDocument::loadHTMLFile($filename));
         if ($dom) {
             drush_log(dt("Successfully parsed and loaded the HTML contained in the release notes' page for !project (!version) project.", array('!project' => $project_name, '!version' => $version)), LogLevel::INFO);
         } else {
             drush_log(dt("Error while requesting the release notes page for !project project.", array('!project' => $project_name)), LogLevel::ERROR);
             continue;
         }
         $xml = simplexml_import_dom($dom);
         // Extract last update time and the notes.
         $last_updated = $xml->xpath('//div[contains(@class,"views-field-changed")]');
         $last_updated = $last_updated[0]->asXML();
         $notes = $xml->xpath('//div[contains(@class,"field-name-body")]');
         $notes = !empty($notes) ? $notes[0]->asXML() : dt("There're no release notes.");
         // Build the notes header.
         $header = array();
         $header[] = '<hr>';
         $header[] = dt("> RELEASE NOTES FOR '!name' PROJECT, VERSION !version:", array('!name' => strtoupper($project_name), '!version' => $version));
         $header[] = dt("> !last_updated.", array('!last_updated' => trim(drush_html_to_text($last_updated))));
         if ($print_status) {
             $header[] = '> ' . implode(', ', $this->parsed['releases'][$version]['release_status']);
         }
         $header[] = '<hr>';
         // Finally add the release notes for the requested project to the tmpfile.
         $content = implode("\n", $header) . "\n" . $notes . "\n";
         #TODO# accept $html as a method argument
         if (!drush_get_option('html', FALSE)) {
             $content = drush_html_to_text($content, array('br', 'p', 'ul', 'ol', 'li', 'hr'));
         }
         file_put_contents($tmpfile, $content, FILE_APPEND);
     }
     #TODO# don't print! Just return the filename
     drush_print_file($tmpfile);
 }
/**
 * Automatically download project dependencies at pm-enable time.
 * Use a pre-pm_enable hook to download before your module is enabled,
 * or a post-pm_enable hook (drush_hook_post_pm_enable) to run after
 * your module is enabled.
 *
 * Your hook will be called every time pm-enable is executed; you should
 * only download dependencies when your module is being enabled.  Respect 
 * the --skip flag, and take no action if it is present.
 */
function drush_hook_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('hook', $modules) && !drush_get_option('skip')) {
        $url = 'http://server.com/path/MyLibraryName.tgz';
        $path = drush_get_context('DRUSH_DRUPAL_ROOT');
        if (module_exists('libraries')) {
            $path .= '/' . libraries_get_path('MyLibraryName') . '/MyLibraryName.tgz';
        } else {
            $path .= '/' . drupal_get_path('module', 'hook') . '/MyLibraryName.tgz';
        }
        drush_download_file($url, $path) && drush_tarball_extract($path);
    }
}