Minimum viable guard

Published
2016-08-07
Tagged

Guard is a cool ruby library for automatically performing tasks every time a file changes. But if you’re not sure what to expect, it can be hard to set up. Here’s the quickest possible setup for Guard.

Use case

In this case, I’m making a quick website prototype in haml. I want to make sure that, whenever I modify one of my haml files, ruby immediately produces the equivalent html file for me in the same directory.

Installation

To set up guard, first you must install it:

1
gem install guard

Once that’s all done, you’re ready to go!

Setup

Navigate to the root directory for your project and create a new file, named Guardfile. This is where you tell guard what to do.

Guard runs using a series of plugins, which must be subclasses of Guard::Plugin. There’s a few methods you can override for your plugin, but the most important are start and run_on_modifications:

  • start runs when the plugin is loaded. It’s useful for informing you that the plugin successfully loaded, or for setting up any initial variables you need set up.
  • run_on_modifications runs whenever a watched file is modified. We’ll talk about watched files in a bit - what you need to know is that guard will pass this method an array of paths, corresponding to the modified files.

Let’s have a look at a sample plugin for turning haml files into html files:

1
# I need this to run haml
2
require "haml"
3
4
class Guard::Hamliser < Guard::Plugin 
5
  def start
6
    puts "Starting Hamliser"
7
  end
8
9
  def run_on_modifications(paths)
10
    paths.each do |haml_file|
11
      html_file = haml_file.gsub(".haml", ".html")
12
      # Render the HAML file into the HTML file
13
      File.open(html_file, "w"){ |io| io.puts Haml::Engine.new(File.read(haml_file)).render }
14
      puts "Rendered #{haml_file} to #{html_file}"
15
    end
16
  end
17
end

You don’t need to print a status message for every modification, but I find it handy.

Watching files

The last thing you need to do is tell guard what to watch. You do this with the guard method, which takes one argument (the Plugin subclass to run, in string form) and a block. The block tells guard which files to watch through a series of different commands, but the most basic is the watch method:

1
guard("hamliser") do
2
  watch %r|.*\.haml$|
3
end

watch takes a string or regex argument, and matches the path of each changes file against this. You can run watch multiple times within the same block: as one of the watch arguments matches, the path will be passed to run_on_modifications. Here, I specify that I only care about files ending in .haml.

Gluing it together and making it go

Now all that remains is to add the guard syntax to the end of my guard file:

1
require "haml"
2
3
class Guard::Hamliser < Guard::Plugin 
4
  def start
5
    puts "Starting Hamliser"
6
  end
7
8
  def run_on_modifications(paths)
9
    paths.each do |haml_file|
10
      html_file = haml_file.gsub(".haml", ".html")
11
      # Render the HAML file into the HTML file
12
      File.open(html_file, "w"){ |io| io.puts Haml::Engine.new(File.read(haml_file)).render }
13
      puts "Rendered #{haml_file} to #{html_file}"
14
    end
15
  end
16
end
17
18
guard("hamliser"){ watch %r|.*\.haml$| }

To run this from the command line, navigate to your root folder and run the guard command. You’re all ready to go!