2010-10-03 21:00:33 +00:00
|
|
|
module Guard
|
|
|
|
class Dsl
|
2010-12-17 15:31:39 +00:00
|
|
|
class << self
|
|
|
|
def evaluate_guardfile(options = {})
|
2011-05-05 09:05:58 +00:00
|
|
|
options.is_a?(Hash) or raise ArgumentError.new("evaluate_guardfile not passed a hash")
|
|
|
|
|
2010-12-17 17:13:31 +00:00
|
|
|
@@options = options
|
2011-05-05 09:05:58 +00:00
|
|
|
prep_guardfile_contents
|
|
|
|
instance_eval_guardfile(guardfile_contents, actual_guardfile(), 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
def instance_eval_guardfile(contents, path, line)
|
|
|
|
begin
|
|
|
|
new.instance_eval(contents, path, line)
|
|
|
|
rescue
|
|
|
|
UI.error "Invalid Guardfile, original error is:\n#{$!}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardfile_include?(guard_name, guardfile = guardfile_contents)
|
|
|
|
guardfile.match(/^guard\s*\(?\s*['":]#{guard_name}['"]?/)
|
|
|
|
end
|
|
|
|
|
|
|
|
def read_guardfile(my_file)
|
|
|
|
@@options[:actual_guardfile] = my_file
|
|
|
|
begin
|
|
|
|
@@options[:guardfile_contents] = File.read(my_file)
|
|
|
|
rescue
|
|
|
|
UI.error("Error reading file #{my_file}")
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def prep_guardfile_contents
|
|
|
|
#todo do we need .rc file interaction?
|
|
|
|
if @@options.has_key?(:guardfile_contents)
|
|
|
|
UI.info "Using options[:guardfile_contents] for Guardfile"
|
|
|
|
@@options[:actual_guardfile] = 'options[:guardfile_contents]'
|
|
|
|
elsif @@options.has_key?(:guardfile)
|
|
|
|
UI.info "Using -command Guardfile"
|
|
|
|
if File.exist?(guardfile_file())
|
|
|
|
read_guardfile(guardfile_file)
|
|
|
|
else
|
|
|
|
UI.error "No Guardfile exists at #{guardfile_file}. Check your -command option"
|
2010-12-17 15:31:39 +00:00
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
else
|
2011-05-05 09:05:58 +00:00
|
|
|
UI.info "Using Guardfile in current dir"
|
|
|
|
if File.exist?(guardfile_default_path)
|
|
|
|
read_guardfile(guardfile_default_path)
|
|
|
|
else
|
|
|
|
UI.error "No Guardfile in current folder, please create one."
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless guardfile_contents_usable?
|
|
|
|
UI.error "The command file(#{@@options[:guardfile_file]}) seems to be empty."
|
2010-10-30 16:26:09 +00:00
|
|
|
exit 1
|
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
end
|
|
|
|
|
2011-05-05 09:05:58 +00:00
|
|
|
def guardfile_contents
|
|
|
|
@@options[:guardfile_contents]
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardfile_file
|
|
|
|
@@options[:guardfile]
|
|
|
|
end
|
|
|
|
|
|
|
|
def actual_guardfile
|
|
|
|
@@options[:actual_guardfile]
|
2010-12-17 17:44:12 +00:00
|
|
|
end
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
def guardfile_contents_usable?
|
|
|
|
guardfile_contents && guardfile_contents.length > 0 #TODO maybe the min length of the smallest possible definition?
|
|
|
|
end
|
|
|
|
|
|
|
|
def guardfile_default_path
|
2010-12-17 17:44:12 +00:00
|
|
|
File.join(Dir.pwd, 'Guardfile')
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-05-05 09:05:58 +00:00
|
|
|
|
|
|
|
def parent_path
|
|
|
|
@@options[:parent]
|
|
|
|
end
|
|
|
|
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
|
|
|
|
end
|
|
|
|
end
|