You are here

function module_builder_drush_output_code in Module Builder 7

Same name and namespace in other branches
  1. 6.2 drush/module_builder.drush.inc \module_builder_drush_output_code()

Output generated text, to terminal or to file.

2 calls to module_builder_drush_output_code()
module_builder_callback_info in drush/module_builder.drush.inc
Handles the callback for info code generation.
module_builder_callback_module in drush/module_builder.drush.inc
Handles the callback for module code generation.

File

drush/module_builder.drush.inc, line 351
Module builder drush commands.

Code

function module_builder_drush_output_code($module_root_name, $filename, $code) {

  // Output to terminal
  if (!drush_get_option('quiet')) {
    drush_print("Proposed {$filename}:");
    drush_print_r($code);
  }
  $write = drush_get_option('write');

  // Write to file
  // Add to file option implies write.
  // Write & go option implies write.
  if (drush_get_option(array(
    'write',
    'add',
    'go',
  ))) {
    $module_dir = pm_dl_destination('module');
    if (drush_get_option('parent')) {
      $parent_dir = drush_get_option('parent');
      if (substr($parent_dir, 0, 1) == '.') {
        $parent_dir = substr($parent_dir, 1);
        $module_dir = drush_get_context('DRUSH_OLDCWD') . '/' . $parent_dir . '/';
      }
      else {
        $module_dir .= $parent_dir;
      }
    }
    $module_dir .= $module_root_name;
    if (!is_dir($module_dir)) {
      @drush_op('mkdir', $module_dir, 0777);

      //drush_print("Module directory $module_dir created");
    }
    $filepath = $module_dir . '/' . $filename;

    // Add to file option
    // if file doesn't exist, we skip this and silently write it anyway
    if (drush_get_option('add') && file_exists($filepath)) {
      $fh = fopen($filepath, 'a');
      fwrite($fh, $code);
      fclose($fh);
      return;
    }

    // if file exists, ask for whether to overwrite
    if (file_exists($filepath)) {
      if (!drush_confirm(dt('File ' . $filename . ' exists. Do you really want to overwrite?'))) {
        return;
      }
    }
    file_put_contents($filepath, $code);
  }
}