I'm always interested in finding ways to reduce the busy-work and get right to the fun parts of development. Drush has been the tool to help me do that. If the last three articles on Drush haven't convinced you, perhaps it's because I haven't told you about aliases and make files. Both require some setup but after that you will wonder why you didn't use them sooner.
Aliases
A Drush alias is a short name for one of three things: a specific site, a Drush command, or a shell command.
Let’s say you have three Drupal sites sharing the same codebase (multisite). We’ll call them “default”, “example.com”, and “store.example.com”. When executing a command, Drush assumes you want to run the command against the default site. But what if you want to check the status of store.example.com? That's where a site alias becomes useful!
We first need to create an alias file where we can define our aliases for Drush. Although it’s possible to add system-wide aliases in /etc/drush
, for this example we are going to create the file drushrc.php
in our user’s home folder. (If you don't already have a .drush
folder, create one.)
~/.drush/drushrc.php
Now we define our alias within that file. In the example code below, I'm defining the alias "store" by telling Drush where to find the Drupal root and the domain name of this particular site. Copy this example and insert it into your file. Replace the alias, root, and uri with the information that fits your situation.
<?php $aliases['store'] = array( 'root' => '/path/to/drupal', 'uri' => 'store.example.com', );
Save the file and you are done! You have just created a site alias. Now you can use this new alias in commands. Prefixed with @
, your alias should be the second thing you type, as in the command below.
drush @store status
This will return the status of the site “store.example.com”. You can create as many aliases as you want.
Next, we are going to create an alias for a Drush command and a shell command. In the same file to which you added the site aliases, add these two lines:
$options['shell-aliases']['enabled'] = 'pm-list --no-core --status=enabled'; $options['shell-aliases']['add'] = '!git add .';
The first line is a Drush command for listing all non-core projects enabled on the current site. I have defined the alias to be "enabled". The second is a shell command defined as "add", an alias to stage your new or changed files with Git. You must prefix all shell commands with an exclamation mark. With these now defined, I need only type drush enabled
or drush add
to execute.
Makefiles
A makefile is basically a long list of projects used in combination with Drush to assemble all the files you need to start the Drupal installer. Think of it as a shopping list for Drush: The list contains all the items Drush needs to get for you, where to get them, and, in some cases, exactly what version to get.
Makefiles can be included in modules to ensure you get the correct library, or can potentially be combined with an install profile for a nearly hands-off install of a distribution. For me, I use them to download modules, themes, and libraries that I typically use when creating a new Drupal project. It saves me a lot of setup time.
The easiest way to create a makefile is to use make-generate
. Using an existing site you can ask Drush to assemble a make file for you with one command. Combine what we learned above with aliases and create a makefile using the command below.
drush @store make-generate mystore.make
This will take a moment but in the end it will save a file named mystore.make
that lists all the enabled modules, themes, Drupal core, and their version numbers used in the site I ran the command against. It also scans for all the available libraries found in the site's libraries folder, and includes all of them even if you aren't using one.
My preference is to generate makefiles without a specific version number. I run the same command above but with the --exclude-versions
parameter. By excluding the version numbers, Drush will download the recommended release instead of a specified version.
Drush will save the makefile in the Drupal root. Open this file in an editor and look toward the bottom of the file. Any projects or libraries Drush couldn't find on Drupal.org will be listed there. Here is an example of what that looks like:
libraries[FooTable][download][type] = "" libraries[FooTable][download][url] = "" libraries[FooTable][directory_name] = "FooTable" libraries[FooTable][type] = "library"
Now to get a fully working makefile you only need to fill in the missing information. In the case of the above library we need values for type
and url
. I'm going to provide a Git repo, so I will insert git
for type
and the git repo's location for url
. It will look something like this:
libraries[FooTable][download][type] = "git" libraries[FooTable][download][url] = "https://github.com/bradvin/FooTable.git"
If your project is packaged as a zip or tar you can define type
as file
instead, and then include the correct location of the file.
Now, for the magic! We're going to use the makefile we just created to start a new project. Here's the command:
drush make mystore.make drupalstore
In this command I'm telling drush to use mystore.make
to build my new project and to put it in a new folder called drupalstore
. Drush will do all the work including the pulling from git or the unzipping of files. Just sit back, drink up, and let it do the work!
If you take the time to set up aliases and makefiles you will most certainly become more productive with your time. The only thing Drush is lacking now is the command to pour me another beer.
Image: "Untitled" by afglobalstrike is licensed under CC BY 2.0