Class: Ergo::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/ergo/runner.rb

Overview

Runner is the main class which controls execution.

Defined Under Namespace

Classes: RootError

Instance Attribute Summary collapse

Instance Method Summary collapse

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(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

Instance Attribute Details

#digestsHash (readonly, private)

Returns [Hash]

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_digestsvoid (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.

Returns:



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?

Returns:



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

#homeString

Home directory.

Returns:

  • (String)

    Returns



121
122
123
# File 'lib/ergo/runner.rb', line 121

def home
  @home ||= File.expand_path('~')
end

#ignoreIgnore

File globs to ignore.

Returns:

  • (Ignore)

    Returns Ignore instance.



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.

Returns:



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

#rootString

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)

    Returns

Raises:



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.

Returns:

  • (String)

    Returns



114
115
116
# File 'lib/ergo/runner.rb', line 114

def root=(dir)
  @root = dir if dir
end

#root?Boolean

Returns:



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

#rulesArray<Rule>

List of rules from the system.

Returns:



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.

Raises:

  • (ArgumentError)


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.

Parameters:

  • marks

    Bookmark names. [Array<String>].



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_rulesvoid (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

#scriptArray

Rules script to load.

Returns:

  • (Array)

    Returns List of file paths.



135
136
137
# File 'lib/ergo/runner.rb', line 135

def script
  @script || (@system ? nil : Dir[RULES_SCRIPT].first)
end

#systemSystem

Instance of System.

Returns:



128
129
130
# File 'lib/ergo/runner.rb', line 128

def system
  @system ||= System.new(script)
end

#trial=(bool) ⇒ Boolean

Set trial run mode.

Parameters:

  • bool

    Flag for trial mode. [Boolean]

Returns:

  • (Boolean)

    Returns ‘bool` flag.



79
80
81
# File 'lib/ergo/runner.rb', line 79

def trial=(bool)
  @trial = !!bool
end

#trial?Boolean

TODO:

Trial mode is not implemented yet!

Is this trial-run only?

Returns:



69
70
71
# File 'lib/ergo/runner.rb', line 69

def trial?
  @trial
end

#watchFixnum

Watch period, default is every 5 minutes.

Returns:

  • (Fixnum)

    Returns



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.

Returns:

  • (Fixnum, nil)

    Returns



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