2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
|
|
|
class Dsl
|
2010-12-17 15:31:39 +00:00
|
|
|
class << self
|
2011-05-28 16:18:45 +00:00
|
|
|
@@options = nil
|
|
|
|
|
2010-12-17 15:31:39 +00:00
|
|
|
def evaluate_guardfile(options = {})
|
2011-05-27 15:56:18 +00:00
|
|
|
options.is_a?(Hash) or raise ArgumentError.new("evaluate_guardfile not passed a Hash!")
|
2011-05-05 09:05:58 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
@@options = options.dup
|
|
|
|
instance_eval_guardfile(fetch_guardfile_contents)
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
def instance_eval_guardfile(contents)
|
2011-05-05 09:05:58 +00:00
|
|
|
begin
|
2011-05-27 15:56:18 +00:00
|
|
|
new.instance_eval(contents, @@options[:guardfile_path], 1)
|
2011-05-05 09:05:58 +00:00
|
|
|
rescue
|
|
|
|
UI.error "Invalid Guardfile, original error is:\n#{$!}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
def guardfile_include?(guard_name)
|
|
|
|
guardfile_contents.match(/^guard\s*\(?\s*['":]#{guard_name}['"]?/)
|
2011-05-05 09:05:58 +00:00
|
|
|
end
|
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
def read_guardfile(guardfile_path)
|
2011-05-05 09:05:58 +00:00
|
|
|
begin
|
2011-05-27 15:56:18 +00:00
|
|
|
@@options[:guardfile_path] = guardfile_path
|
|
|
|
@@options[:guardfile_contents] = File.read(guardfile_path)
|
2011-05-05 09:05:58 +00:00
|
|
|
rescue
|
2011-05-27 15:56:18 +00:00
|
|
|
UI.error("Error reading file #{guardfile_path}")
|
2011-05-05 09:05:58 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
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]}."
|
2011-05-05 09:05:58 +00:00
|
|
|
else
|
2011-05-27 15:56:18 +00:00
|
|
|
UI.error "No Guardfile exists at #{@@options[:guardfile]}."
|
2010-12-17 15:31:39 +00:00
|
|
|
exit 1
|
|
|
|
end
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2010-12-17 15:31:39 +00:00
|
|
|
else
|
2011-05-05 09:05:58 +00:00
|
|
|
if File.exist?(guardfile_default_path)
|
|
|
|
read_guardfile(guardfile_default_path)
|
|
|
|
else
|
2011-05-28 15:52:50 +00:00
|
|
|
UI.error "No Guardfile found, please create one with `guard init`."
|
2011-05-05 09:05:58 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
2011-05-27 15:56:18 +00:00
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
unless guardfile_contents_usable?
|
2011-05-27 15:56:18 +00:00
|
|
|
UI.error "The command file(#{@@options[:guardfile]}) seems to be empty."
|
2010-10-30 16:26:09 +00:00
|
|
|
exit 1
|
|
|
|
end
|
2011-05-28 15:52:50 +00:00
|
|
|
|
2011-05-27 15:56:18 +00:00
|
|
|
guardfile_contents
|
2010-12-17 15:31:39 +00:00
|
|
|
end
|
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
def guardfile_contents
|
2011-05-28 16:18:45 +00:00
|
|
|
@@options ? @@options[:guardfile_contents] : ""
|
2010-12-17 17:44:12 +00:00
|
|
|
end
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
def guardfile_contents_usable?
|
2011-05-27 15:56:18 +00:00
|
|
|
guardfile_contents && guardfile_contents.size >= 'guard :a'.size # smallest guard-definition
|
2011-05-05 09:05:58 +00:00
|
|
|
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-05-25 18:23:02 +00:00
|
|
|
File.expand_path(File.join("~", "Guardfile"))
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-05-05 09:05:58 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
def group(name, &guard_definition)
|
|
|
|
guard_definition.call if guard_definition && (@@options[:group].empty? || @@options[:group].include?(name))
|
2010-10-07 20:37:30 +00:00
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
def guard(name, options = {}, &watch_definition)
|
2010-10-03 21:00:33 +00:00
|
|
|
@watchers = []
|
2010-12-17 17:13:31 +00:00
|
|
|
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-12-17 17:13:31 +00:00
|
|
|
|
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-12-17 17:13:31 +00:00
|
|
|
|
2010-10-03 21:00:33 +00:00
|
|
|
end
|
|
|
|
end
|