You are here

function drush_yamlform_tidy in YAML Form 8

Implements drush_hook_COMMAND().

File

drush/yamlform.drush.inc, line 364
YAML Form module drush commands.

Code

function drush_yamlform_tidy($module = 'yamlform') {
  $dependencies = drush_get_option('dependencies');
  $file_directory_path = drupal_get_path('module', $module) . '/config';
  $files = file_scan_directory($file_directory_path, '/.*\\.yml$/');
  drush_print(dt('Reviewing @count YAML configuration files for the @module.module.', [
    '@count' => count($files),
    '@module' => $module,
  ]));
  $total = 0;
  foreach ($files as $file) {
    $original_yaml = file_get_contents($file->uri);
    $tidied_yaml = $original_yaml;

    // Add module dependency to exporter form and form options config entities.
    if ($dependencies && preg_match('/^(yamlform\\.yamlform\\.|yamlform\\.yamlform_options\\.)/', $file->filename)) {
      $data = Yaml::decode($tidied_yaml);
      if (empty($data['dependencies']['enforced']['module']) || !in_array($module, $data['dependencies']['enforced']['module'])) {
        drush_print(dt('Adding module dependency to @file...', [
          '@file' => $file->filename,
        ]));
        $data['dependencies']['enforced']['module'][] = $module;
        $tidied_yaml = Yaml::encode($data);
      }
    }

    // Tidy and add new line to the end of the tidied file.
    $tidied_yaml = YamlFormTidy::tidy($tidied_yaml) . "\n";
    if ($tidied_yaml != $original_yaml) {
      drush_print(dt('Tidying @file...', [
        '@file' => $file->filename,
      ]));
      file_put_contents($file->uri, $tidied_yaml);
      $total++;
    }
  }
  if ($total) {
    drush_print(dt('@total YAML file(s) tidied.', [
      '@total' => $total,
    ]));
  }
  else {
    drush_print(dt('No YAML files needed to be tidied.'));
  }
}