lib/guard/depend.rb in guard-depend-0.0.1 vs lib/guard/depend.rb in guard-depend-0.1.0
- old
+ new
@@ -1,21 +1,30 @@
require 'guard'
require 'guard/plugin'
module Guard
class Depend < Plugin
- require 'guard/depend/options'
+ require 'guard/depend/detect'
require 'guard/depend/runner'
+ DEFAULTS = {
+ run_on_start: false,
+ output_paths: [],
+ cmd: nil
+ }
+
+ attr_reader :options, :runner, :detect
+
def initialize(options = {})
super
- @options = Options.with_defaults(options)
+ @options = DEFAULTS.merge(options)
@runner = Runner.new
+ @detect = Detect.new(@options[:output_paths])
end
def start
- UI.info 'Guard::Depend is running'
+ UI.info "#{self.class} is running"
run_all if @options[:run_on_start]
end
def stop
end
@@ -31,44 +40,9 @@
run_if_outdated(paths)
end
private
def run_if_outdated(paths = [])
- return false if paths.empty?
-
- outdated = out_of_date?(paths, @options[:output_paths])
- unless outdated
- UI.debug("Output is not outdated with regard to #{paths}")
- return
- end
-
- @runner.run(@options[:cmd])
- end
-
- def out_of_date?(input, output)
- output = output.call if output.respond_to?(:call)
-
- return true if input.nil? || input.empty? || output.nil? || output.empty?
-
- input = input.max_by {|f| input_mtime(f) }
- output = output.min_by {|f| output_mtime(f) }
-
- input_time = input_mtime(input)
- output_time = output_mtime(output)
-
- UI.debug("Newest input file: #{input_time} #{input}")
- UI.debug("Oldest output file: #{output_time} #{output}")
-
- return input_time > output_time
- end
-
- def input_mtime(file)
- return Time.new(9999, 12, 31) unless File.readable?(file)
- File.mtime(file)
- end
-
- def output_mtime(file)
- return Time.new(0, 1, 1) unless File.readable?(file)
- File.mtime(file)
+ @runner.run(@options[:cmd]) if detect.out_of_date?(paths)
end
end
end