You are here

function drush_features_export in Features 8.3

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

Provides Drush command callback for features-export.

File

drush/features.drush8.inc, line 295
Features module drush integration.

Code

function drush_features_export($packages = NULL) {
  $packages = func_get_args();
  $assigner = _drush_features_options();

  /** @var \Drupal\features\FeaturesManagerInterface $manager */
  $manager = \Drupal::service('features.manager');

  /** @var \Drupal\features\FeaturesGeneratorInterface $generator */
  $generator = \Drupal::service('features_generator');
  $current_bundle = $assigner
    ->getBundle();
  if (drush_get_option('add-profile')) {
    if ($current_bundle->isDefault) {
      return drush_set_error('', dt("Must specify a profile name with --name"));
    }
    $current_bundle
      ->setIsProfile(TRUE);
  }
  $all_packages = $manager
    ->getPackages();
  foreach ($packages as $name) {
    if (!isset($all_packages[$name])) {
      return drush_set_error('', dt("The package @name does not exist.", [
        '@name' => $name,
      ]));
    }
  }
  if (empty($packages)) {
    $packages = $all_packages;
    $dt_args = [
      '@modules' => implode(', ', array_keys($packages)),
    ];
    drush_print(dt('The following extensions will be exported: @modules', $dt_args));
    if (!drush_confirm(dt('Do you really want to continue?'))) {
      return drush_user_abort('Aborting.');
    }
  }

  // If any packages exist, confirm before overwriting.
  if ($existing_packages = $manager
    ->listPackageDirectories($packages, $current_bundle)) {
    foreach ($existing_packages as $name => $directory) {
      drush_print(dt("The extension @name already exists at @directory.", [
        '@name' => $name,
        '@directory' => $directory,
      ]));
    }

    // Apparently, format_plural is not always available.
    if (count($existing_packages) == 1) {
      $message = dt('Would you like to overwrite it?');
    }
    else {
      $message = dt('Would you like to overwrite them?');
    }
    if (!drush_confirm($message)) {
      return drush_user_abort();
    }
  }

  // Use the write generation method.
  $method_id = FeaturesGenerationWrite::METHOD_ID;
  $result = $generator
    ->generatePackages($method_id, $current_bundle, $packages);
  foreach ($result as $message) {
    $type = $message['success'] ? 'success' : 'error';
    drush_log($message['message'], $message['variables'], $type);
  }
}