Skip to content


Subversion checkout URL

You can clone with
Download ZIP

Use Guard programmatically cookbook

Cezary Baginski edited this page · 7 revisions
Clone this wiki locally

The flexibility of Guard allows for some creative uses beyond simply running tests, etc. If you've come up with interesting atypical use cases or useful code snippets, please share them here!

Use Guard programmatically in a Rake (or Capistrano) task

Perhaps you wish to compile asset files from CoffeeScript, LESS, SCSS or the like on-demand without starting Guard in a long-running fashion. You may be prototyping an early-stage app and want to precompile these assets and rsync everything straight to a server, or you're simply using some other custom strategy of deploying assets where you want control of when and how this is done. In that case, you might want a Rake or Capistrano task for the job, and why duplicate the effort to specify where the source files are, where the built ones should go, and how to build them if you already have a Guard plugin configured to do so? Here's an example of using Guard programmatically in a Rake task to compile LESS to CSS:

require 'guard'
require 'guard/commander' # needed because of

namespace :assets do
  desc 'Generate CSS from all source LESS files'
  task :less do
    # Setup Guard and pass options directly to .setup
    Guard.setup(group: ['frontend'])

    # With Guard 0.8.0+ this will find the first defined 'less' guard:

    # If you're stuck on an older version for some reason, you can find manually:
    # less = { |g| g.is_a? Guard::Less }.first
    # less.run_all

If you want to do something like this in a Capistrano task run remotely, remember that you may need to include Guard and the needed plugins in a Bundler group other than development in your Gemfile.

Use Guard from a simple Ruby script

In case you can't or don't want to use a Rake task, it's also possible to invoke Guard from a simple script file – which you can run using bundle exec ruby myscript.rb from the Terminal console.

require 'rubygems'
require 'bundler/setup'

require 'guard'
require 'guard/commander' # needed because of
require 'rake'

# Optional: If you use Guard::Rake you also have to manually start up Rake to be able to process the Rakefile
rake =
Rake.application = rake

# Let's load Guard and process the Guardfile

# Finally run all Guards

Starting Guard within your application

You can start guard together with the rails application, just create a config/initializers/guard.rb with something like:

require 'guard'
require 'guard/commander' # needed because of

fork do
  if __FILE__ == '(irb)'

  Guard.guards 'sass'
  Guard.start :no_interactions => true
Something went wrong with that request. Please try again.