Skip to content

Commit d252eca

Browse files
authored
Merge pull request #98 from serundeputy/ongoing-saga-of-en
@opi @jackaponte @jenlampton @wesruv @klonos @quicksketch
2 parents 1e628a0 + 99d7f2c commit d252eca

File tree

4 files changed

+230
-69
lines changed

4 files changed

+230
-69
lines changed

backdrop.drush.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ function backdrop_drush_command_alter(&$command) {
7474
$backdrop_command = 'backdrop-pm-enable';
7575
break;
7676

77+
case 'pm-disable':
78+
$backdrop_command = 'backdrop-pm-disable';
79+
break;
80+
7781
case 'user-password':
7882
$backdrop_command = 'backdrop-user-password';
7983
break;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Drush project management disable command.
6+
*/
7+
8+
use Drush\Log\LogLevel;
9+
10+
/**
11+
* Implements hook_drush_command().
12+
*/
13+
function backdrop_pm_disable_drush_command() {
14+
$items = array();
15+
$items['backdrop-pm-disable'] = array(
16+
'description' => 'Disable backdrop modules.',
17+
'callback' => 'backdrop_command_pm_disable',
18+
'arguments' => array(
19+
'module-name' => array('The name of the module(s) you would like to disable.'),
20+
),
21+
'aliases' => array('dis'),
22+
'required-arguments' => TRUE,
23+
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_FULL,
24+
);
25+
26+
return $items;
27+
}
28+
29+
/**
30+
* Command callback for pm-disable.
31+
*/
32+
function backdrop_command_pm_disable() {
33+
$projects = func_get_args();
34+
35+
// Get modules present in files system that are possible to disable.
36+
$module_list = system_rebuild_module_data();
37+
38+
foreach ($projects as $project) {
39+
// Check if requested module is required by other modules.
40+
$required_bys = $module_list[$project]->required_by;
41+
42+
$kids = [];
43+
if (!empty($required_bys)) {
44+
foreach ($required_bys as $key => $required) {
45+
if (module_exists($key) && !in_array($key, $projects)) {
46+
array_unshift($projects, $key);
47+
$kids = array_merge($projects, $kids);
48+
}
49+
else {
50+
// Kids is already accounted for.
51+
}
52+
}
53+
}
54+
}
55+
56+
$operating_list = ($kids) ? implode(',', $kids) : implode(', ', $projects);
57+
$proceed = drush_confirm(
58+
"The following projects will be disabled: $operating_list.
59+
Do you want to disable the projects?"
60+
);
61+
62+
if (!$proceed) {
63+
drush_print_r(
64+
dt("\n\t\e[033mCancelled\e[0m $operating_list not disabled.\n")
65+
);
66+
}
67+
elseif (!empty($kids)) {
68+
foreach ($kids as $kid) {
69+
module_disable([$kid]);
70+
}
71+
}
72+
else {
73+
// Now disable the projects specified on the command line.
74+
foreach ($projects as $project) {
75+
module_disable([$project]);
76+
}
77+
}
78+
79+
drush_print_r(
80+
dt("\n\t\033[32mSuccess\033[0m: $operating_list are disabled.\n")
81+
);
82+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
/**
3+
* @file
4+
* Drush project management enable command.
5+
*/
6+
7+
use Drupal\Core\Logger\RfcLogLevel;
8+
9+
/**
10+
* Implements hook_drush_command().
11+
*/
12+
function backdrop_pm_enable_drush_command() {
13+
$items = array();
14+
$items['backdrop-pm-enable'] = array(
15+
'description' => 'Enable backdrop modules.',
16+
'callback' => 'backdrop_command_pm_enable',
17+
'hidden' => TRUE,
18+
'arguments' => array(
19+
'module-name' => array('The name of the module(s) you would like to enable.'),
20+
),
21+
'required-arguments' => TRUE,
22+
'aliases' => array('en'),
23+
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_FULL,
24+
);
25+
26+
return $items;
27+
}
28+
29+
/**
30+
* Command callback enable modules.
31+
*
32+
* @see _enable_project()
33+
*/
34+
function backdrop_command_pm_enable() {
35+
$projects = func_get_args();
36+
37+
if (!isset($projects)) {
38+
drush_print_r(dt("\tPlease provide module name(s)\n\n"));
39+
return;
40+
}
41+
42+
// Get modules present in files system that are possible to enable.
43+
$module_list = system_rebuild_module_data();
44+
45+
// Loop through projects to handle dependencies.
46+
foreach ($projects as $project) {
47+
// Check if requested module depends on other modules.
48+
$dependencies = $module_list[$project]->info['dependencies'];
49+
if (!empty($dependencies)) {
50+
foreach ($dependencies as $dependency) {
51+
// Prepend dependency to projects list.
52+
if (!module_exists($dependency) && !in_array($dependency, $projects)) {
53+
array_unshift($projects, $dependency);
54+
}
55+
}
56+
}
57+
}
58+
59+
$projects_list = implode(', ', $projects);
60+
$proceed = drush_confirm(
61+
"The following projects will be enabled: $projects_list.
62+
Do you want to enable the projects?"
63+
);
64+
65+
if (!$proceed) {
66+
drush_print_r(
67+
dt("\n\t\e[033mCancelled\e[0m $projects_list not enabled.\n")
68+
);
69+
}
70+
else {
71+
// Enable the projects specified on the cli with dependencies.
72+
foreach ($projects as $project) {
73+
$status = _enable_project($project, $module_list);
74+
if (!$status) {
75+
// _enable_project() already output an error message.
76+
return FALSE;
77+
}
78+
}
79+
80+
backdrop_flush_all_caches();
81+
}
82+
}
83+
84+
/**
85+
* Internal function to enable module or theme.
86+
*
87+
* @param string $project
88+
* The project machine name to be enabled.
89+
*
90+
* @param array $module_list
91+
* List of modules that exist in the file system.
92+
*
93+
* @return bool
94+
* TRUE if the module is enabled; FALSE otherwise.
95+
*
96+
* @see backdrop_command_pm_enable_validate()
97+
*/
98+
function _enable_project($project, $module_list) {
99+
// Check against the module_list.
100+
$project_exists = backdrop_command_pm_enable_validate($project, $module_list);
101+
// If the $project directory does not exist then gracefully fail.
102+
if (!$project_exists) {
103+
drush_print_r("\n\t\e[031mError\e[0m $project does not exist in your Backdrop installation.");
104+
drush_print_r("\tTry downloading $project first with the command: drush dl $project\n");
105+
return FALSE;
106+
}
107+
$query = db_select('system', 's')
108+
->fields('s');
109+
$query->condition('name', $project);
110+
$query->condition('type', 'module');
111+
$module = $query->execute()->fetchAssoc();
112+
113+
if ($module['status']) {
114+
drush_print_r(
115+
"\n\t\e[31m Failed\e[0m to enable module " . $module['name'] . ": it is already enabled.\n"
116+
);
117+
return FALSE;
118+
}
119+
120+
if (module_enable(array($project), FALSE)) {
121+
drush_print_r("\n\t\e[32mSuccess\e[0m: module $project enabled.\n");
122+
return TRUE;
123+
}
124+
drush_print_r("\n\t\e[31mFailed\e[0m to enable module " . $project);
125+
return FALSE;
126+
}
127+
128+
/**
129+
* Command pm-update validate function.
130+
*
131+
* @param string $project
132+
* The project that the user is attempting to enable.
133+
*/
134+
function backdrop_command_pm_enable_validate($project, $module_list) {
135+
if (array_key_exists($project, $module_list)) {
136+
return TRUE;
137+
}
138+
139+
return FALSE;
140+
}

includes/environment.inc

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22
/**
33
* @file
4-
* Specific functions for a backdrop 7 environment.
5-
* drush_include_engine() magically includes either this file
6-
* or environment_X.inc depending on which version of backdrop drush
7-
* is called from.
4+
* Specific functions for a Backdrop 1 environment.
5+
* drush_include_engine() magically includes either this file
6+
* or environment_X.inc depending on which version of backdrop drush
7+
* is called from.
88
*/
99

1010
/**
@@ -148,51 +148,6 @@ function drush_get_named_extensions_list($extensions) {
148148
return $result;
149149
}
150150

151-
/**
152-
* Enable a list of modules.
153-
*
154-
* It is assumed the list contains all the dependencies not already enabled.
155-
*
156-
* @param array $modules
157-
* Array of module names
158-
*/
159-
function drush_module_enable($modules) {
160-
// The list of modules already have all the dependencies, but they might not
161-
// be in the correct order. Still pass $enable_dependencies = TRUE so that
162-
// Backdrop will enable the modules in the correct order.
163-
module_enable($modules);
164-
// Flush all caches.
165-
backdrop_flush_all_caches();
166-
}
167-
168-
/**
169-
* Disable a list of modules.
170-
*
171-
* It is assumed the list contains all dependents not already disabled.
172-
*
173-
* @param array $modules
174-
* Array of module names
175-
*/
176-
function drush_module_disable($modules) {
177-
// The list of modules already have all the dependencies, but they might not
178-
// be in the correct order. Still pass $enable_dependencies = TRUE so that
179-
// Backdrop will enable the modules in the correct order.
180-
module_disable($modules);
181-
// Flush all caches.
182-
backdrop_flush_all_caches();
183-
}
184-
185-
/**
186-
* Uninstall a list of modules.
187-
*
188-
* @param array $modules
189-
* Array of module names
190-
*/
191-
function drush_module_uninstall($modules) {
192-
require_once DRUSH_BACKDROP_CORE . '/includes/install.inc';
193-
backdrop_uninstall_modules($modules);
194-
}
195-
196151
/**
197152
* Checks that a given module exists and is enabled.
198153
*
@@ -247,26 +202,6 @@ function drush_get_themes($include_hidden = TRUE) {
247202
return $themes;
248203
}
249204

250-
/**
251-
* Enable a list of themes.
252-
*
253-
* @param array $themes
254-
* Array of theme names.
255-
*/
256-
function drush_theme_enable($themes) {
257-
theme_enable($themes);
258-
}
259-
260-
/**
261-
* Disable a list of themes.
262-
*
263-
* @param array $themes
264-
* Array of theme names.
265-
*/
266-
function drush_theme_disable($themes) {
267-
theme_disable($themes);
268-
}
269-
270205
/**
271206
* Helper function to obtain the severity levels based on Backdrop version.
272207
*

0 commit comments

Comments
 (0)