master/lib/guard/dsl.rb

143 lines
4.0 KiB
Ruby
Raw Normal View History

2010-10-03 21:00:33 +00:00
module Guard
class Dsl
class << self
2011-05-28 16:18:45 +00:00
@@options = nil
def evaluate_guardfile(options = {})
options.is_a?(Hash) or raise ArgumentError.new("evaluate_guardfile not passed a Hash!")
@@options = options.dup
fetch_guardfile_contents
instance_eval_guardfile(guardfile_contents_with_user_config)
2011-07-15 06:32:34 +00:00
UI.error "No guards found in Guardfile, please add at least one." if !::Guard.guards.nil? && ::Guard.guards.empty?
end
def reevaluate_guardfile
::Guard.guards.clear
@@options.delete(:guardfile_contents)
Dsl.evaluate_guardfile(@@options)
msg = "Guardfile has been re-evaluated."
UI.info(msg)
Notifier.notify(msg)
end
def instance_eval_guardfile(contents)
begin
new.instance_eval(contents, @@options[:guardfile_path], 1)
rescue
UI.error "Invalid Guardfile, original error is:\n#{$!}"
exit 1
end
end
def guardfile_include?(guard_name)
guardfile_contents.match(/^guard\s*\(?\s*['":]#{guard_name}['"]?/)
end
def read_guardfile(guardfile_path)
begin
@@options[:guardfile_path] = guardfile_path
@@options[:guardfile_contents] = File.read(guardfile_path)
rescue
UI.error("Error reading file #{guardfile_path}")
exit 1
end
end
def fetch_guardfile_contents
# TODO: do we need .rc file interaction?
if @@options[:guardfile_contents]
UI.info "Using inline Guardfile."
@@options[:guardfile_path] = 'Inline Guardfile'
elsif @@options[:guardfile]
if File.exist?(@@options[:guardfile])
read_guardfile(@@options[:guardfile])
UI.info "Using Guardfile at #{@@options[:guardfile]}."
else
UI.error "No Guardfile exists at #{@@options[:guardfile]}."
exit 1
end
else
if File.exist?(guardfile_default_path)
read_guardfile(guardfile_default_path)
else
UI.error "No Guardfile found, please create one with `guard init`."
exit 1
end
end
unless guardfile_contents_usable?
UI.error "The command file(#{@@options[:guardfile]}) seems to be empty."
exit 1
end
end
def guardfile_contents
2011-05-28 16:18:45 +00:00
@@options ? @@options[:guardfile_contents] : ""
end
def guardfile_contents_with_user_config
config = File.read(user_config_path) if File.exist?(user_config_path)
[guardfile_contents, config].join("\n")
end
def guardfile_path
@@options ? @@options[:guardfile_path] : ""
end
def guardfile_contents_usable?
guardfile_contents && guardfile_contents.size >= 'guard :a'.size # smallest guard-definition
end
def guardfile_default_path
2011-05-25 18:23:02 +00:00
File.exist?(local_guardfile_path) ? local_guardfile_path : home_guardfile_path
2011-05-24 21:32:02 +00:00
end
2011-05-25 19:08:11 +00:00
private
2011-05-24 21:32:02 +00:00
def local_guardfile_path
File.join(Dir.pwd, "Guardfile")
end
def home_guardfile_path
2011-06-21 18:43:30 +00:00
File.expand_path(File.join("~", ".Guardfile"))
end
def user_config_path
File.expand_path(File.join("~", ".guard.rb"))
end
2010-10-03 21:00:33 +00:00
end
def group(name, &guard_definition)
@groups = @@options[:group] || []
name = name.to_sym
if guard_definition && (@groups.empty? || @groups.map(&:to_sym).include?(name))
@current_group = name
guard_definition.call
@current_group = nil
end
2010-10-07 20:37:30 +00:00
end
def guard(name, options = {}, &watch_definition)
2010-10-03 21:00:33 +00:00
@watchers = []
watch_definition.call if watch_definition
options.update(:group => (@current_group || :default))
::Guard.add_guard(name.to_s.downcase.to_sym, @watchers, options)
2010-10-03 21:00:33 +00:00
end
2010-10-03 21:00:33 +00:00
def watch(pattern, &action)
2010-10-07 20:37:30 +00:00
@watchers << ::Guard::Watcher.new(pattern, action)
2010-10-03 21:00:33 +00:00
end
2011-09-01 11:30:25 +00:00
def ignore_paths(*paths)
2011-09-01 11:43:02 +00:00
UI.info "Ignoring paths: #{paths.join(', ')}"
2011-09-01 11:30:25 +00:00
::Guard.listener.ignore_paths.push(*paths)
end
2010-10-03 21:00:33 +00:00
end
end