class Fire::Session
Session
is the main Fire
class which controls execution.
TODO: Maybee call this Runner
, and have a special Session
class that limits interface.
Public Class Methods
Initialize new Session
instance.
Returns nothing.
# File lib/fire/session.rb, line 25 def initialize(options={}) self.watch = options[:watch] self.trial = options[:trial] load_system end
Public Instance Methods
Run periodically.
# 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
File globs to ignore.
@return [Array] List of file globs.
# 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
Run once.
# 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
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.
# 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
Change this rules script(s) that are loaded.
# File lib/fire/session.rb, line 102 def script=(files) @script = Array(files) end
Instance of {Fire::System}.
# File lib/fire/session.rb, line 72 def system #@system ||= System.new(:ignore=>ignore, :files=>scripts) @system ||= Fire.system end
Set trial run mode.
Arguments
bool - Flag for trial mode. [Boolean]
Returns ‘bool` flag. [Boolean]
# File lib/fire/session.rb, line 67 def trial=(bool) @trial = !!bool end
Is this trial-run only?
# File lib/fire/session.rb, line 57 def trial? @trial end
Watch period, default is every 5 minutes.
# File lib/fire/session.rb, line 35 def watch @watch || 300 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.
# 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
Private Instance Methods
Home directory.
Returns [String]
# File lib/fire/session.rb, line 250 def home @home ||= File.expand_path('~') end
# File lib/fire/session.rb, line 152 def load_system system.ignore(*ignore) system.import(*script) end
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]
# 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
List of rules from the system.
# File lib/fire/session.rb, line 203 def rules system.rules end
Run the rules.
# File lib/fire/session.rb, line 158 def run_rules runner.run_rules save_digest end
Run the rules.
# File lib/fire/session.rb, line 164 def run_task(*argv) runner.run_task(*argv) end
# File lib/fire/session.rb, line 182 def runner Runner.new(system) end
# File lib/fire/session.rb, line 176 def save_digest digest = Digest.new(:ignore=>ignore) digest.save end
Produce a printable list of tasks that can run from the command line.
Returns [String]
# 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
Mapping of tasks from the system.
# File lib/fire/session.rb, line 208 def tasks system.tasks end