You are here

function module_builder_build_data in Module Builder 6.2

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

Helper function to build the array of module_data.

1 call to module_builder_build_data()
module_builder_callback_build in drush/module_builder.drush.inc
Module builder drush command callback.

File

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

Code

function module_builder_build_data($commands) {

  // Determine whether we're in interactive mode.
  $interactive = !drush_get_option(array(
    'non-interactive',
    'noi',
  ));

  // Information about the keys we need to build the module data.
  $data = array(
    'module_root_name' => array(
      'commands' => 0,
      'prompt' => dt('module name'),
      'required' => TRUE,
    ),
    // It is essential this key follow the root name, so that the the root
    // name gets to the commands array first.
    'hooks' => array(
      'commands' => 'all',
      'prompt' => dt('required hooks'),
      'required' => TRUE,
    ),
    'module_readable_name' => array(
      'key' => 'name',
      'prompt' => dt('human readable name'),
      'required' => TRUE,
      // A callback to generate the default value of this key.
      // The signature is foo($commands, $module_data)
      'default_callback' => 'module_builder_default_readable_name',
    ),
    'module_short_description' => array(
      'key' => 'desc',
      'prompt' => dt('description'),
      'default' => 'TODO: Description of module',
    ),
    'module_help_text' => array(
      'key' => 'helptext',
      'prompt' => dt('help text'),
    ),
    'module_dependencies' => array(
      'key' => 'dep',
      'prompt' => dt('dependencies'),
    ),
    'module_package' => array(
      'key' => 'package',
      'prompt' => dt('package'),
    ),
  );
  foreach ($data as $name => $definition) {

    // Merge in default values.
    $definition += array(
      'required' => FALSE,
    );

    // First pass: get data from either drush command line options...
    if (isset($definition['key'])) {
      $module_data[$name] = drush_get_option($definition['key']);
    }
    elseif (isset($definition['commands'])) {

      // A numeric value of 'commands' means take that index from the commands array.
      if (is_numeric($definition['commands']) && isset($commands[$definition['commands']])) {
        $module_data[$name] = $commands[$definition['commands']];
        unset($commands[$definition['commands']]);
      }
      else {
        $module_data[$name] = $commands;
      }
    }

    // Second pass: prompt the user for data.
    if ($interactive && empty($module_data[$name])) {
      $value = drush_prompt(dt('Enter the @type', array(
        '@type' => $definition['prompt'],
      )), NULL, $definition['required']);
      if ($value !== FALSE) {
        $module_data[$name] = $value;
      }
    }

    // Third pass: set a default value from the definition or a callback.
    if (empty($module_data[$name])) {
      if (isset($definition['default'])) {
        $module_data[$name] = $definition['default'];
        continue;
      }
      elseif (isset($definition['default_callback'])) {
        $function = $definition['default_callback'];
        $module_data[$name] = $function($commands, $module_data);
      }
    }
  }

  // Extra processing for the hooks array (or not).
  if (!is_array($module_data['hooks'])) {
    $module_data['hooks'] = preg_split('/\\s+/', $module_data['hooks']);
  }

  // Convert the array from numeric to keyed by full hook name.
  foreach ($module_data['hooks'] as $hook_name) {
    $hooks["hook_{$hook_name}"] = TRUE;
  }
  $module_data['hooks'] = $hooks;

  //print_r($module_data);
  return $module_data;
}