You probably know by now that as a Drupal developer, Drush can make your life much easier by allowing you to perform simple (and not so simple) tasks from the command line. In fact many of us use Drush quite extensively in our day-to-day tasks, from downloading, enabling, or disabling modules to creating, updating, or reverting features and really, so much more. Drush even allows you to run commands remotely, saving you from having to run ssh and then run the command each time.
Well, with all this coolness that Drush allows we find ourselves running the same tasks over and over. You could script them using a nice bash script to run through a list of commands, but Drush also has a very nice scripting interface that you could use.
Let’s look at an example where we will pretend that every day you come into your work environment and run a git pull and revert your features. Let’s also pretend that you sometimes want to back up your database before doing this and, on occasion, you don’t actually want to revert your features.
<?php
#!/usr/bin/env drush
/**
* This script pulls from the git repository and reverts any features
*
* This script will also take a snapshot prior to updating with the --backup
* flag and use --no-revert to skip reverting the features
*/
// check if we can bootstrap
$self = drush_sitealias_get_record('@self');
if (empty($self)) {
drush_die("I can't bootstrap from the current location.", 0);
}
drush_print("Time to prepare the working environment.");
// Check to see if we should back things up first.
if (drush_get_option('backup')) {
drush_print("But before we do, let's back things up.");
if (_backup_stuff()) {
drush_print("Backup's complete.");
}
}
// let's jump to our site directory before we do anything else
drush_op('chdir', $self['root']);
// we make a few assumptions here, like our remote origin is already set.
drush_op_system('git pull');
// if we have features let's revert them
if (drush_is_command('features-revert-all') && !drush_get_option('no-revert')) {
drush_print('Reverting features.');
drush_invoke('features-revert-all');
}
/**
* Make a database backup
*/
function _backup_stuff() {
// backup the db
$ts = date('Ymdhis');
return drush_invoke('sql-dump', array('result-file' => drush_find_tmp() . '/' . $ts . '_backup.sql', 'gzip' => TRUE));
}
?>Super simple, right? The first thing you’ll notice is the call to Drush on the very first line. This is a pretty standard way of creating a script for any scripting language; it just calls the program (in our case Drush) that will be executing the rest of the file.
Since we are wanting to bootstrap the environment so we can run backups and revert features, we will use the @self alias that is built into Drush. Using @self allows this script to be run inside our Drupal directory or by passing it our own site alias.
Notice that we are using drush_die() instead of the regular die() function and drush_print() instead of print(). Drush has wrappers built around certain php functions so they can run remotely or, in the case of print, so its output can be redirected appropriately.
Next we check the options passed by using drush_get_option, or if you’d like to loop through the options instead, you can do something like while($arg = drush_shift()), which will shift off each of the arguments you pass it.
There are a few different ways to call system calls; we use two here in drush_op and drush_op_system. The call drush_op_system allows for the drush flag --simulate to be passed, and only a debugging message about the command will be passed instead of the command being run.
This script might be run on a Drupal install that doesn’t have features installed, and if it is the runner will be greeted with an error indicating such. To prevent this, we use drush_is_command() to check that features-revert-all exists before running it with drush_invoke.
That’s it! We now have a nice script that can be built on and modified to fit our needs. Please leave a comment and let us know what type of Drush scripts you use.
If you’d like more on scripting, you can run drush topic doc-examplescript or visit the Drush API page to see what other functions can be used.
