Created
April 15, 2015 08:49
-
-
Save pfiaux/74cfd33c5b2b51cd6eda to your computer and use it in GitHub Desktop.
Nothing Module Sync Drush Script
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
<?php | |
/** | |
* Drush commands for quickly enabling & disabling modules based on | |
* nothing environment. This only includes modules and features but not themes. | |
* | |
* About the environment variable: | |
* When multiple versions of the site are used each defines its own environment to allow enabling or disabling specific | |
* modules. We define the environments variable in the site's configuration file settings.php (Drupal settings). | |
* It is also useful to have conditionals in your settings based on environment. | |
* Here's an example with development, staging and production environments: | |
* | |
* <code> | |
* // Possible environments | |
* define('ENVIRONMENT_DEV', 'ENVIRONMENT_DEV'); | |
* define('ENVIRONMENT_STAG', 'ENVIRONMENT_STAG'); | |
* define('ENVIRONMENT_PROD', 'ENVIRONMENT_PROD'); | |
* // Set environment automatically based on domains | |
* if (stripos($_SERVER['HTTP_HOST'], "dev.example.com") !== FALSE) { | |
* define('ENVIRONMENT', ENVIRONMENT_DEV); | |
* } | |
* else if (stripos($_SERVER['HTTP_HOST'], "staging.example.com") !== FALSE) { | |
* define('ENVIRONMENT', ENVIRONMENT_STAG); | |
* } | |
* // If we're not in development or staging env assume production (strictest) | |
* else { | |
* define('ENVIRONMENT', ENVIRONMENT_PROD); | |
* } | |
* </code> | |
* | |
* Or if we don't need that much flexibility: | |
* <code> | |
* // A simple example with a single environment | |
* define('ENVIRONMENT', 'ENVIRONMENT_PROD'); | |
* </code> | |
* | |
* The ENVIRONMENT variable is used to determine the name of the save files. So an ENVIRONMENT of 'ENVIRONMENT_DEV' | |
* will look for the following files: | |
* - ENVIRONMENT_DEV_disabled_modules.txt | |
* - ENVIRONMENT_DEV_enabled_modules.txt | |
* | |
* Note this also means we need to run drush with a high level of bootstrap usually within the sites/{folder} where the | |
* settings.php is. | |
* | |
* TODO & Limitations: | |
* - the modules save doesn't perform a diff, so if the files were updated and modules sync was not run it can | |
* overwrite some changes. | |
* | |
* sync won't be very smart and it won't diff, so if there are in-coming changes and one runs modsave before modsync | |
* | |
* @copyright Nothing Interactive 2014 | |
* @author Patrick Fiaux <nodz@nothing.ch> | |
* @author Fabian Gander <cyclodex@nothing.ch> | |
* @version 1.0.0 | |
* @license http://opensource.org/licenses/MIT MIT | |
*/ | |
/** | |
* @const | |
* This configures where the files will be saved. | |
* it is prepended with the drupal private path. | |
* For example 'sync_files' will write and read from {private}/sync_files | |
*/ | |
define('SAVE_FOLDER', '/../nothing/'); // | |
/** | |
* Implements hook_drush_command(). | |
* See http://drush.ws/docs/commands.html | |
* | |
* @return array | |
*/ | |
function nothing_drush_command() { | |
$items = array(); | |
$items['nothing-modules-sync'] = array( | |
'description' => "Enables and disables modules based on the current environment.", | |
'examples' => array( | |
'drush nimodsync', | |
), | |
'aliases' => array('nimodsync'), | |
); | |
$items['nothing-modules-save'] = array( | |
'description' => "Save the current environment enabled and disabled modules to files" . | |
", don't forget to upload changes to svn!", | |
'examples' => array( | |
'drush nimodsave', | |
), | |
'aliases' => array('nimodsave'), | |
); | |
return $items; | |
} | |
/** | |
* Helper fail we don't have the even, this is just a safety, if we're not bootstrapped | |
* the commands won't work at all but in the event we're bootstrapped without the ENV | |
* this will kick in. | |
* | |
* We also create any folders we need here | |
*/ | |
function _drush_nothing_environment_check() { | |
if (!defined(ENVIRONMENT)) { | |
drush_die('ERROR: The ENVIORNMENT constant is not set! Unable to proceed.'); | |
} | |
} | |
/** | |
* Returns the path to read & write to for save files. | |
* @return string | |
*/ | |
function _drush_nothing_get_save_path() { | |
$private_path = variable_get('file_private_path', FALSE); | |
return $private_path . SAVE_FOLDER; | |
} | |
/** | |
* Takes a newline separated list of modules and reorders them alphabetically. | |
* @param $list | |
* | |
* @return mixed | |
*/ | |
function _drush_nothing_sortModulesList($list) { | |
$modules = explode(PHP_EOL,$list); | |
sort($modules); | |
// Turn back into a string and ensure end of file new line | |
$list = implode(PHP_EOL,$modules) . PHP_EOL; | |
return $list; | |
} | |
/** | |
* Implements drush_COMMANDNAME | |
* (since we're not a module no need for COMMANDFILE) | |
* Command save modules | |
*/ | |
function drush_nothing_modules_save() { | |
$settings_path = _drush_nothing_get_save_path(); | |
// Ensure that the directory exists: | |
drush_mkdir($settings_path); | |
$enabledFilePath = $settings_path . ENVIRONMENT . '_enabled_modules.txt'; | |
$disabledFilePath = $settings_path . ENVIRONMENT . '_disabled_modules.txt'; | |
// Get the enabled modules | |
$enabledModules = drush_invoke_process( | |
'@self', | |
'pml', | |
array(), // No arguments | |
array('--status=enabled', '--pipe', '--type=module') | |
); | |
file_put_contents($enabledFilePath, _drush_nothing_sortModulesList($enabledModules['output'])); | |
drush_print(PHP_EOL . 'Enabled Modules saved to: ' . $enabledFilePath); | |
// Same for the disabled modules | |
$disabledModules = drush_invoke_process( | |
'@self', | |
'pml', | |
array(), // No arguments | |
array('--status=disabled', '--pipe', '--type=module') | |
); | |
file_put_contents($disabledFilePath, _drush_nothing_sortModulesList($disabledModules['output'])); | |
drush_print(PHP_EOL . 'Disabled Modules saved to: ' . $disabledFilePath); | |
} | |
/** | |
* Implements drush_COMMANDNAME | |
* (since we're not a module no need for COMMANDFILE) | |
* Command sync modules | |
*/ | |
function drush_nothing_modules_sync() { | |
_drush_nothing_environment_check(); | |
drush_print('Starting modules sync...' . PHP_EOL); | |
drush_print('Loading module settings for ' . ENVIRONMENT . PHP_EOL); | |
$settings_path = _drush_nothing_get_save_path(); | |
$enabledFilePath = $settings_path . ENVIRONMENT . '_enabled_modules.txt'; | |
$disabledFilePath = $settings_path . ENVIRONMENT . '_disabled_modules.txt'; | |
// Make sure we have the files we need. | |
if (!is_dir($settings_path) || !file_exists($enabledFilePath) || !file_exists($disabledFilePath)) { | |
drush_die('ERROR: Settings file not found! Did you ever run drush nomodsave?'); | |
} | |
// Enable modules | |
$modulesEnable = preg_split('/\s+/', file_get_contents($enabledFilePath)); | |
drush_print('Will try to enable (if not already) ' . count($modulesEnable) . ' modules'); | |
drush_invoke('en', $modulesEnable); | |
// Disable modules if any | |
$modulesDisable = preg_split('/\s+/', file_get_contents($disabledFilePath)); | |
if (count($modulesDisable) > 0) { | |
drush_print('Will try to disable (if not already) ' . count($modulesDisable) . ' modules'); | |
drush_invoke('dis', $modulesDisable); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment