You are here

function ctools_drush_export in Chaos Tool Suite (ctools) 7

Drush callback: export.

1 string reference to 'ctools_drush_export'
ctools_drush_command in drush/ctools.drush.inc
Implements hook_drush_command().

File

drush/ctools.drush.inc, line 152
CTools Drush commands.

Code

function ctools_drush_export($module = 'foo') {
  $error = FALSE;
  if (preg_match('@[^a-z_]+@', $module)) {
    $error = dt('The name of the module must contain only lowercase letters and underscores') . '.';
    drush_log($error, 'error');
    return;
  }

  // Filter by tables.
  $tables = _ctools_drush_explode_options('tables');

  // Check status.
  $filter = drush_get_option('filter', FALSE);
  if (empty($filter)) {
    drush_set_option('filter', 'enabled');
  }

  // Selection.
  $options = array(
    'all' => dt('Export everything'),
    'select' => dt('Make selection'),
  );
  $selection = drush_choice($options, dt('Select to proceed'));
  if (!$selection) {
    return;
  }

  // Present the selection screens.
  if ($selection == 'select') {
    $selections = _ctools_drush_selection_screen($tables);
  }
  else {
    $info = _ctools_drush_export_info($tables, TRUE);
    $selections = $info['exportables'];
  }

  // Subdirectory.
  $dest_exists = FALSE;
  $subdir = drush_get_option('subdir', 'ctools_export');
  $dest = 'sites/all/modules/' . $subdir . '/' . $module;

  // Overwriting files can be set with 'remove' argument.
  $remove = drush_get_option('remove', FALSE);

  // Check if folder exists.
  if (file_exists($dest)) {
    $dest_exists = TRUE;
    if ($remove) {
      if (drush_confirm(dt('All files except for the .info and .module files in !module will be removed. You can choose later if you want to overwrite these files as well. Are you sure you want to proceed ?', array(
        '!module' => $module,
      )))) {
        $remove = TRUE;
        drush_log(dt('Files will be removed'), 'success');
      }
      else {
        drush_log(dt('Export aborted.'), 'success');
        return;
      }
    }
  }

  // Remove files (except for the .module file) if the destination folder exists.
  if ($remove && $dest_exists) {
    _ctools_drush_file_delete($dest);
  }

  // Create new dir if needed.
  if (!$dest_exists) {
    if (!file_exists('sites/all/modules/' . $subdir)) {
      drush_mkdir('sites/all/modules/' . $subdir);
    }
  }

  // Create destination directory.
  drush_mkdir($dest);

  // Load bulk export module.
  module_load_include('module', 'bulk_export');

  // Create options and call Bulk export function.
  // We create an array, because maybe in the future we can pass in more
  // options to the export function (pre-selected modules and/or exportables).
  $options = array(
    'name' => $module,
    'selections' => $selections,
  );
  $files = bulk_export_export(TRUE, $options);
  $alter = array(
    'module' => $module,
    'files' => $files,
  );

  // Let other drush commands alter the files.
  drush_command_invoke_all_ref('drush_ctex_files_alter', $alter);
  $files = $alter['files'];

  // Start writing.
  if (is_array($files)) {
    foreach ($files as $base_file => $data) {
      $filename = $dest . '/' . $base_file;

      // Extra check for .module file.
      if ($base_file == ($module . '.module' || $module . '.info') && file_exists($filename)) {
        if (!drush_confirm(dt('Do you want to overwrite !module_file', array(
          '!module_file' => $base_file,
        )))) {
          drush_log(dt('Writing of !filename skipped. This is the code that was supposed to be written:', array(
            '!filename' => $filename,
          )), 'success');
          drush_print('---------');
          drush_print(shellColours::getColouredOutput("\n{$data}", 'light_green'));
          drush_print('---------');
          continue;
        }
      }
      if (file_put_contents($filename, $data)) {
        drush_log(dt('Succesfully written !filename', array(
          '!filename' => $filename,
        )), 'success');
      }
      else {
        drush_log(dt('Error writing !filename', array(
          '!filename' => $filename,
        )), 'error');
      }
    }
  }
  else {
    drush_log(dt('No files were found to be written.'), 'error');
  }
}