master/lib/guard/dsl.rb

108 lines
3.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
instance_eval_guardfile(fetch_guardfile_contents)
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.has_key?(:guardfile_contents)
UI.info "Using inline Guardfile."
@@options[:guardfile_path] = 'Inline Guardfile'
elsif @@options.has_key?(: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
guardfile_contents
end
def guardfile_contents
2011-05-28 16:18:45 +00:00
@@options ? @@options[:guardfile_contents] : ""
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
2010-10-03 21:00:33 +00:00
end
def group(name, &guard_definition)
2011-06-01 18:12:59 +00:00
guard_definition.call if guard_definition && (@@options[:group].empty? || @@options[:group].include?(name.to_s))
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
2010-10-07 20:37:30 +00:00
::Guard.add_guard(name, @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
2010-10-03 21:00:33 +00:00
end
end