You are on page 1of 23

San Francisco Drupal Users Group

DRUSH
A Presentation on Recent Changes by
Greg Anderson

10 February 2014 San Francisco Group Drush


Drupal Users2014
10 February 1
Drush Installation
Recommended method is now Composer

10 February 2014 San Francisco Drupal Users Group Drush 2


Benefits of Composer

Manages and installs dependencies

Drupal 8 is using Composer

Comes with PSR-0 / PSR-4 autoloader

10 February 2014 San Francisco Drupal Users Group Drush 3


PSR-What?
PSR-0 PSR-4

Source: http://www.sitepoint.com/battle-autoloaders-psr-0-vs-psr-4/ Note: Drupal currently uses 'lib', but may switch to 'src'

10 February 2014 San Francisco Drupal Users Group Drush 4


Autoloading in Drush
Bold text removed with Autoloader
function drush_get_class($class_name, $args, $class_dir = NULL) {
if (!isset($class_dir)) {
$class_dir = DRUSH_BASE_PATH . '/classes';
}
$version = drush_drupal_major_version();
drush_include($class_dir, $class_name, $version, 'php');
$versioned_name = $class_name . $version;
if (class_exists($versioned_name)) {
$reflectionClass = new ReflectionClass($versioned_name);
return $reflectionClass->newInstanceArgs($args);
}
}

NOT COMMITTED YET. Issue: https://github.com/drush-ops/drush/pull/88

10 February 2014 San Francisco Drupal Users Group Drush 5


Autoloading with Drush Commands
$items['role-list'] = array(
'description' => 'Display a list of all roles.',
'examples' => array(
"drush role-list 'anonymous user'" => 'Display
all of the permissions assigned to the anonymous
user role.'
),
'command-class' => 'Drush\core\RoleCommand',
'aliases' => array('rls'),
);
NOT IMPELEMENTED YET. Issue: https://github.com/drush-ops/drush/pull/88

Advantage: Drush can create a RoleCommand7


for Drupal 7, and a RoleCommand8 for Drupal8.
The base class can handle logic and argument
parsing, and derived classes provide active code.
10 February 2014 San Francisco Drupal Users Group Drush 6
DEMO

$ drush @site php

$ drush @remote php

$ drush ev return menu_get_object();

Q: Why return instead of print?

10 February 2014 San Francisco Drupal Users Group Drush 7


Basics of Drush Output Formats
Drush 5 Way: New Way in Drush 6:
function drush_hello_world() { function drush_hello_world() {
print "Hello World." return "Hello World."
} }

Drush will now print our results for you if you


return them as the result of your main command
function.

A secondary advantage of this is that Drush will


return
10 February 2014 a proper
Sanobject when
Francisco Drupal Users Group called
Drush via a remote
8
DEMO
$ drush hello
The drush command hello could not
be found.
$ drush dl drushify
$ drush drushify hello
$ drush hello
Language Message
English
We will use theHello
drushifyworld!
code generator to make
a quick Hello World Drush command

10 February 2014 San Francisco Drupal Users Group Drush 9


Selecting Output Format

$ drush status --format=json

Most Drush commands now support formatters

Commands return data (usually in arrays)

Formatters render the data in different ways

10 February 2014 San Francisco Drupal Users Group Drush 10


Declaring Default Formats
$items['hello'] = array(

'outputformat' => array(
'default' => 'table',
'pipe-format' => 'var_export',
),
);
The minimum requirements for a command to
support the --format option is to declare its
default types

10 February 2014 San Francisco Drupal Users Group Drush 11


