Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support the ignore of config collections #17

Open
wants to merge 2 commits into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Its like `drush cex` but with 74% more shiny<sup>[1](#ref1)</sup>.

So the normal `drush cex` command comes with a `--skip-modules` option that prevents configuration from say devel module from being exported. But let's go back to our original use case.

We want to export all configuration, but we want to exclude certain patterns.
We want to export all configuration, but we want to exclude certain patterns or collections.

This is where the `--ignore-list` option of `drush cexy` comes in.

Expand All @@ -33,9 +33,11 @@ ignore:
- core.entity_view_display.contact_form*
- system.site
- workbench_email.workbench_email_template.*
ignore_collection:
- language.sw
```

You'll note there are some wildcards there. We're ignoring all contact message fields and forms as well as any form or view display configuration. Additionally we're ignoring [Workbench Email](https://drupal.org/project/workbench_email) templates and the system site settings.
You'll note there are some wildcards there. We're ignoring all contact message fields and forms as well as any form or view display configuration. Additionally we're ignoring [Workbench Email](https://drupal.org/project/workbench_email) templates and the system site settings in addition to excluding the Swedish language collection.

So now we run `drush cexy` like so

Expand Down
25 changes: 21 additions & 4 deletions drush_cmi_tools.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ function drush_drush_cmi_tools_config_export_plus($destination = NULL) {
return drush_log(dt('You must provide a --destination option'), LogLevel::ERROR);
}
$patterns = [];
$collections = [];
if ($ignore_list = drush_get_option('ignore-list')) {
if (!is_file($ignore_list)) {
return drush_log(dt('The file specified in --ignore-list option does not exist.'), LogLevel::ERROR);
Expand All @@ -87,24 +88,40 @@ function drush_drush_cmi_tools_config_export_plus($destination = NULL) {
catch (InvalidDataTypeException $e) {
$ignore_list_error = TRUE;
}
if (!isset($parsed['ignore']) || !is_array($parsed['ignore'])) {
if (!isset($parsed['ignore']) || !is_array($parsed['ignore']) || !isset($parsed['ignore_collection']) || !is_array($parsed['ignore_collection'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a hard break - can we make existing config-ignore.yml files that don't have the ignore_collection key continue to work?

e.g. we could

$parsed += ['ignore_collection' => []];
``

$ignore_list_error = TRUE;
}
if ($ignore_list_error) {
return drush_log(dt('The file specified in --ignore-list option is in the wrong format. It must be valid YAML with a top-level ignore key.'), LogLevel::ERROR);
return drush_log(dt('The file specified in --ignore-list option is in the wrong format. It must be valid YAML with a top-level ignore and ignore_collection key.'), LogLevel::ERROR);
}
foreach ($parsed['ignore'] as $ignore) {
// Allow for accidental .yml extension.
if (substr($ignore, -4) === '.yml') {
$ignore = substr($ignore, 0, -4);
}
$patterns[] = '/^' . str_replace('\*', '(.*)', preg_quote($ignore)) . '\.yml/';
$patterns[] = '/^' . str_replace('\*', '(.*)', preg_quote($ignore)) . '\.yml/';
}
foreach ($parsed['ignore_collection'] as $ignore) {
$collections[] = str_replace('.', '/', $ignore);
}
}
}

$result = _drush_config_export($destination, $destination_dir, FALSE);
$file_service = \Drupal::service('file_system');
foreach ($collections as $collection_dir) {
$collection_path = $destination_dir . '/' . $collection_dir;
if (is_dir($collection_path)) {
file_unmanaged_delete_recursive($collection_path);
drush_log("Removed $collection_path collection according to ignore list.", LogLevel::OK);
}
$collection_directories = explode('/', $collection_dir);
$collection_root_dir = $destination_dir . '/' . reset($collection_directories);
if (is_dir($collection_root_dir) && (count(scandir($collection_root_dir)) == 2)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a comment here about what this is doing? The 2 seems magical.

Can we use glob here - if all we're doing is removing directories that match the ignore_collection key?

file_unmanaged_delete_recursive($collection_root_dir);
}
}

$file_service = \Drupal::service('file_system');
foreach ($patterns as $pattern) {
foreach (file_scan_directory($destination_dir, $pattern) as $file_url => $file) {
$file_service->unlink($file_url);
Expand Down