-
Notifications
You must be signed in to change notification settings - Fork 483
Use Guard programmatically cookbook
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!
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 https://github.com/guard/guard/issues/793
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:
Guard.guards('less').run_all
# If you're stuck on an older version for some reason, you can find manually:
# less = Guard.guards.select { |g| g.is_a? Guard::Less }.first
# less.run_all
end
end
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
.
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 https://github.com/guard/guard/issues/793
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.new
Rake.application = rake
rake.init
rake.load_rakefile
# Let's load Guard and process the Guardfile
Guard.setup
# Finally run all Guards
Guard.run_all
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 https://github.com/guard/guard/issues/793
# Start Guard only with rails.
if File.basename($0, '.*') == 'rails'
fork do
if __FILE__ == '(irb)'
end
Guard.guards 'sass'
Guard.start :no_interactions => true
end
end
This wiki and the Guard README document contain a lot of information, please take your time and read these instructions carefully.
If you run into any trouble, you may start by understanding how Guard works.
We provide detailed changes for each Guard release.
Be sure to read the CONTRIBUTING guidelines before reporting a new Guard issue or open a pull request.
If you have any questions about the Guard usage or want to share some information with the Guard community, please go to one of the following places:
- Google+ community
- Google group
- StackOverflow
- IRC channel
#guard
(irc.freenode.net) for chatting