-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from serundeputy/ongoing-saga-of-en
- Loading branch information
Showing
4 changed files
with
230 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Drush project management disable command. | ||
*/ | ||
|
||
use Drush\Log\LogLevel; | ||
|
||
/** | ||
* Implements hook_drush_command(). | ||
*/ | ||
function backdrop_pm_disable_drush_command() { | ||
$items = array(); | ||
$items['backdrop-pm-disable'] = array( | ||
'description' => 'Disable backdrop modules.', | ||
'callback' => 'backdrop_command_pm_disable', | ||
'arguments' => array( | ||
'module-name' => array('The name of the module(s) you would like to disable.'), | ||
), | ||
'aliases' => array('dis'), | ||
'required-arguments' => TRUE, | ||
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_FULL, | ||
); | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Command callback for pm-disable. | ||
*/ | ||
function backdrop_command_pm_disable() { | ||
$projects = func_get_args(); | ||
|
||
// Get modules present in files system that are possible to disable. | ||
$module_list = system_rebuild_module_data(); | ||
|
||
foreach ($projects as $project) { | ||
// Check if requested module is required by other modules. | ||
$required_bys = $module_list[$project]->required_by; | ||
|
||
$kids = []; | ||
if (!empty($required_bys)) { | ||
foreach ($required_bys as $key => $required) { | ||
if (module_exists($key) && !in_array($key, $projects)) { | ||
array_unshift($projects, $key); | ||
$kids = array_merge($projects, $kids); | ||
} | ||
else { | ||
// Kids is already accounted for. | ||
} | ||
} | ||
} | ||
} | ||
|
||
$operating_list = ($kids) ? implode(',', $kids) : implode(', ', $projects); | ||
$proceed = drush_confirm( | ||
"The following projects will be disabled: $operating_list. | ||
Do you want to disable the projects?" | ||
); | ||
|
||
if (!$proceed) { | ||
drush_print_r( | ||
dt("\n\t\e[033mCancelled\e[0m $operating_list not disabled.\n") | ||
); | ||
} | ||
elseif (!empty($kids)) { | ||
foreach ($kids as $kid) { | ||
module_disable([$kid]); | ||
} | ||
} | ||
else { | ||
// Now disable the projects specified on the command line. | ||
foreach ($projects as $project) { | ||
module_disable([$project]); | ||
} | ||
} | ||
|
||
drush_print_r( | ||
dt("\n\t\033[32mSuccess\033[0m: $operating_list are disabled.\n") | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Drush project management enable command. | ||
*/ | ||
|
||
use Drupal\Core\Logger\RfcLogLevel; | ||
|
||
/** | ||
* Implements hook_drush_command(). | ||
*/ | ||
function backdrop_pm_enable_drush_command() { | ||
$items = array(); | ||
$items['backdrop-pm-enable'] = array( | ||
'description' => 'Enable backdrop modules.', | ||
'callback' => 'backdrop_command_pm_enable', | ||
'hidden' => TRUE, | ||
'arguments' => array( | ||
'module-name' => array('The name of the module(s) you would like to enable.'), | ||
), | ||
'required-arguments' => TRUE, | ||
'aliases' => array('en'), | ||
'bootstrap' => \Drush\Boot\BackdropBoot::BOOTSTRAP_FULL, | ||
); | ||
|
||
return $items; | ||
} | ||
|
||
/** | ||
* Command callback enable modules. | ||
* | ||
* @see _enable_project() | ||
*/ | ||
function backdrop_command_pm_enable() { | ||
$projects = func_get_args(); | ||
|
||
if (!isset($projects)) { | ||
drush_print_r(dt("\tPlease provide module name(s)\n\n")); | ||
return; | ||
} | ||
|
||
// Get modules present in files system that are possible to enable. | ||
$module_list = system_rebuild_module_data(); | ||
|
||
// Loop through projects to handle dependencies. | ||
foreach ($projects as $project) { | ||
// Check if requested module depends on other modules. | ||
$dependencies = $module_list[$project]->info['dependencies']; | ||
if (!empty($dependencies)) { | ||
foreach ($dependencies as $dependency) { | ||
// Prepend dependency to projects list. | ||
if (!module_exists($dependency) && !in_array($dependency, $projects)) { | ||
array_unshift($projects, $dependency); | ||
} | ||
} | ||
} | ||
} | ||
|
||
$projects_list = implode(', ', $projects); | ||
$proceed = drush_confirm( | ||
"The following projects will be enabled: $projects_list. | ||
Do you want to enable the projects?" | ||
); | ||
|
||
if (!$proceed) { | ||
drush_print_r( | ||
dt("\n\t\e[033mCancelled\e[0m $projects_list not enabled.\n") | ||
); | ||
} | ||
else { | ||
// Enable the projects specified on the cli with dependencies. | ||
foreach ($projects as $project) { | ||
$status = _enable_project($project, $module_list); | ||
if (!$status) { | ||
// _enable_project() already output an error message. | ||
return FALSE; | ||
} | ||
} | ||
|
||
backdrop_flush_all_caches(); | ||
} | ||
} | ||
|
||
/** | ||
* Internal function to enable module or theme. | ||
* | ||
* @param string $project | ||
* The project machine name to be enabled. | ||
* | ||
* @param array $module_list | ||
* List of modules that exist in the file system. | ||
* | ||
* @return bool | ||
* TRUE if the module is enabled; FALSE otherwise. | ||
* | ||
* @see backdrop_command_pm_enable_validate() | ||
*/ | ||
function _enable_project($project, $module_list) { | ||
// Check against the module_list. | ||
$project_exists = backdrop_command_pm_enable_validate($project, $module_list); | ||
// If the $project directory does not exist then gracefully fail. | ||
if (!$project_exists) { | ||
drush_print_r("\n\t\e[031mError\e[0m $project does not exist in your Backdrop installation."); | ||
drush_print_r("\tTry downloading $project first with the command: drush dl $project\n"); | ||
return FALSE; | ||
} | ||
$query = db_select('system', 's') | ||
->fields('s'); | ||
$query->condition('name', $project); | ||
$query->condition('type', 'module'); | ||
$module = $query->execute()->fetchAssoc(); | ||
|
||
if ($module['status']) { | ||
drush_print_r( | ||
"\n\t\e[31m Failed\e[0m to enable module " . $module['name'] . ": it is already enabled.\n" | ||
); | ||
return FALSE; | ||
} | ||
|
||
if (module_enable(array($project), FALSE)) { | ||
drush_print_r("\n\t\e[32mSuccess\e[0m: module $project enabled.\n"); | ||
return TRUE; | ||
} | ||
drush_print_r("\n\t\e[31mFailed\e[0m to enable module " . $project); | ||
return FALSE; | ||
} | ||
|
||
/** | ||
* Command pm-update validate function. | ||
* | ||
* @param string $project | ||
* The project that the user is attempting to enable. | ||
*/ | ||
function backdrop_command_pm_enable_validate($project, $module_list) { | ||
if (array_key_exists($project, $module_list)) { | ||
return TRUE; | ||
} | ||
|
||
return FALSE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters