Class: Ergo::Runner
- Inherits:
-
Object
- Object
- Ergo::Runner
- Defined in:
- lib/ergo/runner.rb
Overview
Runner is the main class which controls execution.
Defined Under Namespace
Classes: RootError
Instance Attribute Summary collapse
-
#digests ⇒ Hash
readonly
private
Returns [Hash].
Instance Method Summary collapse
-
#autorun(*marks) ⇒ void
private
Run rules periodically.
-
#clear_digests ⇒ void
private
Clear away all digests but the main digest.
-
#digest(name = nil) ⇒ Object
private
get digest by name, if it doesn’t exit create a new one.
-
#fresh=(boolean) ⇒ Boolean
Set whether to nullify digest and make a fresh run.
-
#fresh? ⇒ Boolean
Nullify digest and make a fresh run?.
-
#fresh_digest(*marks) ⇒ void
private
Start with a clean slate by remove the digest.
-
#home ⇒ String
Home directory.
-
#ignore ⇒ Ignore
File globs to ignore.
-
#ignore=(file) ⇒ Object
Set ignore.
-
#initialize(options = {}) ⇒ void
constructor
Initialize new Session instance.
-
#latest_digest(rule) ⇒ Digest
private
Get the most recent digest for a given rule.
-
#monorun(*marks) ⇒ void
private
Run rules once.
-
#root ⇒ String
Locate project root.
-
#root=(dir) ⇒ String
Set the root directory.
- #root? ⇒ Boolean
-
#rules ⇒ Array<Rule>
List of rules from the system.
-
#run(*marks) ⇒ void
Run rules.
-
#run_bookmarks(*marks) ⇒ void
private
Run only those rules with a specific bookmark.
-
#run_rules ⇒ void
private
Run all rules (expect private rules).
-
#save_digests(*bookmarks) ⇒ void
private
Save digests for given bookmarks.
-
#script ⇒ Array
Rules script to load.
-
#system ⇒ System
Instance of System.
-
#trial=(bool) ⇒ Boolean
Set trial run mode.
-
#trial? ⇒ Boolean
Is this trial-run only?.
-
#watch ⇒ Fixnum
Watch period, default is every 5 minutes.
-
#watch=(seconds) ⇒ Fixnum?
Set watch seconds.
Constructor Details
#initialize(options = {}) ⇒ void
Initialize new Session instance.
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/ergo/runner.rb', line 13 def initialize(={}) @script = [:script] @system = [:system] self.root = [:root] self.trial = [:trial] self.fresh = [:fresh] self.watch = [:watch] self.ignore = [:ignore] @digests = {} end |
Instance Attribute Details
#digests ⇒ Hash (readonly, private)
Returns [Hash]
220 221 222 |
# File 'lib/ergo/runner.rb', line 220 def digests @digests end |
Instance Method Details
#autorun(*marks) ⇒ void (private)
This method returns an undefined value.
Run rules periodically.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# 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_digests ⇒ void (private)
This method returns an undefined value.
Clear away all digests but the main digest.
298 299 300 301 |
# File 'lib/ergo/runner.rb', line 298 def clear_digests Digest.clear_digests @digests = {} end |
#digest(name = nil) ⇒ Object (private)
get digest by name, if it doesn’t exit create a new one.
269 270 271 |
# File 'lib/ergo/runner.rb', line 269 def digest(name=nil) @digests[name] ||= Digest.new(:ignore=>ignore, :name=>name) end |
#fresh=(boolean) ⇒ Boolean
Set whether to nullify digest and make a fresh run.
60 61 62 |
# File 'lib/ergo/runner.rb', line 60 def fresh=(boolean) @fresh = !! boolean end |
#fresh? ⇒ Boolean
Nullify digest and make a fresh run?
53 54 55 |
# File 'lib/ergo/runner.rb', line 53 def fresh? @fresh end |
#fresh_digest(*marks) ⇒ void (private)
This method returns an undefined value.
Start with a clean slate by remove the digest.
284 285 286 287 288 289 290 291 292 293 |
# 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 |
#home ⇒ String
Home directory.
121 122 123 |
# File 'lib/ergo/runner.rb', line 121 def home @home ||= File.('~') end |
#ignore ⇒ Ignore
File globs to ignore.
149 150 151 |
# File 'lib/ergo/runner.rb', line 149 def ignore @ignore ||= Ignore.new(:root=>root) end |
#ignore=(file) ⇒ Object
Set ignore.
154 155 156 |
# File 'lib/ergo/runner.rb', line 154 def ignore=(file) @ignore = Ignore.new(:root=>root, :file=>file) end |
#latest_digest(rule) ⇒ Digest (private)
Get the most recent digest for a given rule.
276 277 278 279 |
# File 'lib/ergo/runner.rb', line 276 def latest_digest(rule) name = Digest.latest(*rule.bookmarks) digest(name) end |
#monorun(*marks) ⇒ void (private)
This method returns an undefined value.
Run rules once.
183 184 185 186 187 188 189 190 191 192 193 |
# 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 |
#root ⇒ String
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.
90 91 92 93 94 |
# File 'lib/ergo/runner.rb', line 90 def root dir = root? raise RootError, "cannot locate project root" unless dir dir end |
#root=(dir) ⇒ String
Set the root directory.
114 115 116 |
# File 'lib/ergo/runner.rb', line 114 def root=(dir) @root = dir if dir end |
#root? ⇒ Boolean
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# 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 |
#rules ⇒ Array<Rule>
List of rules from the system.
161 162 163 |
# File 'lib/ergo/runner.rb', line 161 def rules system.rules end |
#run(*marks) ⇒ void
This method returns an undefined value.
Run rules.
168 169 170 171 172 173 174 175 176 |
# 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 |
#run_bookmarks(*marks) ⇒ void (private)
This method returns an undefined value.
Run only those rules with a specific bookmark.
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# 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_rules ⇒ void (private)
This method returns an undefined value.
Run all rules (expect private rules).
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# 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(*bookmarks) ⇒ void (private)
This method returns an undefined value.
Save digests for given bookmarks.
306 307 308 309 310 |
# File 'lib/ergo/runner.rb', line 306 def save_digests(*bookmarks) bookmarks.each do |mark| digest(mark).save end end |
#script ⇒ Array
Rules script to load.
135 136 137 |
# File 'lib/ergo/runner.rb', line 135 def script @script || (@system ? nil : Dir[RULES_SCRIPT].first) end |
#system ⇒ System
Instance of System.
128 129 130 |
# File 'lib/ergo/runner.rb', line 128 def system @system ||= System.new(script) end |
#trial=(bool) ⇒ Boolean
Set trial run mode.
79 80 81 |
# File 'lib/ergo/runner.rb', line 79 def trial=(bool) @trial = !!bool end |
#trial? ⇒ Boolean
Trial mode is not implemented yet!
Is this trial-run only?
69 70 71 |
# File 'lib/ergo/runner.rb', line 69 def trial? @trial end |
#watch ⇒ Fixnum
Watch period, default is every 5 minutes.
29 30 31 |
# File 'lib/ergo/runner.rb', line 29 def watch @watch end |
#watch=(seconds) ⇒ Fixnum?
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.
40 41 42 43 44 45 46 47 48 |
# 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 |