Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Guard::Shell automatically run shell commands when watched files are modified.

branch: master

This branch is 0 commits ahead and 0 commits behind master

Merge pull request #6 from nerde/master

Fixing example.
latest commit eb4ac06b1c
Joshua Hawxwell hawx authored
Octocat-spinner-32 lib Version bump to 0.4.0 March 09, 2012
Octocat-spinner-32 .gitignore Version bump to 0.1 October 16, 2010
Octocat-spinner-32 LICENSE Initial commit October 16, 2010
Octocat-spinner-32 Readme.md Fixing example. January 07, 2014
Octocat-spinner-32 guard-shell.gemspec Fix warning message upon build on Linux January 24, 2012
Readme.md

Guard::Shell

This little guard allows you to run shell commands when files are altered.

Install

Make sure you have guard installed.

Install the gem with:

gem install guard-shell

Or add it to your Gemfile:

gem 'guard-shell'

And then add a basic setup to your Guardfile:

guard init shell

Usage

If you can do something in your shell, or in ruby, you can do it when a file changes with guard-shell. It simply executes the block passed to watch if a change is detected, and if anything is returned from the block it will be printed. For example

guard :shell do
  watch /.*/ do |m|
    m[0] + " has changed."
  end
end

will simply print a message telling you a file has been changed when it is changed. This admittedly isn't a very useful example, but you hopefully get the idea. To run everything on start pass :all_on_start to #guard,

guard :shell, :all_on_start => true do
  # ...
end

There is also a shortcut for easily creating notifications,

guard :shell do
  watch /.*/ do |m|
    n m[0], 'File Changed'
  end
end

#n takes up to three arguments; the first is the body of the message, here the path of the changed file; the second is the title for the notification; and the third is the image to use. There are three (four counting nil the default) different images that can be specified :success, :pending and :failed.

Examples

Saying the Name of the File You Changed and Displaying a Notification

guard :shell do
  watch /(.*)/ do |m|
    n m[0], 'Changed'
    `say -v cello #{m[0]}`
  end
end

Rebuilding LaTeX

guard :shell, :all_on_start => true do
  watch /^([^\/]*)\.tex/ do |m|
    `pdflatex -shell-escape #{m[0]}`
    `rm #{m[1]}.log`

    count = `texcount -inc -nc -1 #{m[0]}`.split('+').first
    msg = "Built #{m[1]}.pdf (#{count} words)"
    n msg, 'LaTeX'
    "-> #{msg}"
  end
end

Check Syntax of a Ruby File

guard :shell do
  watch /.*\.rb$/ do |m|
    if system("ruby -c #{m[0]}")
      n "#{m[0]} is correct", 'Ruby Syntax', :success
    else
      n "#{m[0]} is incorrect", 'Ruby Syntax', :failed
    end
  end
end
Something went wrong with that request. Please try again.