class Ergo::Runner
Runner
is the main class which controls execution.
Attributes
Returns [Hash]
Public Class Methods
Initialize new Session instance.
Returns nothing.
# File lib/ergo/runner.rb, line 13 def initialize(options={}) @script = options[:script] @system = options[:system] self.root = options[:root] self.trial = options[:trial] self.fresh = options[:fresh] self.watch = options[:watch] self.ignore = options[:ignore] @digests = {} end
Public Instance Methods
Set whether to nullify digest and make a fresh run.
Returns [Boolean]
# File lib/ergo/runner.rb, line 60 def fresh=(boolean) @fresh = !! boolean end
Nullify digest and make a fresh run?
Returns [Boolean]
# File lib/ergo/runner.rb, line 53 def fresh? @fresh end
Home directory.
Returns [String]
# File lib/ergo/runner.rb, line 121 def home @home ||= File.expand_path('~') end
File globs to ignore.
Returns [Ignore] instance.
# File lib/ergo/runner.rb, line 149 def ignore @ignore ||= Ignore.new(:root=>root) end
Set ignore.
# File lib/ergo/runner.rb, line 154 def ignore=(file) @ignore = Ignore.new(:root=>root, :file=>file) end
Locate project root. This method ascends up the file system starting as the current working directory looking for a ‘.ergo` directory. When found, the directory in which it is found is returned as the root. It is also memoized, so repeated calls to this method will not repeat the search.
Returns [String]
# File lib/ergo/runner.rb, line 90 def root dir = root? raise RootError, "cannot locate project root" unless dir dir end
Set the root directory.
Returns [String]
# File lib/ergo/runner.rb, line 114 def root=(dir) @root = dir if dir end
# File lib/ergo/runner.rb, line 97 def root? @root ||= ( r = nil d = Dir.pwd while d != home && d != '/' if File.directory?('.ergo') break r = d end d = File.dirname(d) end r ) end
List of rules from the system.
Returns [Array<Rule>]
# File lib/ergo/runner.rb, line 161 def rules system.rules end
Run rules.
Returns nothing.
# File lib/ergo/runner.rb, line 168 def run(*marks) raise ArgumentError, "invalid bookmark" unless marks.all?{ |m| /\w+/ =~ m } if watch autorun(*marks) else monorun(*marks) end end
Rules script to load.
Returns List of file paths. [Array]
# File lib/ergo/runner.rb, line 135 def script @script || (@system ? nil : Dir[RULES_SCRIPT].first) end
Instance of {Ergo::System}.
Returns [System]
# File lib/ergo/runner.rb, line 128 def system @system ||= System.new(script) end
Set trial run mode.
Arguments
bool - Flag for trial mode. [Boolean]
Returns ‘bool` flag. [Boolean]
# File lib/ergo/runner.rb, line 79 def trial=(bool) @trial = !!bool end
Is this trial-run only?
TODO: Trial mode is not implemented yet!
Returns [Boolean]
# File lib/ergo/runner.rb, line 69 def trial? @trial end
Watch period, default is every 5 minutes.
Returns [Fixnum]
# File lib/ergo/runner.rb, line 29 def watch @watch end
Set watch seconds. Minimum watch time is 1 second. Setting watch before calling run
creates a simple loop. It can eat up CPU cycles so use it wisely. A watch time of 4 seconds is a good time period. If you are patient go for 15 seconds or more.
Returns [Fixnum,nil]
# File lib/ergo/runner.rb, line 40 def watch=(seconds) if seconds seconds = seconds.to_i seconds = 1 if seconds < 1 @watch = seconds else @watch = nil end end
Private Instance Methods
Run rules periodically.
Returns nothing.
# File lib/ergo/runner.rb, line 198 def autorun(*marks) Dir.chdir(root) do fresh_digest(*marks) if fresh? trap("INT") { puts "\nPutting out the fire!"; exit } puts "Fire started! (pid #{Process.pid})" if marks.size > 0 loop do run_bookmarks(*marks) sleep(watch) end else loop do run_rules sleep(watch) end end end end
Clear away all digests but the main digest.
Returns nothing.
# File lib/ergo/runner.rb, line 298 def clear_digests Digest.clear_digests @digests = {} end
get digest by name, if it doesn’t exit create a new one.
# File lib/ergo/runner.rb, line 269 def digest(name=nil) @digests[name] ||= Digest.new(:ignore=>ignore, :name=>name) end
Start with a clean slate by remove the digest.
Returns nothing.
# File lib/ergo/runner.rb, line 284 def fresh_digest(*marks) if marks.empty? clear_digests else marks.each do |mark| d = @digests.delete(mark) d.remove if d end end end
Get the most recent digest for a given rule.
Returns [Digest]
# File lib/ergo/runner.rb, line 276 def latest_digest(rule) name = Digest.latest(*rule.bookmarks) digest(name) end
Run rules once.
Returns nothing.
# File lib/ergo/runner.rb, line 183 def monorun(*marks) Dir.chdir(root) do fresh_digest(*marks) if fresh? if marks.size > 0 run_bookmarks(*marks) else run_rules end end end
Run only those rules with a specific bookmark.
marks - Bookmark names. [Array<String>].
Returns nothing.
# File lib/ergo/runner.rb, line 250 def run_bookmarks(*marks) system.rules.each do |rule| case rule when Book book = rule book.rules.each do |rule| next unless marks.any?{ |mark| rule.mark?(mark) } rule.apply(latest_digest(rule)) end else next unless marks.any?{ |mark| rule.mark?(mark) } rule.apply(latest_digest(rule)) end end save_digests(*marks) end
Run all rules (expect private rules).
Returns nothing.
# File lib/ergo/runner.rb, line 225 def run_rules system.rules.each do |rule| case rule when Book book = rule book.rules.each do |rule| next if rule.private? rule.apply(latest_digest(rule)) end else next if rule.private? rule.apply(latest_digest(rule)) end end clear_digests digest.save end
Save digests for given bookmarks.
Returns nothing.
# File lib/ergo/runner.rb, line 306 def save_digests(*bookmarks) bookmarks.each do |mark| digest(mark).save end end