Skip to content

Instantly share code, notes, and snippets.

@midwire
Last active December 31, 2015 07:09
Show Gist options
  • Save midwire/7952086 to your computer and use it in GitHub Desktop.
Save midwire/7952086 to your computer and use it in GitHub Desktop.
DRY Guardfile with defined groups for running in focused mode or default mode. Switch at the Guard prompt using scope focus, or scope default.
# Run Guard normally. This Guardfile defines 2 groups:
# 1) default: This is the default group and will run all appropriate specs when anything changes.
# 2) focus: This is the group you want when you are focusing on a specific spec or context of specs. When in this
# scope Guard will only run specs tagged with :focus.
#
# Example:
# context "GET on :index, /", focus: true do
# ...
# end
#
# Change scope at the guard prompt:
# > scope focus
# > scope default
guard 'bundler' do
watch('Gemfile')
end
# Newer versions of guard-rspec require the actual 'rspec' to be in the CLI
# TODO: Determine the version that switched this command and switch it automatically
# based on version of guard-rspec, appropriately
CLI = 'rspec --format doc'
# If on an older version you may want this one:
#CLI = '--format doc'
def watchers
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{spec/support/.+\.rb}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
watch('config/boot.rb') { 'spec' }
# for Padrino
watch(%r{^app/controllers/(.+).rb$}) { |m| ["spec/app/controllers/#{m[1]}_controller_spec.rb"] }
watch(%r{^app/models/(.+).rb$}) { |m| ["spec/app/models/#{m[1]}_spec.rb"] }
watch(%r{^app/app\.rb$}) { "spec" }
# Factories
watch(%r{^spec/factories/(.+)\.rb$}) do |m|
%W[
spec/models/#{m[1].singularize}_spec.rb
spec/controllers/#{m[1]}_controller_spec.rb
]
end
# Rabl templates
watch(%r{^app/views/(.+)/(.+)\.rabl$}) do |m|
%W[
spec/models/#{m[1].singularize}_spec.rb
spec/controllers/#{m[1]}_controller_spec.rb
]
end
# Whatever else...
end
def rubocop_watchers
watch(%r{.+\.rb$})
watch(%r{.+\.rake$})
watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
end
group :default do
guard :rspec, :cmd => CLI do
watchers
end
guard :rubocop, cli: ['-D'] do
rubocop_watchers
end
guard 'yard', port: '8000', cli: '--plugin activerecord --readme README.md' do
watch(%r{README.md})
watch(%r{app/.+\.rb})
watch(%r{lib/.+\.rb})
watch(%r{ext/.+\.c})
end
end
group :focus do
guard :rspec, :cmd => "#{CLI} --tag focus" do
watchers
end
end
scope group: :default
@midwire
Copy link
Author

midwire commented Dec 13, 2013

At the Guard prompt, run

scope focus

to switch to focus mode, and:

scope default

to switch back to default mode.

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