diff --git a/bulk_export/bulk_export.module b/bulk_export/bulk_export.module index cfa5bbc..66f8aff 100644 --- a/bulk_export/bulk_export.module +++ b/bulk_export/bulk_export.module @@ -36,8 +36,13 @@ function bulk_export_menu() { /** * FAPI gateway to the bulk exporter. + * + * @param $cli + * Whether this function is called from command line. + * @param $options + * A collection of options, only passed in by drush_ctools_export(). */ -function bulk_export_export() { +function bulk_export_export($cli = FALSE, $options = array()) { ctools_include('export'); $schemas = ctools_export_get_schemas(TRUE); $exportables = $export_tables = array(); @@ -58,14 +63,35 @@ function bulk_export_export() { 'no_redirect' => TRUE, 'exportables' => $exportables, 'export_tables' => $export_tables, + 'name' => '', + 'code' => '', ); - $output = drupal_build_form('bulk_export_export_form', $form_state); - if (!empty($form_state['submitted'])) { + + // If called from drush_ctools_export, get the module name and + // select all exportables and call the submit function directly. + if ($cli) { + $module_name = $options['module']; + $form_state['values']['name'] = $module_name; + $form_state['values']['tables'] = array(); + foreach ($exportables as $table => $names) { + $form_state['values']['tables'][$table] = array(); + foreach ($names as $name => $title) { + $form_state['values']['tables'][$table][$name] = $name; + } + } + $output = bulk_export_export_form_submit($form, $form_state); + } + else { + $output = drupal_build_form('bulk_export_export_form', $form_state); + $module_name = $form_state['module']; + } + + if (!empty($form_state['submitted']) || $cli) { drupal_set_title(t('Bulk export results')); $output = ''; $module_code = ''; $api_code = array(); - $dependencies = array(); + $dependencies = $file_data = array(); foreach ($form_state['code'] as $module => $api_info) { if ($module == 'general') { $module_code .= $api_info; @@ -81,15 +107,20 @@ function bulk_export_export() { $api_code[$api_hook] .= " }\n"; $dependencies[$module] = TRUE; - $file = $form_state['module'] . '.' . $api . '.inc'; + $file = $module_name . '.' . $api . '.inc'; $code = " $file))); - $output .= drupal_render($export_form); + if ($cli) { + $file_data[$file] = $code; + } + else { + $export_form = drupal_get_form('ctools_export_form', $code, t('Place this in @file', array('@file' => $file))); + $output .= drupal_render($export_form); + } } } } @@ -100,7 +131,7 @@ function bulk_export_export() { $api = "\n/**\n"; $api .= " * Implements hook_$api_hook().\n"; $api .= " */\n"; - $api .= "function $form_state[module]_$api_hook(\$module, \$api) {\n"; + $api .= "function {$module_name}_$api_hook(\$module, \$api) {\n"; $api .= $text; $api .= "}\n"; $module_code = $api . $module_code; @@ -114,8 +145,13 @@ function bulk_export_export() { $module .= " * Bulk export of objects generated by Bulk export module.\n"; $module .= " */\n"; $module .= $module_code; - $export_form = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array('@file' => $form_state['module'] . '.module'))); - $output = drupal_render($export_form) . $output; + if ($cli) { + $file_data[$module_name . '.module'] = $module; + } + else { + $export_form = drupal_get_form('ctools_export_form', $module, t('Place this in @file', array('@file' => $form_state['module'] . '.module'))); + $output = drupal_render($export_form) . $output; + } } $info = strtr("name = @module export module\n", array('@module' => $form_state['module'])); @@ -125,12 +161,21 @@ function bulk_export_export() { } $info .= "package = Chaos tool suite\n"; $info .= "core = 7.x\n"; - $export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info'))); - $output = drupal_render($export_form) . $output; - + if ($cli) { + $file_data[$module_name . '.info'] = $info; + } + else { + $export_form = drupal_get_form('ctools_export_form', $info, t('Place this in @file', array('@file' => $form_state['module'] . '.info'))); + $output = drupal_render($export_form) . $output; + } } - return $output; + if ($cli) { + return $file_data; + } + else { + return $output; + } } else { return t('There are no objects to be exported at this time.'); diff --git a/drush/ctools.drush.inc b/drush/ctools.drush.inc new file mode 100644 index 0000000..0b5b4c8 --- /dev/null +++ b/drush/ctools.drush.inc @@ -0,0 +1,104 @@ + array('ctex'), + 'description' => 'Export multiple CTools-handled data objects directly to code.', + 'arguments' => array( + 'module' => 'Name of your module.', + ), + 'options' => array( + 'subdir' => 'The name of the sub directory to create the module in. Defaults to ctools_export which will be placed into sites/all/modules.', + ), + 'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL, + 'drupal dependencies' => array('bulk_export'), + 'examples' => array( + 'drush ctex export_module' => 'Export CTools exportables to a module called "export_module".', + ), + ); + + return $items; +} + +/** + * Drush callback: export + */ +function drush_ctools_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; + } + + // Subdirectory. + $dest_exists = FALSE; + $subdir = drush_get_option('subdir', 'ctools_export'); + $dest = 'sites/all/modules/' . $subdir . '/' . $module; + + // Check if module or folder exists. + if (module_exists($module) || file_exists($dest)) { + $dest_exists = TRUE; + if (drush_confirm(dt('Are you sure to overwrite files in folder !module ? An extra confirmation will be asked for the module file.', array('!module' => $module)))) { + drush_log(dt('Files will be overwritten'), 'success'); + } + else { + drush_log(dt('Export aborted.'), 'success'); + return; + } + } + + // 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); + + // 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( + 'module' => $module, + ); + $files = bulk_export_export(TRUE, $options); + + // 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' && 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($data); + 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'); + } +} \ No newline at end of file