13th January 2016

Drush Make to the rescue!

Dan James
Senior Developer

It’s inevitable that from time to time you’ll have to apply patches to core or contrib modules. No matter how hard you try, eventually there will be some bug that you can’t work around or some feature that’s not yet complete but that you desperately need.

Traditionally we’ve always kept a comprehensive readme file stored with our projects that documents all the patches that we’ve applied, along with notes on our reasoning and links to appropriate Drupal issues.

These readme files can get quite long and make managing module updates quite challenging.

Drush Make to the rescue!

We keep two files in our project’s root directory (one level above the docroot): drupal-make.yml and drupal-make.lock.ini

drupal-make.yml

First up, we’re replacing our old markdown file with a Drush Make file, which now supports a Yaml based syntax. We can include all the patches we want to apply in here as follows (with overly-verbose comments):

core: 7.x
api: '2'
# Send projects to sites/all/modules/contrib by default.
defaults:
  projects:
    subdir: contrib

# Include our lock file, more on this later.
includes:
  - drupal-make.lock.ini

# A list of "special cases". Either modules that we're patching, or whenever
# we're using a dev branch.
projects:
  entity:
    patch:
      # Prevent notice in entity_metadata_no_hook_node_access() when node is not saved
      # https://www.drupal.org/node/2086225#comment-9627407
      - 'https://www.drupal.org/files/issues/2086225-entity-access-check-18.patch'
  devel:
    # Using dev version because Devel hasn't had a release in a long time.
    version: 1.x-dev

drupal-make.lock.ini

To make our Make file complete we also need to include details of all the other installed modules. Drush is able to generate this for us (hence the .lock extension, borrowed from composer’s .lock file)

To make this file, simply run:

drush @vdd make-generate ../drupal-make.lock.ini

(note that we’re using VDD and that we keep these files one level above the docroot)

(Re-)building Drupal

Now theoretically you no longer need to commit Drupal core or any contrib modules to your repository – the make files describe in enough detail where to get all the code from.

 To rebuild Drupal, try the following:

drush @vdd make ../drupal-make.yml

Or, if you’re applying just one patch and want a quicker process:

drush @vdd make --no-core --projects=mymodule ../drupal-make.yml

Module updates

When it comes to running module updates you’ve got two choices. Either run drush up as you probably do now, then when you're done just run the make-generate tool to get a new lock file, or use make-update to run the updates in the lock file.

Keeping our tools in one place

To keep the execution of these tools bundled up in one place, we keep a (not to be confused with Drush) Makefile in our project root:

ENVIRONMENT=vdd
DRUSH_ARGS= -y --nocolor
	
drush-make:
	chmod u+w docroot/sites/default/ docroot/sites/default/settings.php
	cd docroot && \
	  drush $(DRUSH_ARGS) make ../drupal-make.yml
	
drush-make-recreate:
	cd docroot && \
	  drush @$(ENVIRONMENT) $(DRUSH_ARGS) make-generate ../drupal-make.lock.ini
	
drush-make-apply-patch:
	cd docroot && \
	  drush $(DRUSH_ARGS) make --no-core --projects=$(MODULES) ../drupal-make.yml