[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/module_builder/drush/ -> module_builder.drush.inc (source)

   1  <?php
   2  
   3  /**
   4   * @file
   5   *  Module builder drush commands.
   6   *
   7   * IMPORTANT: This file should be identical across versions of Drupal.
   8   */
   9   
  10  /**
  11   * Initialization.
  12   *
  13   * @todo: is there a hook to move this to?
  14   */ 
  15  // Include common code.
  16  include_once(dirname(__FILE__) . '/../includes/common.inc');
  17  // Set our environment.
  18  define('MODULE_BUILDER_ENV', 'drush');
  19  
  20  /**
  21   * Implementation of hook_drush_command().
  22   *
  23   * In this hook, you specify which commands your
  24   * drush module makes available, what it does and 
  25   * description.
  26   *
  27   * Notice how this structure closely resembles how 
  28   * you define menu hooks.
  29   * 
  30   * @See drush_parse_command() for a list of recognized keys.
  31   *
  32   * @return
  33   *   An associative array describing your command(s).
  34   */
  35  function module_builder_drush_command() {
  36    $items = array();
  37  
  38    // the key in the $items array is the name of the command.
  39    $items['mb-build'] = array(
  40      'callback' => 'module_builder_callback_build',
  41      'description' => "Generate the code for a new Drupal module, including file headers and hook implementations.",
  42      'arguments' => array(
  43        'module name' => 'The machine name of the module.',
  44        'hooks' => 'Short names of hooks, separated by spaces.',
  45      ),
  46      'aliases' => array('mb'),
  47      'options' => array(
  48        '--noi' => "Disables interactive mode.",
  49        '--data' => "Location to read hook data. May be absolute, or relative to Drupal files dir. Defaults to 'files/hooks'.",
  50        '--build' => "Which file type to generate: 'all', 'code', 'info', 'FILE'. " .
  51          "'all' generates everything: info and any code files needed by the requested hooks. " .
  52          "'code' generates code files as needed. " .
  53          "'info' makes just the info file. " .
  54          "'module', 'install' make just the foo.module or foo.install files. " .
  55          "'If custom modules define other files to output, you can request those too, omitting the module root name part and any .inc extension, eg 'module_builder' for 'foo.module_builder.inc. " .
  56          "Default is 'all' if writing new files, 'code' if appending to file or outputting only to terminal.",
  57        '--write' => 'Write files to sites/all/modules. Will prompt to overwrite existing files; use --yes to force. Use --quiet to suppress output to the terminal.',
  58        '--go' => 'Write all module files and enable the new module. Take two commands into the shower? Not me.',
  59        '--add' => "Append hooks to module file. Implies '--write --build=code'. Warning: will not check hooks already exist.",
  60        '--name' => 'Readable name of the module.',
  61        '--desc' => 'Description (for the admin module list).',
  62        '--helptext' => 'Module help text (for the system help).',
  63        '--dep' => 'Dependencies, separated by spaces, eg "forum views".',
  64        '--package' => 'Module package.',
  65        '--parent' => "Name of a module folder to place this new module into; use if this module is to be added to an existing package. Use '.' for the current working directory.",
  66      ),
  67      'examples' => array(
  68        'drush mb my_module menu cron nodeapi' => 
  69          'Generate module code with hook_menu, hook_cron, hook_nodeapi.',
  70        'drush mb my_module --build=info --name="My module" --dep="forum views"' => 
  71          'Generate module info with readable name and dependencies.',
  72        'drush mb my_module menu cron --write --name="My module" --dep="forum views"' => 
  73          'Generate both module files, write files and also output to terminal.',
  74        'drush mb my_module menu cron --write ' => 
  75          'Generate module code, write files and also output to terminal.',
  76        'drush mb my_module menu cron --write --quiet --name="My module" --dep="forum views"' => 
  77          'Generate both module files, write files and output nothing to terminal.',
  78        'drush mb my_module menu cron --add'=> 
  79          'Generate code for hook_cron and add it to the existing my_module.module file.',
  80        'drush mb my_module menu cron --write --parent=cck'=> 
  81          'Generate both module files, write files to a folder my_module inside the cck folder.',
  82        'drush mb my_module menu cron --write --parent=.'=> 
  83          'Generate both module files, write files to a folder my_module in the current working directory.',
  84      ),
  85    );
  86    
  87    $items['mb-download'] = array(
  88      'callback' => 'module_builder_callback_hook_download',
  89      'description' => "Update module_builder hook data.",
  90      'options' => array(
  91        '--data' => "Location to save downloaded files. May be absolute, or relative to Drupal files dir. Defaults to 'files/hooks'.",
  92      ),
  93      'aliases' => array('mbdl'),
  94      //'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.    
  95    );
  96    
  97    $items['mb-list'] = array(
  98      'callback' => 'module_builder_callback_hook_list',
  99      'description' => "List the hooks module_builder knows about.",
 100    );
 101  
 102    $items['mb-analyze'] = array(
 103      'callback' => 'module_builder_callback_hook_analyze',
 104      'description' => "List the hooks found in a given module.",
 105      'aliases' => array('mban'),
 106    );
 107  
 108    $items['mb-dochooks'] = array(
 109      'callback' => 'module_builder_callback_doc_hooks',
 110      'description' => "Adds comment headers to hooks that need them in the given module.",
 111    );
 112  
 113    $items['mb-docparams'] = array(
 114      'callback' => 'module_builder_callback_doc_params',
 115      'description' => "Adds params... WIP!",
 116    );
 117    
 118    $items['mb-debug'] = array(
 119      'callback' => 'module_builder_callback_debug',
 120      'description' => "Debug module builder. Does whatever was needed at the time.",
 121      //'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
 122    );
 123  
 124    $items['mb-dir'] = array(
 125      'callback' => 'module_builder_callback_get_data_dir',
 126      'description' => "Print the location of the module builder data directory.",
 127    );
 128    
 129    return $items;
 130  }
 131  
 132  /**
 133   * Implementation of hook_drush_help().
 134   *
 135   * This function is called whenever a drush user calls
 136   * 'drush help <name-of-your-command>'
 137   *
 138   * @param
 139   *   A string with the help section (prepend with 'drush:')
 140   *
 141   * @return
 142   *   A string with the help text for your command.
 143   */
 144  function module_builder_drush_help($section) {
 145    switch ($section) {
 146      case 'drush:mb-build':
 147        return dt('Generates and optionally writes module code with the specified hooks. ' .
 148          'By default this runs in interactive mode, and will prompt you for each ' .
 149          "of the module's properties. Use the --noi option to use as a single command.");
 150    }
 151  }
 152  
 153  /**
 154   * Module builder drush command callback.
 155   *
 156   * Form:
 157   * $drush mb machine_name hookA hookB hookC
 158   * where 'hookA' is the short name, ie 'menu' not hook_menu'.
 159   */
 160  function module_builder_callback_build() {  
 161    $commands = func_get_args();
 162    
 163    // Build the module data.
 164    $module_data = module_builder_build_data($commands);
 165    
 166    // What to build
 167    $build = drush_get_option('build');
 168    
 169    // write options:
 170    // - all -- everything we can do
 171    // - code -- code files, not info (module + install _ ..?)
 172    // - info -- only info fole
 173    // - module -- only module file
 174    // - install -- only install file
 175    // - ??? whatever hooks need
 176      
 177    // No build: set nice default.
 178    if (!$build) {
 179      // If we are adding, 'code' is implied
 180      if (drush_get_option('add')) {
 181        $build = 'code';
 182      }
 183      // If we are writing or going, all.
 184      elseif (drush_get_option(array('write', 'go'))) {
 185        $build = 'all';
 186      }
 187      // Otherwise, outputting to terminal: only module
 188      else {
 189        $build = 'code';     
 190      }
 191    }
 192    
 193    //print_r($build);
 194    
 195    // Make a list  
 196    $build_list = explode(' ', $build);
 197    
 198    // Multi build: set a single string to switch on below.
 199    if (count($build_list) > 1) {
 200      $build = 'code';  
 201    }
 202    
 203    //print_r($build_list);  
 204    
 205    // Build files.
 206    // Include generating component file.
 207    module_builder_include('generate');
 208    // Build module code in all cases bar 'info'.
 209    if ($build != 'info') {
 210      // Check hook data file exists.
 211      if (!_module_builder_check_hook_data()) {
 212        return drush_set_error("DRUSH_NOT_COMPLETED', 'No hook definitions found. You need to download hook definitions before using this module: see the command 'mbdl'.");
 213      }
 214      
 215      module_builder_build_module($commands, $module_data, $build_list);        
 216    }
 217    // Build info code in cases 'info' and 'all'.
 218    if ($build == 'info' or $build == 'all') {
 219      module_builder_build_info($commands, $module_data);
 220    }
 221    
 222    /*
 223    switch ($build) {
 224      case 'info':
 225        // info and stop
 226        module_builder_callback_info($commands, $module_data);
 227        break;
 228      case 'all':
 229        // info and fall through
 230        module_builder_callback_info($commands, $module_data);
 231      case 'code':
 232        // this is just here to look pretty
 233      default:
 234        // anything else, eg module, install etc
 235        module_builder_callback_module($commands, $module_data, $build_list);    
 236    }
 237    */
 238  
 239    if (drush_get_option('go')) {
 240      pm_module_manage(array(array_shift($commands)), TRUE);
 241    }
 242  }
 243  
 244  /**
 245   * Helper function to build the array of module_data.
 246   */
 247  function module_builder_build_data($commands) {
 248    // Determine whether we're in interactive mode.
 249    $interactive = !drush_get_option(array('non-interactive', 'noi'));
 250  
 251    // Information about the keys we need to build the module data.
 252    $data = array(
 253      'module_root_name' => array(
 254        'commands' => 0,
 255        'prompt' => dt('module name'),
 256        'required' => TRUE,
 257      ),
 258      // It is essential this key follow the root name, so that the the root
 259      // name gets to the commands array first.
 260      'hooks' => array(
 261        'commands' => 'all',
 262        'prompt' => dt('required hooks'),
 263        'required' => TRUE,
 264      ),
 265      'module_readable_name' => array(
 266        'key' => 'name',
 267        'prompt' => dt('human readable name'),
 268        'required' => TRUE,
 269        // A callback to generate the default value of this key.
 270        // The signature is foo($commands, $module_data)
 271        'default_callback' => 'module_builder_default_readable_name',
 272      ),
 273      'module_short_description' => array(
 274        'key' => 'desc',
 275        'prompt' => dt('description'),
 276        'default' => 'TODO: Description of module',
 277      ),
 278      'module_help_text' => array(
 279        'key' => 'helptext',
 280        'prompt' => dt('help text'),
 281      ),
 282      'module_dependencies' => array(
 283        'key' => 'dep',
 284        'prompt' => dt('dependencies'),
 285      ),
 286      'module_package' => array(
 287        'key' => 'package',
 288        'prompt' => dt('package'),
 289      ),
 290    );
 291  
 292    foreach ($data as $name => $definition) {
 293      // Merge in default values.
 294      $definition += array(
 295        'required' => FALSE,
 296      );
 297      // First pass: get data from either drush command line options...
 298      if (isset($definition['key'])) {
 299        $module_data[$name] = drush_get_option($definition['key']);
 300      }
 301      // ... or the commands themselves.
 302      elseif (isset($definition['commands'])) {
 303        // A numeric value of 'commands' means take that index from the commands array.
 304        if (is_numeric($definition['commands']) && isset($commands[$definition['commands']])) {
 305          $module_data[$name] = $commands[$definition['commands']];
 306          unset($commands[$definition['commands']]);
 307        }
 308        // Otherwise, take the whole thing.
 309        // This depends on the module root name having been taken out first!
 310        else {
 311          $module_data[$name] = $commands;
 312        }
 313      }
 314  
 315      // Second pass: prompt the user for data.
 316      if ($interactive && empty($module_data[$name])) {
 317        $value = drush_prompt(dt('Enter the @type', array('@type' => $definition['prompt'])), NULL, $definition['required']);
 318        if ($value !== FALSE) {
 319          $module_data[$name] = $value;
 320        }
 321      }
 322      
 323      // Third pass: set a default value from the definition or a callback.
 324      if (empty($module_data[$name])) {
 325        if (isset($definition['default'])) {
 326          $module_data[$name] = $definition['default'];
 327          continue;
 328        }
 329        elseif (isset($definition['default_callback'])) {
 330          $function = $definition['default_callback'];
 331          $module_data[$name] = $function($commands, $module_data);
 332        }
 333      }
 334    }
 335    
 336    // Extra processing for the hooks array (or not).
 337    if (!is_array($module_data['hooks'])) {
 338      $module_data['hooks'] = preg_split('/\s+/', $module_data['hooks']);
 339    }
 340    // Convert the array from numeric to keyed by full hook name.
 341    foreach ($module_data['hooks'] as $hook_name) {
 342      $hooks["hook_$hook_name"] = TRUE;
 343    }
 344    $module_data['hooks'] = $hooks;
 345  
 346    //print_r($module_data);
 347    return $module_data;
 348  }
 349  
 350  /**
 351   * Callback for generating default for module readable name. 
 352   */
 353  function module_builder_default_readable_name($commands, $module_data) {
 354    return ucfirst(str_replace('_', ' ', $module_data['module_root_name']));
 355  }
 356  
 357  /**
 358   * Generates and outputs module code.
 359   *
 360   * @param $commands
 361   *    The drush array of commands.
 362   * @param $module_data
 363   *    An array of module data. Passed by reference so file information can
 364   *    be added by module_builder_generate_module().
 365   * @param $build_list
 366   *    An array of requested code files to output
 367   *    'code' or 'all' means all of them.
 368   */  
 369  function module_builder_build_module($commands, &$module_data, $build_list) {
 370    //drush_print_r($module_data);
 371    //exit;
 372    /*
 373    $input = drush_input('input?...');
 374    drush_print("You entered: >$input<");
 375    */
 376     
 377    // Generate all our code.
 378    $module_code = module_builder_generate_module($module_data, drush_get_option('add'));
 379    
 380    if (is_null($module_code)) {
 381      return drush_set_error('DRUSH_NOT_COMPLETED', 'No module code has been generated: perhaps you have specified invalid hook names or hooks this module does not know about.');
 382    }
 383  
 384    //print_r($build_list);  
 385    if (!in_array($build_list[0], array('code', 'all'))) {
 386      // We have keys in module_code that are entire filenames, eg 'foo.install'
 387      // We have array items in build_list that are sort of file endings, eg 'install'
 388      // Match them up!
 389      $requested_files = module_builder_requested_filenames($module_data['module_root_name'], array_keys($module_code), $build_list);
 390    }
 391    else {
 392      // Meh we want the lot.
 393      $requested_files = array_keys($module_code);
 394    }
 395    //print_r($requested_files);  
 396  
 397    foreach ($requested_files as $filename) {
 398      $code = $module_code[$filename];
 399      module_builder_drush_output_code($module_data['module_root_name'], $filename, $code);   
 400      
 401    }
 402    return;
 403  }
 404  
 405  /**
 406   * Figure out which of $real filenames are being requested in the list of $abbrev'iated ones.
 407   *
 408   * @return
 409   *  A flat array of filenames from $real. Those whose abreviations were not found.
 410   *  in $abbrev are removed.
 411   */
 412  function module_builder_requested_filenames($module_root_name, $real, $abbrev) {
 413    //print_r($real);
 414    
 415    foreach ($real as $r) {
 416      $p = preg_replace(
 417        array(
 418          "[^$module_root_name\.]", // module_name. at start
 419          '[\.inc$]'), // possibly .inc at end
 420        array('', ''),
 421        $r
 422      );
 423      $processed[$r] = $p;
 424      // build an array with the real names as keys
 425      // and the abbrevs as values 
 426    }
 427    //print_r($processed);
 428    //print_r($abbrev);
 429    
 430    // Intersecting thorws away values that are not in $abbrev
 431    // while keeping the real filename keys.
 432    $result = array_intersect($processed, $abbrev);
 433     
 434    //print_r($result); 
 435    // We only care about the keys anyway
 436    return array_keys($result);
 437  }
 438  
 439  /**
 440   * Generates and outputs info file code.
 441   */  
 442  function module_builder_build_info($commands, $module_data) {
 443    module_builder_include('generate');
 444    $info_code = module_builder_generate_info_oo($module_data);
 445    
 446    /*
 447    module_builder_include('generate_info');
 448    $info_code = module_builder_generate_info($module_data);
 449    */
 450  
 451    module_builder_drush_output_code($module_data['module_root_name'], $module_data['module_root_name'] . '.info', $info_code); 
 452  }
 453  
 454  /**
 455   * Output generated text, to terminal or to file.
 456   */
 457  function module_builder_drush_output_code($module_root_name, $filename, $code) {
 458      
 459    // Output to terminal
 460    if (!drush_get_option('quiet')) {
 461      drush_print("Proposed $filename:");
 462      drush_print_r($code);
 463    }
 464    
 465    $write = drush_get_option('write');
 466      
 467    // Write to file
 468    // Add to file option implies write.
 469    // Write & go option implies write.
 470    if (drush_get_option(array('write', 'add', 'go'))) {
 471      
 472      $module_dir = pm_dl_destination('module');
 473      
 474      // Gee great. Drush HEAD doesn't give us the trailing /.
 475      if (substr($module_dir, -1, 1) != '/') {
 476        $module_dir .= '/';
 477      }
 478  
 479      // Drush tries to put any module into 'contrib' if the folder exists;
 480      // hack this out and put the code in 'custom'.
 481      if (substr($module_dir, -8, 7) == 'contrib') {
 482        $module_dir_custom = substr_replace($module_dir, 'custom', -8, 7);
 483        if (is_dir($module_dir_custom)) {
 484          $module_dir = $module_dir_custom;
 485        }
 486      }
 487  
 488      if (drush_get_option('parent')) {
 489        // The --parent option allows the user to specify a location for the new module folder.
 490        $parent_dir = drush_get_option('parent');
 491        if (substr($parent_dir, 0 , 1) == '.') {
 492          // An initial . means to start from the current directory rather than 
 493          // the modules folder, which allows submodules to be created where the
 494          // user is standing.
 495          $module_dir = drush_get_context('DRUSH_OLDCWD') . '/';
 496          // Remove both the . and the following /.
 497          $parent_dir = substr($parent_dir, 2);
 498          if ($parent_dir) {
 499            // If there's anything left (since just '.' is a valid option), append it.
 500            $module_dir .= $parent_dir . '/';
 501          }
 502        }
 503        else {
 504          $module_dir .= $parent_dir . '/';
 505        }
 506      }
 507      // $module_dir should now be a full path to the parent of the destination
 508      // folder, with a trailing slash.
 509      $module_dir .= $module_root_name;
 510      
 511      if (!is_dir($module_dir)) {
 512        @drush_op('mkdir', $module_dir, 0777);
 513        //drush_print("Module directory $module_dir created");
 514      }
 515  
 516      $filepath = $module_dir . '/' . $filename;
 517      
 518      // Add to file option
 519      // if file doesn't exist, we skip this and silently write it anyway
 520      if (drush_get_option('add') && file_exists($filepath)) {
 521        $fh = fopen($filepath, 'a');
 522        fwrite($fh, $code);
 523        fclose($fh);
 524        return;
 525      }
 526  
 527      // if file exists, ask for whether to overwrite
 528      if (file_exists($filepath)) {
 529        if (!drush_confirm(dt('File ' . $filename . ' exists. Do you really want to overwrite?'))) {
 530          return; 
 531        }
 532      }
 533      
 534      file_put_contents($filepath, $code);
 535    }
 536  }
 537  
 538  /**
 539   * Ask the user for input. DOESN'T WORK.
 540   *
 541   * @param $msg The question to ask
 542   * @return The entered string.
 543   */
 544  function drush_input($msg, $required = FALSE, $indent = 0) {
 545    print str_repeat(' ', $indent) . (string)$msg . ": ";
 546  
 547    while ($line = trim(fgets(STDIN))) {
 548      if (!$required or strlen($line) > 0) {
 549        return $line;
 550      }
 551      print 'we never get here wtf?';
 552      print str_repeat(' ', $indent) . (string)$msg . ": ";
 553    }
 554  }
 555  
 556  /**
 557   * Callback for downloading hook data.
 558   */
 559  function module_builder_callback_hook_download() {
 560    $directory = _module_builder_get_hooks_directory();  
 561    $return = module_builder_update_data();
 562    if (!$return) {
 563      return drush_set_error('Problem downloading hooks.');
 564    }
 565    else {
 566      drush_print("Hook files have been downloaded to $directory and processed.");
 567    }
 568  }
 569  
 570  /** 
 571   * Callback to list known hooks.
 572   */
 573  function module_builder_callback_hook_list() {
 574    // Include generating component file.
 575    module_builder_include('process');
 576    
 577    $data = module_builder_get_hook_data();
 578    $time = module_builder_get_hook_data_last_updated();
 579  
 580    foreach ($data as $file => $hooks) {
 581      drush_print("Group $file:", 2);
 582      foreach ($hooks as $key => $hook) {
 583        drush_print($hook['name'] . ': ' . $hook['description'], 4);
 584      }
 585    }
 586    
 587    drush_print(t("Hook data retrieved from @dir.", array('@dir' => _module_builder_get_hooks_directory())));  
 588    drush_print(t("Hook data was processed on @time.", array('@time' => $time)));
 589  
 590    //print_r($data);
 591  }
 592  
 593  /**
 594   * Callback to list hook implementations found in a given module.
 595   */
 596  function module_builder_callback_hook_analyze() {
 597    $commands = func_get_args();
 598    
 599    // The first argument is the module machine name.
 600    $module_root_name = array_shift($commands);  
 601  
 602    // Include process component file.
 603    module_builder_include('process');
 604    $hooks = module_builder_get_hook_names(_module_builder_get_hooks_directory(), TRUE);
 605    foreach ($hooks as $key => $hook) {
 606      $hooks[$key] = $module_root_name . '_' . $hook;
 607    }
 608  
 609    $module_files = module_builder_get_module_files($module_root_name);
 610    //drush_print_r($module_files);
 611    
 612    foreach ($module_files as $file) {
 613      $functions = module_builder_get_functions($file);
 614      
 615      $module_hooks[$file] = array_intersect($functions, $hooks);
 616    }
 617    
 618    if (drush_get_option('flat')) {
 619  
 620    }  
 621    
 622    drush_print_r($module_hooks);
 623    drush_print_r(array_merge($module_hooks));
 624  }
 625  
 626  /** 
 627   * Callback to add doc headers to existing hooks.
 628   */
 629  function module_builder_callback_doc_hooks() {
 630    $commands = func_get_args();
 631    
 632    // The first argument is the module machine name.
 633    $module_root_name = array_shift($commands);  
 634    
 635    $module_files = module_builder_get_module_files($module_root_name);
 636    
 637    // Include component files.
 638    module_builder_include('process');
 639    module_builder_include('generate');
 640    
 641    $hook_names = module_builder_get_hook_names('short');
 642  
 643    $pattern = '[(?<! \* / \n )' . # no PHP doc: single quoted so \n works
 644      "function \ image_gallery _ ( \w * )  # function declaration: capture hook name
 645       ]mx";
 646      
 647    foreach ($module_files as $filename) {
 648      $code = file_get_contents($filepath . '/' . $filename);
 649      //print $code;
 650      
 651      // Get functions that have no docs.
 652      preg_match_all($pattern, $code, $function_names);
 653      
 654      // Get only those that are actual hooks.
 655      $bad_hooks = array_intersect($function_names[1], $hook_names);
 656      
 657      // For each hook that has no documentation.
 658      foreach ($bad_hooks as $hook_name) {
 659        $doc = module_builder_generate_hook_doxy("hook_$hook_name");
 660        $pattern2 = "[(?= function \ image_gallery _ $hook_name )]x";
 661        $code = preg_replace($pattern2, $doc, $code);
 662      }
 663  
 664      if (!drush_get_option('quiet')) {
 665        print $code;
 666      }
 667      
 668      print 'Added hook documentation headers for: ' . implode(', ', $bad_hooks) . "\n";
 669      if (!drush_confirm(dt('Are you sure you want to overwrite ' . $filename . '?'))) {
 670        continue; 
 671      }
 672      file_put_contents($filepath . '/' .$filename, $code);  
 673    }
 674  }
 675  
 676  /**
 677   * Callback to output the location of the data directory.
 678   */
 679  function module_builder_callback_get_data_dir() {
 680    drush_print('Module builder data is in ' . _module_builder_get_hooks_directory());
 681  }
 682  
 683  /**
 684   * WORK IN PROGRESS
 685   * Add function headers wherever needed with params.
 686   */
 687  function module_builder_callback_doc_params() {
 688    $commands = func_get_args();
 689    
 690    
 691    print 'wip!!!';
 692    return;
 693    
 694    // The first argument is the module machine name.
 695    $module_root_name = array_shift($commands);  
 696    
 697    $filepath = drupal_get_path('module', $module_root_name);
 698  
 699    //$old_dir = getcwd();
 700    //chdir($filepath);
 701    $files = scandir($filepath);
 702  
 703    foreach ($files as $filename) {
 704      $ext = substr(strrchr($filename, '.'), 1);
 705      if (in_array($ext, array('module', 'install', 'inc'))) {
 706        $module_files[] = $filename;
 707      }
 708    }
 709    
 710    // Include component files.
 711    module_builder_include('process');
 712    module_builder_include('generate');
 713    
 714    $hook_names = module_builder_get_hook_names('short');
 715  
 716    $pattern = '[
 717        / \* \* \n    # start phpdoc
 718        \ \* \ ( .* ) \n  # first line of phpdoc: capture the text
 719    (?: \ \* .* \n )* # lines of phpdoc
 720        \ \* /  \n    # end phpdoc
 721        function \ ( \w* ) \( ( .* ) \) \  { # function declaration: capture both entire declaration and name
 722    ]mx'; 
 723      
 724    foreach ($module_files as $filename) {
 725      $code = file_get_contents($filepath . '/' . $filename);
 726      //print $code;
 727      
 728      // Get functions that have no docs.
 729      preg_match_all($pattern, $code, $function_names);
 730      
 731      
 732      
 733      
 734      // Get only those that are actual hooks.
 735      //$bad_hooks = array_intersect($function_names[1], $hook_names);
 736      
 737      // For each hook that has no documentation.
 738      foreach ($bad_hooks as $hook_name) {
 739        $doc = module_builder_generate_hook_doxy("hook_$hook_name");
 740        $pattern2 = "[(?= function \ image_gallery _ $hook_name )]x";
 741        $code = preg_replace($pattern2, $doc, $code);
 742      }
 743  
 744      if (!drush_get_option('quiet')) {
 745       // print $code;
 746      }
 747      
 748      print 'Added hook documentation headers for: ' . implode(', ', $bad_hooks) . "\n";
 749      if (!drush_confirm(dt('Are you sure you want to overwrite ' . $filename . '?'))) {
 750        continue; 
 751      }
 752      //file_put_contents($filepath . '/' .$filename, $code);  
 753    }
 754  }
 755  
 756  /**
 757   * Just for testing stuff on the commandline while developing the module.
 758   */
 759  function module_builder_callback_debug() {
 760    
 761    /*
 762    include(dirname(__FILE__) . '/../includes/process.inc');
 763    include(dirname(__FILE__) . '/../includes/update.inc');
 764    
 765    module_builder_update_documentation();
 766    */
 767    
 768      
 769    return;  
 770  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7