Search code examples
guard

How can I "pipe" input from one guard to another?


I'm trying out guard, and I would like to write handlebars templates using haml. Is there a way to "pipe" the output of guard-haml to guard-handlebars (i.e. have 2 guards operate on the same file)? Or perhaps, is there a guard plugin that can do both steps?


Solution

  • Cobbled together a solution using guard-shell. The fundamental problem is that guard-handlebars expects it's input files to end in ".handlebars" and there is no configuration option to change that. Rather than patch guard-handlebars, I simply used guard-shell to rename the files after guard-haml had processed them. Here is the resulting code in my Guardfile:

    guard 'haml', :input => 'src/templates', :output => 'public/js/templates/html' do
        watch %r{^.+(\.hbs\.haml)$}
    end
    
    guard :shell do
      watch %r{^public/js/templates/html/.+(\.hbs\.html)$} do |m|
        path = m[0]
        new_path = path.gsub(/\.hbs\.html$/, '.handlebars')
        `mv #{path} #{new_path}`
      end
    end
    
    guard 'handlebars', :input => 'public/js/templates/html', :output => 'public/js/templates' do
      watch %r{^.+(\.handlebars)$}
    end