drupalmoduleupgrader.drush.inc
9.06 KB
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
<?php
use Drupal\drupalmoduleupgrader\Report;
use Drupal\drupalmoduleupgrader\Target;
use Symfony\Component\Filesystem\Filesystem;
/**
* Implements hook_drush_command().
*/
function drupalmoduleupgrader_drush_command() {
$items = [];
$items['dmu-list'] = [
'description' => 'Lists available plugins.',
'arguments' => [
'plugin_type' => 'The plugin type to query. Can be one of: indexer, analyzer, converter, cleaner.',
],
'required-arguments' => TRUE,
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
];
$items['dmu-index'] = [
'description' => 'Indexes a target module.',
'arguments' => [
'module' => 'The name of a Drupal 7 module.',
],
'required-arguments' => TRUE,
'examples' => [
'drush dmu-index pants' => 'Indexes the pants module.',
],
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
];
$items['dmu-analyze'] = [
'description' => "Analyzes a Drupal 7 module and reports the changes needed to port it to Drupal 8.",
'arguments' => [
'module' => 'The machine name of a Drupal 7 module.',
],
'required-arguments' => TRUE,
'options' => [
'only' => [
'description' => 'A comma-separated list of analyzers to run, excluding all others.',
'example-value' => 'HookMenu,VariableAPI,BlockInfo',
],
'skip' => [
'description' => 'A comma-separated list of analyzers to skip.',
'example-value' => 'HookInit,HookExit',
],
'path' => [
'description' => 'Optional path to the target module.',
'example-value' => 'drupal/modules/foobaz',
],
'output' => [
'description' => 'Optional path to output the report.',
'example-value' => 'path/to/module/analyze.html',
],
],
'examples' => [
'drush dmu-analyze pants' => 'Analyze what needs to be changed in order to port the pants module.',
],
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
];
$items['dmu-upgrade'] = [
'description' => "Upgrades a Drupal 7 module to Drupal 8.",
'arguments' => [
'module' => 'The machine name of a Drupal 7 module.',
],
'required-arguments' => TRUE,
'options' => [
'backup' => [
'description' => 'If set, creates a backup copy of the module before conversion.',
],
'only' => [
'description' => 'A comma-separated list of converters to run, excluding all others.',
'example-value' => 'HookMenu,VariableAPI,BlockInfo',
],
'skip' => [
'description' => 'A comma-separated list of converters to skip.',
'example-value' => 'HookInit,HookExit',
],
'path' => [
'description' => 'Optional path to the target module. Will be determined automatically if omitted.',
'example-value' => 'drupal/modules/foobaz',
],
],
'examples' => [
'drush dmu-upgrade pants' => 'Upgrade whatever can be automatically upgraded in the pants module.',
],
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_ROOT,
];
return $items;
}
/**
* Returns a list of plugin IDs of a given type, filtered by the --only
* and --skip options.
*
* @param string $plugin_type
* The plugin type. Can be one of indexer, analyzer, converter, cleaner.
*
* @return string[]
*/
function _dmu_plugin_list($plugin_type) {
// Instantiate the plugin manager and get all available plugin IDs.
$manager = \Drupal::service('plugin.manager.drupalmoduleupgrader.' . $plugin_type);
$plugin_IDs = array_keys($manager->getDefinitions());
// Filter by the --only and --skip options, if set.
if ($only = drush_get_option('only', FALSE)) {
$plugin_IDs = array_intersect($plugin_IDs, explode(',', $only));
}
elseif ($skip = drush_get_option('skip', FALSE)) {
$plugin_IDs = array_diff($plugin_IDs, explode(',', $skip));
}
return $plugin_IDs;
}
/**
* Checks for autoload.php, and includes it if it exists or sets an error
* if it doesn't.
*/
function _dmu_ensure_autoload() {
$autoload = __DIR__ . '/vendor/autoload.php';
if (file_exists($autoload)) {
require_once $autoload;
}
else {
drush_set_error('no_autoload', 'autoload.php not found! Did you remember to run composer install from the drupalmoduleupgrader directory?');
}
}
/**
* Determines the path to a module.
*
* @param string $module
* The module's machine name.
*
* @return string|NULL
*/
function _dmu_get_directory($module) {
if ($path = drush_get_option('path', NULL)) {
return $path;
}
else {
$search_directories = [
DRUPAL_ROOT . '/modules/' . $module,
__DIR__ . '/'. $module,
];
$directories = array_filter($search_directories, 'is_dir');
if ($directories) {
return reset($directories);
}
}
}
/**
* Checks possible locations of a target module, and ensures that at least
* one exists. If none do, sets an error.
*
* @param string $module
* The target module's machine name.
*/
function _dmu_ensure_directory($module) {
$directory = _dmu_get_directory($module);
if (empty($directory)) {
if ($path = drush_get_option('path', NULL)) {
drush_set_error('invalid_dir', 'Invalid path: ' . $path);
}
else {
drush_set_error('no_directory', "Cannot determine base directory of module $module. Try passing --path=modules/foobar");
}
}
}
/**
* Validates any of the DMU commands.
*/
function _dmu_validate_command($module) {
_dmu_ensure_autoload();
_dmu_ensure_directory($module);
}
function _dmu_build_target($module) {
$target = new Target(_dmu_get_directory($module), \Drupal::getContainer());
drush_print(\Drupal::translation()->translate('Indexing...'), 0, NULL, FALSE);
$target->buildIndex();
drush_print(\Drupal::translation()->translate('done.'));
return $target;
}
/**
* ----- dmu-list -----
*/
/**
* Lists all the available module-wide plugins.
*/
function drush_drupalmoduleupgrader_dmu_list($plugin_type) {
$manager = \Drupal::service('plugin.manager.drupalmoduleupgrader.' . $plugin_type);
$list = [];
foreach ($manager->getDefinitions() as $id => $definition) {
$list[$id] = $definition['description'];
}
drush_print_table(drush_key_value_to_array_table($list));
}
/**
* ----- dmu-index -----
*/
function drush_drupalmoduleupgrader_dmu_index_validate($module) {
_dmu_validate_command($module);
}
/**
* ----- dmu-analyze -----
*/
function drush_drupalmoduleupgrader_dmu_analyze_validate($module) {
_dmu_validate_command($module);
}
/**
* Analyzes what needs changing in a module to port it to Drupal 8.
*
* @param string $module
* The machine name of the module to analyze.
*/
function drush_drupalmoduleupgrader_dmu_analyze($module) {
$target = _dmu_build_target($module);
$total_issues = 0;
$report = new Report();
$analyzers = \Drupal::service('plugin.manager.drupalmoduleupgrader.analyzer');
foreach (_dmu_plugin_list('analyzer') as $id) {
drush_log(\Drupal::translation()->translate('Executing plugin: @plugin_id', ['@plugin_id' => $id]), 'notice');
$issues = $analyzers->createInstance($id)->analyze($target);
if ($issues) {
if (! is_array($issues)) {
$issues = array($issues);
}
foreach ($issues as $issue) {
$report->addIssue($issue);
}
$total_issues += sizeof($issues);
}
}
if ($total_issues) {
$render = [
'#theme' => 'dmu_report',
'#report' => $report,
'#group_by' => 'category',
];
$destination = drush_get_option('output', $target->getPath('upgrade-info.html'));
$output = \Drupal::service('renderer')->renderRoot($render);
file_put_contents($destination, $output);
drush_log(\Drupal::translation()->translate('Generated a report at @path', ['@path' => $destination]), 'success');
}
else {
drush_log(\Drupal::translation()->translate('Wow...no issues found! You get a cookie :)', 'success'));
}
}
/**
* ----- dmu-upgrade -----
*/
function drush_drupalmoduleupgrader_dmu_upgrade_validate($module) {
_dmu_validate_command($module);
}
/**
* Tries to automatically convert a Drupal 7 module to Drupal 8.
*
* @param string $module
* The module to upgrade.
*/
function drush_drupalmoduleupgrader_dmu_upgrade($module) {
$target = _dmu_build_target($module);
if (drush_get_option('backup', FALSE)) {
$fs = new Filesystem();
$backup_at = $target->getBasePath() . '.bak';
$fs->mirror($target->getBasePath(), $backup_at);
drush_log(\Drupal::translation()->translate('Created backup at @path', [ '@path' => $backup_at ]), 'success');
}
$converters = \Drupal::service('plugin.manager.drupalmoduleupgrader.converter');
foreach (_dmu_plugin_list('converter') as $id) {
/** @var \Drupal\drupalmoduleupgrader\ConverterInterface $converter */
$converter = $converters->createInstance($id);
if ($converter->isExecutable($target)) {
drush_log(\Drupal::translation()->translate('Executing plugin: @plugin_id', ['@plugin_id' => $id]), 'notice');
try {
$converter->convert($target);
}
catch (Exception $e) {
drush_log($e->getMessage(), 'error');
// Being a notice, the stack trace will only appear in verbose mode.
drush_log($e->getTraceAsString(), 'notice');
}
}
}
}