summaryrefslogtreecommitdiffstats
path: root/config_update_ui/config_update_ui.drush.inc
blob: fd8cfb220486d831e376fd26a9621d9896da764f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
<?php

/**
 * @file
 * Drush commands for the Configuration Update Reports module.
 */

use Drupal\Component\Diff\DiffFormatter;

/**
 * Implements hook_drush_command().
 */
function config_update_ui_drush_command() {

  $items = [];

  $items['config-list-types'] = [
    'description' => 'List config types',
    'aliases' => ['clt'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-added-report'] = [
    'description' => 'Display a list of config items that did not come from your installed modules, themes, or install profile',
    'arguments' => [
      'name' => 'The type of config to report on. See config-list-types to list them. You can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-added-report action' => 'Displays the added config report for action config.',
    ],
    'aliases' => ['cra'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-missing-report'] = [
    'description' => 'Display a list of config items from your installed modules, themes, or install profile that are not currently in your active config',
    'arguments' => [
      'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
      'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-missing-report type action' => 'Displays the missing config report for action config.',
    ],
    'aliases' => ['crm'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-inactive-report'] = [
    'description' => 'Display a list of optional config items from your installed modules, themes, or install profile that are not currently in your active config',
    'arguments' => [
      'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
      'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-inactive-report type action' => 'Displays the inactive config report for action config.',
    ],
    'aliases' => ['cri'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-different-report'] = [
    'description' => 'Display a list of config items that differ from the versions provided by your installed modules, themes, or install profile. See config-diff to show what the differences are.',
    'arguments' => [
      'type' => 'Run the report for: module, theme, profile, or "type" for config entity type.',
      'name' => 'The machine name of the module, theme, etc. to report on. See config-list-types to list types for config entities; you can also use system.all for all types, or system.simple for simple config.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-different-report type action' => 'Displays the differing config report for action config.',
    ],
    'aliases' => ['crd'],
    'core' => ['8+'],
    'outputformat' => [
      'default' => 'list',
    ],
  ];

  $items['config-diff'] = [
    'description' => 'Display line-by-line differences for one config item between your active config and the version currently being provided by an installed module, theme, or install profile',
    'arguments' => [
      'name' => 'The config item to diff. See config-different-report to list config items that are different.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-diff block.block.bartik_search' => 'Displays the config differences for the search block in the Bartik theme.',
    ],
    'aliases' => ['cfd'],
    'core' => ['8+'],
  ];

  $items['config-revert'] = [
    'description' => 'Revert one config item in active storage to the version provided by an installed module, theme, or install profile.',
    'arguments' => [
      'name' => 'The config item to revert. See config-different-report to list config items that are different.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-revert block.block.bartik_search' => 'Revert the config for the search block in the Bartik theme to the version provided by the install profile.',
    ],
    'aliases' => ['cfr'],
    'core' => ['8+'],
  ];

  $items['config-import-missing'] = [
    'description' => 'Import a missing or inactive config item provided by an installed module, theme, or install profile. Be sure that requirements are met.',
    'arguments' => [
      'name' => 'The name of the config item to import (usually the ID you would see in the user interface). See config-missing-report to list config items that are missing, and config-inactive-report to list config items that are inactive.',
    ],
    'required-arguments' => 1,
    'examples' => [
      'drush config-import-missing block.block.bartik_search' => 'Import the config for the search block in the Bartik theme from the version provided by the install profile.',
    ],
    'aliases' => ['cfi'],
    'core' => ['8+'],
  ];

  $items['config-revert-multiple'] = [
    'description' => 'Revert a set of config items to the versions provided by installed modules, themes, or install profiles. A set is all differing items from one extension, or one type of configuration.',
    'arguments' => [
      'type' => 'Type of set to revert: "module" for all items from a module, "theme" for all items from a theme, "profile" for all items from the install profile, or "type" for all items of one config entity type. See config-different-report to list config items that are different.',
      'name' => 'The machine name of the module, theme, etc. to revert items of. All items in the corresponding config-different-report will be reverted.',
    ],
    'required-arguments' => 2,
    'examples' => [
      'drush config-revert-multiple type action' => 'Revert all differing config items of type action.',
    ],
    'aliases' => ['cfrm'],
    'core' => ['8+'],
  ];

  return $items;
}

/**
 * Lists available config types.
 */
function drush_config_update_ui_config_list_types() {
  $list = [];

  $definitions = \Drupal::service('config_update.config_list')->listTypes();
  return array_keys($definitions);
}

/**
 * Runs the config added report.
 *
 * @param string $name
 *   Type of config to report on.
 */
function drush_config_update_ui_config_added_report($name) {
  list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig('type', $name);

  $added = array_diff($active_list, $install_list, $optional_list);

  if (!count($added)) {
    drush_print(dt('No added config'), 0, STDERR);
  }

  sort($added);
  return $added;
}

/**
 * Runs the config missing report.
 *
 * @param string $type
 *   Type of report to run: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of item to report on.
 */
function drush_config_update_ui_config_missing_report($type, $name) {
  list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);

  $missing = array_diff($install_list, $active_list);
  if (!count($missing)) {
    drush_print(dt('No missing config'), 0, STDERR);
  }

  sort($missing);
  return $missing;
}

/**
 * Runs the config inactive report.
 *
 * @param string $type
 *   Type of report to run: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of item to report on.
 */
function drush_config_update_ui_config_inactive_report($type, $name) {
  list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);

  $missing = array_diff($optional_list, $active_list);
  if (!count($missing)) {
    drush_print(dt('No inactive config'), 0, STDERR);
  }

  sort($missing);
  return $missing;
}

/**
 * Runs the config different report.
 *
 * @param string $type
 *   Type of report to run: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of item to report on.
 */
function drush_config_update_ui_config_different_report($type, $name) {
  list($active_list, $install_list, $optional_list) = \Drupal::service('config_update.config_list')->listConfig($type, $name);

  $reverter = \Drupal::service('config_update.config_update');
  $differ = \Drupal::service('config_update.config_diff');

  $added = array_diff($active_list, $install_list, $optional_list);
  $both = array_diff($active_list, $added);
  $different = [];
  foreach ($both as $name) {
    $active = $reverter->getFromActive('', $name);
    $extension = $reverter->getFromExtension('', $name);
    if (!$differ->same($active, $extension)) {
      $different[] = $name;
    }
  }

  if (!count($different)) {
    drush_print(dt('No different config'), 0, STDERR);
  }

  sort($different);
  return $different;
}

/**
 * Runs the drush config-diff command.
 *
 * @param string $name
 *   Config item to diff.
 */
function drush_config_update_ui_config_diff($name) {
  $reverter = \Drupal::service('config_update.config_update');
  $differ = \Drupal::service('config_update.config_diff');
  $formatter = new DiffFormatter();

  $extension = $reverter->getFromExtension('', $name);
  $active = $reverter->getFromActive('', $name);
  if ($extension && $active) {
    $diff = $differ->diff($extension, $active);
    $output = $formatter->format($diff);
    return $output;
  }
  else {
    drush_print(dt('Config is missing, cannot diff'), 0, STDERR);
    return '';
  }
}

/**
 * Runs the drush config-revert command.
 *
 * @param string $name
 *   Config item to revert.
 */
function drush_config_update_ui_config_revert($name) {
  $lister = \Drupal::service('config_update.config_list');
  $reverter = \Drupal::service('config_update.config_update');
  $manager = \Drupal::service('entity.manager');

  // The revert command needs the type and the unprefixed name.
  $type = $lister->getTypeNameByConfigName($name);
  $shortname = $name;
  if ($type && $type != 'system.simple') {
    $definition = $manager->getDefinition($type);
    $prefix = $definition->getConfigPrefix() . '.';
    if (strpos($name, $prefix) === 0) {
      $shortname = substr($name, strlen($prefix));
    }
  }

  if ($reverter->revert($type, $shortname)) {
    drush_print(dt('The configuration item @name was reverted to its source.', ['@name' => $name]));
  }
  else {
    drush_print(dt('There was an error and the configuration item @name was not reverted.', ['@name' => $name]), 0, STDERR);
  }
}

/**
 * Runs the drush config-import-missing command.
 *
 * @param string $name
 *   Name of config item to import, without prefix.
 */
function drush_config_update_ui_config_import_missing($name) {
  $lister = \Drupal::service('config_update.config_list');
  $reverter = \Drupal::service('config_update.config_update');
  $manager = \Drupal::service('entity.manager');

  // The import command needs the type and the unprefixed name.
  $type = $lister->getTypeNameByConfigName($name);
  $shortname = $name;
  if ($type && $type != 'system.simple') {
    $definition = $manager->getDefinition($type);
    $prefix = $definition->getConfigPrefix() . '.';
    if (strpos($name, $prefix) === 0) {
      $shortname = substr($name, strlen($prefix));
    }
  }

  if ($reverter->import($type, $shortname)) {
    drush_print(dt('The configuration item @name was imported from its source.', ['@name' => $name]));
  }
  else {
    drush_print(dt('There was an error and the configuration item @name was not imported.', ['@name' => $name]), 0, STDERR);
    drush_print(dt('The configuration was imported from its source.'));
  }
}

/**
 * Runs the drush config-revert-multiple command.
 *
 * @param string $type
 *   Type of config to revert: 'type', 'module', 'theme', or 'profile'.
 * @param string $name
 *   Machine name of type to revert.
 */
function drush_config_update_ui_config_revert_multiple($type, $name) {
  // This command will print an error if there are no items to revert.
  $different = drush_config_update_ui_config_different_report($type, $name);
  foreach ($different as $name) {
    // This command will print a message if it succeeds or fails for each item.
    drush_config_update_ui_config_revert($name);
  }
}