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
|
2011-07-02 08:01:45 +00:00
|
|
|
|
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-07-02 08:01:45 +00:00
|
|
|
|
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?
|
2011-07-02 08:01:45 +00:00
|
|
|
end
|
|
|
|
|
2011-07-28 22:22:53 +00:00
|
|
|
def reevaluate_guardfile
|
2011-07-02 08:01:45 +00:00
|
|
|
::Guard.guards.clear
|
2011-08-14 16:41:05 +00:00
|
|
|
@@options.delete(:guardfile_contents)
|
2011-07-02 08:01:45 +00:00
|
|
|
Dsl.evaluate_guardfile(@@options)
|
|
|
|
msg = "Guardfile has been re-evaluated."
|
|
|
|
UI.info(msg)
|
|
|
|
Notifier.notify(msg)
|
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?
|
2011-07-29 07:05:40 +00:00
|
|
|
if @@options[:guardfile_contents]
|
2011-05-27 15:56:18 +00:00
|
|
|
UI.info "Using inline Guardfile."
|
|
|
|
@@options[:guardfile_path] = 'Inline Guardfile'
|
2011-04-16 21:02:13 +00:00
|
|
|
|
2011-07-29 07:05:40 +00:00
|
|
|
elsif @@options[:guardfile]
|
2011-05-27 15:56:18 +00:00
|
|
|
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-04-16 21:02:13 +00:00
|
|
|
|
2010-12-17 17:44:12 +00:00
|
|
|
def guardfile_path
|
2011-07-02 08:01:45 +00:00
|
|
|
@@options ? @@options[:guardfile_path] : ""
|
2010-10-30 16:26:09 +00:00
|
|
|
end
|
2011-07-02 08:01:45 +00:00
|
|
|
|
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-06-21 18:43:30 +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)
|
2011-08-14 08:16:16 +00:00
|
|
|
@groups = @@options[:group] || []
|
2011-08-16 22:36:02 +00:00
|
|
|
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
|
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
|
2011-08-17 08:04:42 +00:00
|
|
|
options.update(:group => (@current_group || :default))
|
2011-08-17 08:45:20 +00:00
|
|
|
::Guard.add_guard(name.to_s.downcase.to_sym, @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)
|
2010-10-07 20:37:30 +00:00
|
|
|
@watchers << ::Guard::Watcher.new(pattern, action)
|
2011-04-16 21:02:13 +00:00
|
|
|
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
|