forked from backdrop-contrib/coder_upgrade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coder.drush.inc
71 lines (65 loc) · 2.47 KB
/
coder.drush.inc
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
<?php
/**
* @file
* Drush integration for Coder module.
*/
/**
* Implements hook_drush_command().
*/
function coder_drush_command() {
$items['coder-format'] = array(
'description' => dt('Re-format and rewrite code according Backdrop coding standards.'),
'arguments' => array(
'path' => dt('The path of a file to reformat. Or the name of a directory to (recursively) reformat all contained files within.'),
),
'options' => array(
'undo' => dt('Restores already processed files from backups generated by Coder format. Automatically searches for the latest backup file ([filename].coder.orig) and each file is replaced with its original version.'),
),
'examples' => array(
'drush coder-format sites/all/modules/coder/coder.module' => 'Re-format coder.module according to Backdrop coding standards.',
'drush coder-format --undo sites/all/modules/coder/coder.module' => 'Restore coder.module from coder.module.coder.orig backup file (if existent).',
'drush coder-format sites/all/modules/coder' => 'Recursively re-format Coder module files according to Backdrop coding standards.',
'drush coder-format --undo sites/all/modules/coder' => 'Recursively restore Coder module files from *.coder.orig backup files (if existent).',
),
);
return $items;
}
/**
* Implements hook_drush_help().
*/
function coder_drush_help($section) {
switch ($section) {
case 'drush:coder-format':
return dt('Re-format and rewrite code according Backdrop coding standards.');
}
}
/**
* Performs the desired coder-format command for drush.
*
* Drush command callback for coder-format.
*/
function drush_coder_format() {
$path = drush_get_context('DRUSH_BACKDROP_ROOT');
$path .= '/' . backdrop_get_path('module', 'coder') . '/scripts/coder_format/coder_format.inc';
require_once $path;
$undo = drush_get_option('undo', FALSE);
$files = func_get_args();
$cwd = drush_get_context('DRUSH_OLDCWD');
foreach ($files as $file) {
// Additionally support relative paths.
// Do this first to prevent typo disasters (e.g., $file == '/').
if (is_dir($cwd . '/' . $file)) {
coder_format_recursive($cwd . '/' . $file, $undo);
}
elseif (file_exists($cwd . '/' . $file)) {
coder_format_file($cwd . '/' . $file, $undo);
}
// If argument is a directory, process it recursively.
elseif (is_dir($file)) {
coder_format_recursive($file, $undo);
}
else {
coder_format_file($file, $undo);
}
}
}