You are here

function drush_features_diff in Features 7

Same name and namespace in other branches
  1. 8.4 drush/features.drush8.inc \drush_features_diff()
  2. 8.3 drush/features.drush8.inc \drush_features_diff()
  3. 6 features.drush.inc \drush_features_diff()
  4. 7.2 features.drush.inc \drush_features_diff()

Show the diff of a feature module.

File

./features.drush.inc, line 716
Features module drush integration.

Code

function drush_features_diff() {
  if (!($args = func_get_args())) {
    drush_features_list();
    return;
  }
  $module = $args[0];
  $feature = features_load_feature($module);
  if (!module_exists($module)) {
    drush_log(dt('No such feature is enabled: ' . $module), 'error');
    return;
  }
  module_load_include('inc', 'features', 'features.export');
  $overrides = features_detect_overrides($feature);
  if (empty($overrides)) {
    drush_log(dt('Feature is in its default state. No diff needed.'), 'ok');
    return;
  }
  module_load_include('inc', 'diff', 'diff.engine');
  if (!class_exists('DiffFormatter')) {
    if (drush_confirm(dt('It seems that the Diff module is not available. Would you like to download and enable it?'))) {

      // Download it if it's not already here.
      $project_info = drush_get_projects();
      if (empty($project_info['diff']) && !drush_invoke_process(drush_sitealias_get_record('@self'), 'dl', array(
        'diff',
      ), array(
        '#integrate' => TRUE,
      ))) {
        return drush_set_error(dt('Diff module could not be downloaded.'));
      }
      if (!drush_invoke_process(drush_sitealias_get_record('@self'), 'en', array(
        'diff',
      ), array(
        '#integrate' => TRUE,
      ))) {
        return drush_set_error(dt('Diff module could not be enabled.'));
      }
    }
    else {
      return drush_set_error(dt('Diff module is not enabled.'));
    }

    // If we're still here, now we can include the diff.engine again.
    module_load_include('inc', 'diff', 'diff.engine');
  }
  $formatter = new DiffFormatter();
  $formatter->leading_context_lines = 2;
  $formatter->trailing_context_lines = 2;
  $formatter->show_header = FALSE;
  if (drush_get_context('DRUSH_NOCOLOR')) {
    $red = $green = "%s";
  }
  else {
    $red = "\33[31;40m\33[1m%s\33[0m";
    $green = "\33[0;32;40m\33[1m%s\33[0m";
  }

  // Print key for colors
  drush_print(dt('Legend: '));
  drush_print(sprintf($red, dt('Code:       drush features-revert will remove the overrides.')));
  drush_print(sprintf($green, dt('Overrides:  drush features-update will update the exported feature with the displayed overrides')));
  drush_print();
  foreach ($overrides as $component => $items) {
    $diff = new Diff(explode("\n", $items['default']), explode("\n", $items['normal']));
    drush_print();
    drush_print(dt("Component: !component", array(
      '!component' => $component,
    )));
    $rows = explode("\n", $formatter
      ->format($diff));
    foreach ($rows as $row) {
      if (strpos($row, '>') === 0) {
        drush_print(sprintf($green, $row));
      }
      elseif (strpos($row, '<') === 0) {
        drush_print(sprintf($red, $row));
      }
      else {
        drush_print($row);
      }
    }
  }
}