Skip to content

Instantly share code, notes, and snippets.

@opi
Created October 8, 2013 10:19
Show Gist options
  • Select an option

  • Save opi/6882634 to your computer and use it in GitHub Desktop.

Select an option

Save opi/6882634 to your computer and use it in GitHub Desktop.
drush custom local command
<?php
/**
* Implements hook_drush_command().
*/
function custom_local_drush_command() {
$items['files-fix-permissions'] = array(
'description' => 'Fix file permissions',
'aliases' => array('ffp'),
);
$items['schema-version-get'] = array(
'description' => 'Get schema version',
'aliases' => array('svg'),
'bootstrap' => DRUSH_BOOTSTRAP_DATABASE,
);
$items['schema-version-set'] = array(
'description' => 'Set schema version',
'aliases' => array('svs'),
'bootstrap' => DRUSH_BOOTSTRAP_DATABASE,
);
$items['whereis'] = array(
'description' => 'Find module or theme location',
'bootstrap' => DRUSH_BOOTSTRAP_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() {
$files_dir = _drush_custom_local_get_files_directory();
drush_op_system('sudo chown -R $USER "' . $files_dir . '"');
drush_op_system('sudo chgrp -R www-data "' . $files_dir . '"');
drush_op_system('sudo chmod -R 775 "' . $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 $USER "' . $file->getPathname() . '"');
drush_op_system('sudo chgrp www-data "' . $file->getPathname() . '"');
drush_op_system('sudo chmod 775 "' . $file->getPathname() . '"');
}
}
}
function drush_custom_local_schema_version_get($module) {
// Ensure that install.inc will be included.
module_load_install($module);
$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 not installed or available.");
}
}
function drush_custom_local_schema_version_set($module, $version) {
// Ensure that install.inc will be included.
module_load_install($module);
$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);
}
}
/**
* Add support for clearing the bootstrap cache.
*/
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';
}
}
}
/**
* Clears the bootstrap cache.
*/
function custom_local_drush_cache_clear_bootstrap() {
cache_clear_all('*', 'cache_bootstrap', TRUE);
}
/**
* Implements drush_hook_pre_COMMAND() for site_install().
*
* Delete the PHP config directory when running the si command on D8.
*/
function drush_custom_local_pre_site_install() {
if (drush_drupal_major_version() >= 8) {
$php_dir = _drush_custom_local_get_files_directory() . '/php';
drush_op_system('sudo rm -rf "' . $php_dir . '"');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment