Skip to content

Commit

Permalink
Merge pull request #98 from serundeputy/ongoing-saga-of-en
Browse files Browse the repository at this point in the history
  • Loading branch information
serundeputy authored Jun 13, 2019
2 parents 1e628a0 + 99d7f2c commit d252eca
Show file tree
Hide file tree
Showing 4 changed files with 230 additions and 69 deletions.
4 changes: 4 additions & 0 deletions backdrop.drush.inc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ function backdrop_drush_command_alter(&$command) {
$backdrop_command = 'backdrop-pm-enable';
break;

case 'pm-disable':
$backdrop_command = 'backdrop-pm-disable';
break;

case 'user-password':
$backdrop_command = 'backdrop-user-password';
break;
Expand Down
82 changes: 82 additions & 0 deletions commands/pm/backdrop_pm_disable.drush.inc
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")
);
}
140 changes: 140 additions & 0 deletions commands/pm/backdrop_pm_enable.drush.inc
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;
}
73 changes: 4 additions & 69 deletions includes/environment.inc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* @file
* Specific functions for a backdrop 7 environment.
* drush_include_engine() magically includes either this file
* or environment_X.inc depending on which version of backdrop drush
* is called from.
* Specific functions for a Backdrop 1 environment.
* drush_include_engine() magically includes either this file
* or environment_X.inc depending on which version of backdrop drush
* is called from.
*/

/**
Expand Down Expand Up @@ -148,51 +148,6 @@ function drush_get_named_extensions_list($extensions) {
return $result;
}

/**
* Enable a list of modules.
*
* It is assumed the list contains all the dependencies not already enabled.
*
* @param array $modules
* Array of module names
*/
function drush_module_enable($modules) {
// The list of modules already have all the dependencies, but they might not
// be in the correct order. Still pass $enable_dependencies = TRUE so that
// Backdrop will enable the modules in the correct order.
module_enable($modules);
// Flush all caches.
backdrop_flush_all_caches();
}

/**
* Disable a list of modules.
*
* It is assumed the list contains all dependents not already disabled.
*
* @param array $modules
* Array of module names
*/
function drush_module_disable($modules) {
// The list of modules already have all the dependencies, but they might not
// be in the correct order. Still pass $enable_dependencies = TRUE so that
// Backdrop will enable the modules in the correct order.
module_disable($modules);
// Flush all caches.
backdrop_flush_all_caches();
}

/**
* Uninstall a list of modules.
*
* @param array $modules
* Array of module names
*/
function drush_module_uninstall($modules) {
require_once DRUSH_BACKDROP_CORE . '/includes/install.inc';
backdrop_uninstall_modules($modules);
}

/**
* Checks that a given module exists and is enabled.
*
Expand Down Expand Up @@ -247,26 +202,6 @@ function drush_get_themes($include_hidden = TRUE) {
return $themes;
}

/**
* Enable a list of themes.
*
* @param array $themes
* Array of theme names.
*/
function drush_theme_enable($themes) {
theme_enable($themes);
}

/**
* Disable a list of themes.
*
* @param array $themes
* Array of theme names.
*/
function drush_theme_disable($themes) {
theme_disable($themes);
}

/**
* Helper function to obtain the severity levels based on Backdrop version.
*
Expand Down

0 comments on commit d252eca

Please sign in to comment.