Search code examples
ruby-on-railsguard

Guardfile DSL - How to make a site-specific config


I'm trying to use Guard on several different machines for the same project.

One machine is OSX. Another machine's developer wants the emacs notifications. I personally loathe the tmux notifications as well as the emacs notifications, but I really like the terminal title and libnotify popups.

I thought I was clever when I added to the beginning of my Guardfile:

Dir.glob("./Guardfile-site-*").each { |file| include file }

which simply contains:

notification :libnotify, :timeout => 5, :transient => true, :append => false, :urgency => :critical

Then adding the file, /Guardfile-site-myname-home-dev to the .gitignore

Unfortunately that yields

[1] guard(main)> 01:21:38 - ERROR - Invalid Guardfile, original error is:
> [#] undefined method `include' for #<Guard::Dsl:0x000000037e5920>

What is the "right way" to do this without my having to reconfigure this manually every time in Guard?

I'm thinking environment variables. Setting GUARD_NOTIFY to FALSE will stop them entirely but that's not what I want.


Solution

  • You can put user specific config into ~/.guard.rb. Guard will prepend the contents of that file to your Guardfile automatically.

    That would work for you if you are happy to use the same config across all projects.

    If you need to tweak settings on a per-project basis then you can adapt the code you already have. include won't work because its a class method on Module and it takes a module parameter. Looking at the way guard loads a Guardfile something like this should work:

    Dir.glob("Guardfile-site-*").each { |file| config = File.read("#{File.dirname(__FILE__)}/#{file}"); instance_eval(config) }