Skip to content

Instantly share code, notes, and snippets.

@davereid
Created July 24, 2012 16:40
Show Gist options
  • Save davereid/3171094 to your computer and use it in GitHub Desktop.
Save davereid/3171094 to your computer and use it in GitHub Desktop.
Helpful local Drush commands
<?php
/**
* Implements hook_drush_command().
*/
function custom_local_drush_command() {
$items['files-fix-permissions'] = array(
'description' => 'Fix file permissions',
'options' => array(
'owner' => "The name of the user to assign ownership of all files with chown(). Defaults to \$USER.",
'group' => "The name of the group to assgin ownership of all files with chgrp(). Defaults to www-data.",
'chmod' => 'The chmod() to apply to all files. Defaults to 775.',
),
'aliases' => array('ffp'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION,
);
$items['schema-version-get'] = array(
'description' => 'Get schema version',
'arguments' => array(
'module' => 'The module name.',
),
'required-arguments' => TRUE,
'aliases' => array('svg'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
);
$items['schema-version-set'] = array(
'description' => 'Set schema version',
'arguments' => array(
'module' => 'The module name.',
'schema-version' => 'A schema version to set the module to.',
),
'required-arguments' => TRUE,
'aliases' => array('svs'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
);
$items['whereis'] = array(
'description' => 'Find module or theme location',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
);
return $items;
}
function _drush_custom_local_get_files_directory() {
$paths = _core_path_aliases();
return rtrim(realpath($paths['%files']), '/');
}
function drush_custom_local_files_fix_permissions() {
$user = drush_get_option('owner', '$USER');
$group = drush_get_option('group', 'www-data');
$chmod = drush_get_option('chmod', '775');
$files_dir = _drush_custom_local_get_files_directory();
drush_op_system("sudo chown -R {$user} \"{$files_dir}\"");
drush_op_system("sudo chgrp -R {$group} \"{$files_dir}\"");
drush_op_system("sudo chmod -R {$chmod} \"{$files_dir}\"");
// For any hidden files sitting inside the files directory, apply the same.
$iterator = new DirectoryIterator($files_dir);
foreach ($iterator as $file) {
if ($file->isFile() && substr($file->getFilename(), 0, 1) == '.') {
drush_op_system("sudo chown -R {$user} \"{$file->getPathname()}\"");
drush_op_system("sudo chgrp -R {$group} \"{$file->getPathname()}\"");
drush_op_system("sudo chmod -R {$chmod} \"{$file->getPathname()}\"");
}
}
}
function drush_custom_local_schema_version_get($module) {
// Ensure that install.inc will be included.
include_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
$version = drupal_get_installed_schema_version($module);
if ($version > SCHEMA_UNINSTALLED) {
drush_print("Module $module is currently at schema version $version.");
}
else {
drush_print("Module $module is not installed.");
}
}
function drush_custom_local_schema_version_set($module, $version) {
// Ensure that install.inc will be included.
include_once DRUSH_DRUPAL_CORE . '/includes/install.inc';
if (preg_match('/current([\+\-])(\d+)/', $version, $matches)) {
$current_version = drupal_get_installed_schema_version($module);
switch ($matches[1]) {
case '+':
$version = $current_version + (int) $matches[2];
break;
case '-':
$version = $current_version - (int) $matches[2];
break;
}
}
$version = (int) $version;
drupal_set_installed_schema_version($module, $version);
$new_version = drupal_get_installed_schema_version($module);
if ($new_version == $version) {
drush_print("Updated $module to schema version $version.");
}
else {
drush_print("Unable to set schema to $version, or module $module not installed or available.");
}
}
function drush_custom_local_whereis($target) {
drush_include_engine('drupal', 'environment');
$targets = array_merge(drush_get_modules(), drush_get_themes());
if (isset($targets[$target])) {
drush_print($target. ': ' . $targets[$target]->filename);
}
}
/**
* Implements hook_drush_cache_clear().
*/
function custom_local_drush_cache_clear(&$types) {
if (drush_drupal_major_version() >= 7) {
$types['bootstrap'] = 'custom_local_drush_cache_clear_bootstrap';
if (drush_has_boostrapped(DRUSH_BOOTSTRAP_DRUPAL_FULL)) {
$types['entity'] = 'entity_info_cache_clear';
$types['field'] = 'field_cache_clear';
}
if (drush_drupal_major_version() >= 8) {
$types['php'] = 'custom_local_drush_cache_clear_compiled_php';
}
}
if (extension_loaded('apc')) {
$types['apc'] = 'custom_local_drush_cache_clear_apc';
}
}
/**
* Clears the bootstrap cache.
*/
function custom_local_drush_cache_clear_bootstrap() {
switch (drush_drupal_major_version()) {
case 7:
module_implements('', FALSE, TRUE);
cache_clear_all('*', 'cache_bootstrap', TRUE);
break;
case 8:
drupal_container()->get('module_handler')->resetImplementations();
cache('bootstrap')->deleteAll();
break;
}
}
/**
* Clears the APC cache.
*/
function custom_local_drush_cache_clear_apc() {
apc_clear_cache();
apc_clear_cache('user');
apc_clear_cache('opcode');
}
/**
* Clears the compiled PHP cache.
*
* @todo Possibly remove when http://drupal.org/node/1899842 is fixed in Drush?
*/
function custom_local_drush_cache_clear_compiled_php() {
if (drush_drupal_major_version() >= 8) {
if ($files_dir = _drush_custom_local_get_files_directory()) {
$php_dir = $files_dir . '/php';
if (is_dir($php_dir)) {
drush_op_system('sudo rm -rf "' . $php_dir . '"');
}
}
}
}
/**
* Implements drush_hook_pre_COMMAND() for site_install().
*
* Delete the PHP config directory when running the si command on D8.
*
* @todo Possibly remove when http://drupal.org/node/1899842 is fixed in Drush?
*/
function drush_custom_local_pre_site_install() {
custom_local_drush_cache_clear_compiled_php();
}
/**
* Implements drush_hook_post_COMMAND() for updatedb.
*
* Ensure the caches are cleared whenever the command is run.
*
* @todo Remove when http://drupal.org/node/602182 is fixed in Drush.
*/
function drush_custom_local_post_updatedb() {
if (drush_drupal_major_version() > 6) {
drush_invoke_process('@self', 'cache-clear', array('all'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment