Class: Fire::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/fire/session.rb

Overview

TODO:

Maybee call this Runner, and have a special Session class that limits interface.

Session is the main Fire class which controls execution.

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ void

Initialize new Session instance.



25
26
27
28
29
30
# File 'lib/fire/session.rb', line 25

def initialize(options={})
  self.watch = options[:watch]
  self.trial = options[:trial]

  load_system
end

Instance Method Details

#autorun(argv) ⇒ Object

Run periodically.



138
139
140
141
142
143
144
145
146
147
# File 'lib/fire/session.rb', line 138

def autorun(argv)
  Dir.chdir(root) do
    trap("INT") { puts "\nPutting out the fire!"; exit }
    puts "Fire started! (pid #{Process.pid})"
    loop do
      run_rules
      sleep(watch)
    end
  end
end

#homeString (private)

Home directory.

Returns:

  • (String)

    Returns



250
251
252
# File 'lib/fire/session.rb', line 250

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

#ignoreObject

File globs to ignore.

@return [Array] List of file globs.



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/fire/session.rb', line 112

def ignore
  @ignore ||= (
    f = File.join(root, IGNORE_FILE)
    i = []
    if File.exist?(f)
      File.read(f).lines.each do |line|
        glob = line.strip
        i << glob unless glob.empty?
      end
    end
    i
  )
end

#load_systemObject (private)



152
153
154
155
# File 'lib/fire/session.rb', line 152

def load_system
  system.ignore(*ignore)
  system.import(*script)
end

#rootString (private)

Locate project root. This method ascends up the file system starting as the current working directory looking for ‘ROOT_INDICATORS`. When a match is 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



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/fire/session.rb', line 232

def root
  @root ||= (
    r = nil
    d = Dir.pwd
    while d != home && d != '/'
      if ROOT_INDICATORS.any?{ |g| Dir.glob(File.join(d, g), File::FNM_CASEFOLD).first }
        break r = d
      end
      d = File.dirname(d)
    end
    abort "Can't locate project root." unless r
    r
  )
end

#rulesObject (private)

List of rules from the system.



203
204
205
# File 'lib/fire/session.rb', line 203

def rules
  system.rules
end

#run(argv) ⇒ Object

Run once.



127
128
129
130
131
132
133
134
135
# File 'lib/fire/session.rb', line 127

def run(argv)
  Dir.chdir(root) do
    if argv.size > 0
      run_task(*argv)
    else
      run_rules
    end
  end
end

#run_rulesObject (private)

Run the rules.



158
159
160
161
# File 'lib/fire/session.rb', line 158

def run_rules
  runner.run_rules
  save_digest
end

#run_task(*argv) ⇒ Object (private)

Run the rules.



164
165
166
# File 'lib/fire/session.rb', line 164

def run_task(*argv)
  runner.run_task(*argv)
end

#runnerObject (private)



182
183
184
# File 'lib/fire/session.rb', line 182

def runner
  Runner.new(system)
end

#save_digestObject (private)



176
177
178
179
# File 'lib/fire/session.rb', line 176

def save_digest
  digest = Digest.new(:ignore=>ignore)
  digest.save
end

#scriptObject Also known as: scripts

Default rules script is the first file matching ‘.fire/rules.rb` or `rules.rb` from the the project’s root directory. The easiest way to customize this is to use ‘.fire/rules.rb` and use `import` to load which ever files you prefer, e.g. `import “task/*.fire”`.

@return [Array] List of file paths.



91
92
93
94
95
96
# File 'lib/fire/session.rb', line 91

def script
  @script ||= (
    glob = File.join(root, '{.fire/rules.rb,rules.rb}')
    Dir.glob(glob, File::FNM_CASEFOLD).first
  )
end

#script=(files) ⇒ Object Also known as: scripts=

Change this rules script(s) that are loaded.



102
103
104
# File 'lib/fire/session.rb', line 102

def script=(files)
  @script = Array(files)
end

#systemObject

Instance of Fire::System.



72
73
74
75
# File 'lib/fire/session.rb', line 72

def system
  #@system ||= System.new(:ignore=>ignore, :files=>scripts)
  @system ||= Fire.system
end

#task_sheetString (private)

Produce a printable list of tasks that can run from the command line.

Returns:

  • (String)

    Returns



215
216
217
218
219
220
221
222
223
# File 'lib/fire/session.rb', line 215

def task_sheet
  max    = tasks.map{|n,t| n.to_s.size}.max.to_i
  layout = "ou %-#{max}s  # %s"
  text   = []
  tasks.each do |name, task|
    text << layout % [name, task.description] if task.description
  end
  text.join("\n")
end

#tasksObject (private)

Mapping of tasks from the system.



208
209
210
# File 'lib/fire/session.rb', line 208

def tasks
  system.tasks
end

#trial=(bool) ⇒ Boolean

Set trial run mode.

Parameters:

  • bool

    Flag for trial mode. [Boolean]

Returns:

  • (Boolean)

    Returns ‘bool` flag.



67
68
69
# File 'lib/fire/session.rb', line 67

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

#trial?Boolean

Is this trial-run only?

Returns:



57
58
59
# File 'lib/fire/session.rb', line 57

def trial?
  @trial
end

#watchObject

Watch period, default is every 5 minutes.



35
36
37
# File 'lib/fire/session.rb', line 35

def watch
  @watch || 300
end

#watch=(seconds) ⇒ Object

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.



46
47
48
49
50
51
52
53
54
# File 'lib/fire/session.rb', line 46

def watch=(seconds)
  if seconds
    seconds = seconds.to_i
    seconds = 1 if seconds < 1
    @watch = seconds
  else
    @watch = nil 
  end
end