Skip to content

Instantly share code, notes, and snippets.

@rymai
Created July 29, 2011 15:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rymai/1114054 to your computer and use it in GitHub Desktop.
Save rymai/1114054 to your computer and use it in GitHub Desktop.
Watch any folder, from anywhere (new Guard CLI options, in the next release)!

In ~/prison, you put the Guardfile that describe which files to watch for changes and what to execute once a file is modified.

guard 'shell' do
  watch(%r{^.+\.txt$}) { |m| `echo #{File.join(::Guard.listener.directory, m[0])}` }
end

In ~/watch_from_here, you put the Gemfile that list the guards you want to use.

source "http://rubygems.org"

gem 'rb-fsevent' # OSX
# gem 'rb-inotify' # Linux
# gem 'rb-fchange' # Windows

# Guard is not yet released with the :watchdir and :guardfile options
gem 'guard', :git => 'git://github.com/guard/guard.git'
gem 'guard-shell' # for this example we will use guard-shell

In ~/watch_here, you create a file the_great_escape.txt that you will modify later.

You are ready to test this out!

cd ~/watch_from_here
→ bundle exec guard -watchdir ~/watch_here -guardfile ~/prison/Guardfile
# or
→ bundle exec guard -w ~/watch_here -G ~/prison/Guardfile

Then modify ~/watch_here/the_great_escape.txt and you should see the file path printed out by Guard!

YOUPI!!!

Protip: I personally use the be alias for bundle exec, but since be guard -w ~/watch_here -G ~/prison/Guardfile is still wayyyyyyy too long, I use another alias bg for bundle exec guard, that's better. And if you like the -clear/-c option of Guard, I also use the bgc for bundle exec guard -c!

@rctay
Copy link

rctay commented Jun 10, 2013

This worked great for me, 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment