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 = {})
|
2010-12-17 17:13:31 +00:00
|
|
|
@@options = options
|
2011-04-16 21:02:13 +00:00
|
|
|
|
2010-12-17 17:44:12 +00:00
|
|
|
if File.exists?(guardfile_path)
|
2010-12-17 15:31:39 +00:00
|
|
|
begin
|
2010-12-17 17:44:12 +00:00
|
|
|
new.instance_eval(File.read(guardfile_path), guardfile_path, 1)
|
2010-12-17 15:31:39 +00:00
|
|
|
rescue
|
|
|
|
UI.error "Invalid Guardfile, original error is:\n#{$!}"
|
|
|
|
exit 1
|
|
|
|
end
|
|
|
|
else
|
|
|
|
UI.error "No Guardfile in current folder, please create one."
|
2010-10-30 16:26:09 +00:00
|
|
|
exit 1
|
|
|
|
end
|
2010-12-17 15:31:39 +00:00
|
|
|
end
|
|
|
|
|
2010-12-17 17:37:24 +00:00
|
|
|
def guardfile_include?(guard_name)
|
2010-12-17 17:44:12 +00:00
|
|
|
File.read(guardfile_path).match(/^guard\s*\(?\s*['":]#{guard_name}['"]?/)
|
|
|
|
end
|
2011-04-16 21:02:13 +00:00
|
|
|
|
2010-12-17 17:44:12 +00:00
|
|
|
def guardfile_path
|
|
|
|
File.join(Dir.pwd, 'Guardfile')
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
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
|
|
|
|
2011-04-16 21:02:13 +00:00
|
|
|
def guard(name, options = {}, &watch_and_callback_definition)
|
|
|
|
@watchers = []
|
|
|
|
@callbacks = []
|
|
|
|
watch_and_callback_definition.call if watch_and_callback_definition
|
|
|
|
::Guard.add_guard(name, @watchers, @callbacks, 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)
|
2011-04-16 21:02:13 +00:00
|
|
|
@watchers << { :pattern => pattern, :action => action }
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback(*args, &listener)
|
2011-04-29 22:40:12 +00:00
|
|
|
listener, events = args.size > 1 ? args : [listener, args[0]]
|
2011-04-16 21:02:13 +00:00
|
|
|
@callbacks << { :events => events, :listener => listener }
|
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
|