Declaring Supported Data Types
$items['hello'] = array(

'outputformat' => array(
'default' => 'table',
'pipe-format' => 'var_export',
'output-data-type' => 'format-
table',
Commands may also declare a data type; this will

exclude incompatible formatters from drush
help, etc.

10 February 2014 San Francisco Drupal Users Group Drush 12


Available Output Data Types

Data Type Available Formatters


'format-table' Table
CSV (one record per row, csv fields)

'format-list' List (one-dimensional array of values)


Key-value (values with labels)

'format-single' String

TRUE Special value that selects only those formatters that


work with all data types:
var_export, yaml, json, etc.

10 February 2014 San Francisco Drupal Users Group Drush 13


Declare Field Labels
$items['hello'] = array(

'outputformat' => array(
'field-labels' => array(
'mgs' => 'Message',
'lang' => 'Language',
),
'fields-default' => array(
'msg', 'lang'),

10 February 2014 San Francisco Drupal Users Group Drush 14
DEMO

$ drush hello --fields=msg

$ drush hello --fields=lang

$ drush hello --fields=Message,Language

10 February 2014 San Francisco Drupal Users Group Drush 15


Create a Scratch Drupal Site

$ drush qd d7 og cck devel --site-name=SFDUG

and, while we're waiting for that to run...

$ drush @remote uli admin admin/index

10 February 2014 San Francisco Drupal Users Group Drush 16


Drush Command Hooks
$ drush status --show-invoke 2>&1 |
grep hello
drush_hello_core_status_pre_validate
drush_hello_core_status_validate
drush_hello_pre_core_status
drush_hello_core_status
drush_hello_post_core_status
Note: The output from --show-invoke is sent to
STDERR, just like the Drush log and error output; we
redirect STDERR to STDOUT to use grep.

10 February 2014 San Francisco Drupal Users Group Drush 17


Example Post-Sync Hook
function drush_sync_enable_post_sql_sync($src, $dst) {
$modules = drush_get_option_list('enable');
if (!empty($modules)) {
drush_log(dt("Enable !modules post-sql-sync",
array('!modules' => implode(',', $modules))),
'ok');
drush_invoke_process($dst, 'en', $modules,
array('yes' => TRUE));
}
// processing continues below

10 February 2014 San Francisco Drupal Users Group Drush 18


Using Sync-Enable Hook
Copy the example commandfile:
$ cp examples/sync_enable.drush.inc ~/.drush

Configure your site alias:


$aliases["dev"] = array (
'root' => '/path/to/drupalroot',
'uri' => 'http://dev.site.org',
'target-command-specific' => array
(
'sql-sync' => array (
'enable' => array ('devel',
'stage_file_proxy'
10 February 2014
),
San Francisco Drupal Users Group Drush 19
Use settings.php to modify variables
$conf['error_level'] = 2;
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors',
TRUE);
$conf['preprocess_css'] = 0;
$conf['cache'] = that
It is frequently suggested 0; the sync_enable hook
should support variable_set() and variable_delete();
however, this is not necessary, as $conf statements in
settings.php can be used to the same effect.

10 February 2014 San Francisco Drupal Users Group Drush 20


Sanitize Hook to Clear CCK Fields
function
MYMODULE_drush_sql_sync_sanitize($site) {
$fields_to_sanitize = array(
'field_baby_pictures_url' =>
'http://bit.ly/1daFGUm',
'field_secret_missile_launch_codes' => '1234',
);

foreach ($fields_to_sanitize as $field_name =>


$field_value) {
10 February 2014San Francisco Drupal Users Group Drush 21
$sql = "UPDATE field_data_{$field_name} SET
Spur of the Moment Demos
$ drush topic # bastion

examples/example.bashrc # cd @site:%files

$ drush @remote status-report


$ drush pm-updatestatus
$ drush pm-update --lock=module-to-skip

10 February 2014 San Francisco Drupal Users Group Drush 22


Drush Resources

Project Page:
https://github.com/drush-ops/drush
Support:
http://drupal.stackexchange.com/questions/tagged/drush
Drush Site:
http://drush.org
Drush Chapter of DGD7 (Free Download):
http://dgd7.org/book/26-drush

10 February 2014 San Francisco Drupal Users Group Drush 23

You might also like