Index: helpinject.drush.inc
===================================================================
RCS file: helpinject.drush.inc
diff -N helpinject.drush.inc
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ helpinject.drush.inc	31 Aug 2010 04:20:19 -0000
@@ -0,0 +1,154 @@
+<?php
+// $Id: $
+
+/**
+ * Implementation of hook_drush_help().
+ */
+function helpinject_drush_help($section) {
+  switch ($section) {
+    case 'drush:helpinject':
+      return dt('Generate Advanced Help files and contextual icons based on data in books.');
+  }
+}
+
+/**
+ * Implementation of hook_drush_command().
+ */
+function helpinject_drush_command() {
+  $items = array();
+
+  $items['helpinject'] = array(
+    'description' => "Generate Advanced Help files and contextual icons based on data in books.",
+    'examples' => array(
+      'drush helpinject example' => 'Add help from the book with the same human readable name to the example module.',
+      'drush helpinject --human_readable_name=Example --package_name=Examples example 21' => 'Generate a new help module from the book with nid 21.',
+      'drush helpinject --human_readable_name=Example_two --package_name=Examples --languages=fr,es example_two 22' => 'Generate a new help module from the book with nid 21, exporting French and Spanish Translations.',
+    ),
+    'arguments' => array(
+      'module' => 'Machine name of the module to generate help for. If a module by this name is installed locally, help will be added to that module. Otherwise, help will be generated as a new module.',
+      'book_ids' => 'A space delimited list of one or more book IDs to be used for help. If no book_ids are given, if there is a book with the same name as the human-readable name of the designated module, that book will be used. Otherwise, a list of available books will be returned.',
+    ),
+    'options' => array(
+      '--module_readable_name' => "The human readable name of a new module that will be generated.",
+      '--package_name' => "The package name that groups modules on the admin/build/modules page.",
+      '--languages' => "Comma delimited list of languages to be exported.",
+      '--destination' => "Directory that new module should be written into.",
+    ),
+    'drupal dependencies' => array('helpinject'),
+    'aliases' => array('hi'),
+  );
+
+  return $items;
+}
+
+
+/**
+ * Clone an existing feature module.
+ */
+function drush_helpinject() {
+  if ($book_ids = func_get_args()) {
+    $module_info = array('module_base_name' => array_shift($book_ids));
+    $drupal_root = drush_get_context('DRUSH_DRUPAL_ROOT');
+    // Determine if module exists.
+    if ($filename = drush_db_result(drush_db_select('system', array('filename'), "name = :name AND type = 'module'", array(':name' => $module_info['module_base_name'])))) {
+      $directory = $drupal_root . '/' . dirname($filename);
+      $info = drupal_parse_info_file($directory . '/' . $module_info['module_base_name'] . '.info');
+      $module_info['module_readable_name'] = $info['name'];
+
+      // Back up existing module before making changes.
+      // We can't call drush_pm_version_control_backup::pre_update because
+      // that moves and we want to copy. So this code is adapted from pre_update.
+
+      // Save the date to be used in the backup directory's path name.
+      $date = date('YmdHis', $_SERVER['REQUEST_TIME']);
+
+      $backup_dir = drush_get_option('backup-dir', $drupal_root  . '/backup');
+      $backup_dir = rtrim($backup_dir, '/');
+      @drush_op('mkdir', $backup_dir, 0777);
+      $backup_dir .= '/modules';
+      @drush_op('mkdir', $backup_dir, 0777);
+      $backup_dir .= "/$date";
+      @drush_op('mkdir', $backup_dir, 0777);
+      $backup_target = $backup_dir . '/'. $module_info['module_base_name'];
+      if (!drush_op('copy', $directory, $backup_target)) {
+        return drush_set_error('DRUSH_PM_BACKUP_FAILED', dt('Failed to backup project directory !project to !backup_target', array('!project' => $directory, '!backup_target' => $backup_target)));
+      }
+    }
+    else {
+      $directory = pm_dl_destination('module') . '/' . $module_info['module_base_name'];
+      $module_info['module_readable_name'] = drush_get_option('module_readable_name');
+      $module_info['package_name'] = drush_get_option('package_name');
+    }
+    $languages = explode(',', drush_get_option('languages'));
+    if (empty($book_ids)) {
+      // Look for a book with the same title as the module readable name.
+      if ($bid = drush_db_result(drush_db_select('node', array('nid'), "title = :title AND type = 'book'", array(':title' => $module_info['module_readable_name'])))) {
+        $book_ids[] = $bid;
+      }
+      else {
+        $rows = array(array(dt('Book ID'), dt('Title')));
+        $result = drush_db_select('node', array('nid', 'title'), "type = 'book'", NULL, NULL, NULL, 'nid');
+        while ($book = drush_db_fetch_object($result)) {
+          $rows[] = array($book->nid, $book->title);
+        }
+        drush_print(dt('Available books'));
+        drush_print_table($rows, TRUE);
+        return;
+      }
+    }
+    // If there is no readable name set, use the title of the first book.
+    if (empty($module_info['module_readable_name'])) {
+      $book = node_load($book_ids[0]);
+      $module_info['module_readable_name'] = strip_tags($book->title);
+    }
+    $result = helpinject_generate($module_info, $book_ids, $languages);
+    // Bail if any errors were returned.
+    foreach ($return['messages'] as $message) {
+      if (isset($message['type']) && $message['type'] == 'error') {
+        return drush_set_error('HELPINJECT_GENERATE_FAILED', dt('Errors encountered in trying to generate help files: !message', array('!message' => $message['message'])));
+      }
+    }
+    // Confirm before overwriting existing help.
+    if (is_dir($directory . '/help')) {
+      drush_print(dt('Help appears to already exist in !dir', array('!dir' => $directory)));
+      if (!drush_confirm(dt('Do you really want to continue?'))) {
+        drush_die('Aborting.');
+      }
+    }
+    // Copy over the generated files.
+    drush_op('rename', $drupal_root . '/' . $return['dir'], $directory);
+
+    // If needed, merge in additions.
+    foreach (array('module', 'info') as $extension) {
+      $file = $directory . '/' . $module_info['module_base_name'] . $extension;
+      $additions_file = $file . '.additions';
+      if (file_exists($additions_file)) {
+        $contents = file_get_contents($file);
+        if (strpos('helpinjector', $contents) === FALSE) {
+          $additions = file_get_contents($additions_file);
+          if ($extension == 'module') {
+            $contents .= $additions;
+            drush_op('file_put_contents', $file, $contents);
+          }
+          else {
+            // Try to add the dependency information in a suitable place in the .info file.
+            foreach (array('dependencies', 'description') as $key) {
+              if (($position = strpos($contents, $key)) !== FALSE) {
+                $contents = substr_replace($contents, $additions, $position, 0);
+                drush_op('file_put_contents', $file, $contents);
+                break;
+              }
+            }
+          }
+        }
+      }
+      drush_op('unlink', $additions_file);
+    }
+    $readme = $directory . '/helpinject.readme.txt';
+    if (file_exists($readme)) {
+      drush_op('unlink', $directory . '/helpinject.readme.txt');
+    }
+    drush_print(dt('Help information written to !directory', array('!directory', $directory)));
+  }
+}
+

