You are here

function module_builder_drush_output_code in Module Builder 6.2

Same name and namespace in other branches
  1. 7 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_build_info in drush/module_builder.drush.inc
Generates and outputs info file code.
module_builder_build_module in drush/module_builder.drush.inc
Generates and outputs module code.

File

drush/module_builder.drush.inc, line 457
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');

    // Gee great. Drush HEAD doesn't give us the trailing /.
    if (substr($module_dir, -1, 1) != '/') {
      $module_dir .= '/';
    }

    // Drush tries to put any module into 'contrib' if the folder exists;
    // hack this out and put the code in 'custom'.
    if (substr($module_dir, -8, 7) == 'contrib') {
      $module_dir_custom = substr_replace($module_dir, 'custom', -8, 7);
      if (is_dir($module_dir_custom)) {
        $module_dir = $module_dir_custom;
      }
    }
    if (drush_get_option('parent')) {

      // The --parent option allows the user to specify a location for the new module folder.
      $parent_dir = drush_get_option('parent');
      if (substr($parent_dir, 0, 1) == '.') {

        // An initial . means to start from the current directory rather than
        // the modules folder, which allows submodules to be created where the
        // user is standing.
        $module_dir = drush_get_context('DRUSH_OLDCWD') . '/';

        // Remove both the . and the following /.
        $parent_dir = substr($parent_dir, 2);
        if ($parent_dir) {

          // If there's anything left (since just '.' is a valid option), append it.
          $module_dir .= $parent_dir . '/';
        }
      }
      else {
        $module_dir .= $parent_dir . '/';
      }
    }

    // $module_dir should now be a full path to the parent of the destination
    // folder, with a trailing slash.
    $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);
  }
}