Class: Fire::System
- Inherits:
-
Module
- Object
- Module
- Fire::System
- Defined in:
- lib/fire/system.rb
Overview
System stores states and rules.
Instance Attribute Summary collapse
-
#digest ⇒ Object
readonly
File digest.
-
#rules ⇒ Object
readonly
Array of defined rules.
-
#session ⇒ Object
readonly
Current session.
-
#states ⇒ Object
readonly
Array of defined states.
-
#tasks ⇒ Object
readonly
Mapping of defined tasks.
Instance Method Summary collapse
-
#desc(description) ⇒ Object
Set task description.
-
#env(name_to_pattern) ⇒ State
Define an environment state.
-
#file(pattern) ⇒ FileState
Define a file state.
-
#file_rule(pattern, options = {}, &procedure) ⇒ void
private
Define a file rule.
-
#ignore(*globs) ⇒ Object
Add paths to be ignored in file rules.
-
#import(*globs) ⇒ Object
Import from another file, or glob of files, relative to project root.
-
#initialize(options = {}) ⇒ System
constructor
Instantiate new system.
-
#notify(message, options = {}) ⇒ Object
Issue notification.
-
#parse_arrow(argument) ⇒ Array
private
Split a hash argument into it’s key and value pair.
-
#rule(state, &procedure) ⇒ Object
Define a rule.
-
#run(task_name) ⇒ Object
Run a task.
-
#state(name = nil, &condition) ⇒ nil, State
Define a named state.
-
#state?(name, *args) ⇒ Boolean
Check a name state.
-
#task(name_and_state, &procedure) ⇒ Task
Define a command line task.
Constructor Details
#initialize(options = {}) ⇒ System
Instantiate new system.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/fire/system.rb', line 28 def initialize(={}) extend self extend ShellUtils @ignore = Array([:ignore] || []) @files = Array([:files] || []) @rules = [] @states = {} @tasks = {} @digest = Digest.new @session = OpenStruct.new @files.each do |file| module_eval(File.read(file), file) end end |
Instance Attribute Details
#digest ⇒ Object (readonly)
File digest.
60 61 62 |
# File 'lib/fire/system.rb', line 60 def digest @digest end |
#rules ⇒ Object (readonly)
Array of defined rules.
54 55 56 |
# File 'lib/fire/system.rb', line 54 def rules @rules end |
#session ⇒ Object (readonly)
Current session.
48 49 50 |
# File 'lib/fire/system.rb', line 48 def session @session end |
#states ⇒ Object (readonly)
Array of defined states.
51 52 53 |
# File 'lib/fire/system.rb', line 51 def states @states end |
#tasks ⇒ Object (readonly)
Mapping of defined tasks.
57 58 59 |
# File 'lib/fire/system.rb', line 57 def tasks @tasks end |
Instance Method Details
#desc(description) ⇒ Object
Set task description. The next task defined will get the most recently defined description attached to it.
174 175 176 |
# File 'lib/fire/system.rb', line 174 def desc(description) @_desc = description end |
#env(name_to_pattern) ⇒ State
Define an environment state.
128 129 130 131 132 133 134 |
# File 'lib/fire/system.rb', line 128 def env(name_to_pattern) State.new do name_to_pattern.any? do |name, re| re === ENV[name.to_s] # or `all?` instead? end end end |
#file(pattern) ⇒ FileState
Define a file state.
118 119 120 |
# File 'lib/fire/system.rb', line 118 def file(pattern) FileState.new(pattern, digest, ignore) end |
#file_rule(pattern, options = {}, &procedure) ⇒ void (private)
This method returns an undefined value.
Define a file rule. A file rule is a rule with a specific state based on changes in files.
@example
file_rule 'test/**/case_*.rb' do |files|
sh "ruby-test " + files.join(" ")
end
237 238 239 240 |
# File 'lib/fire/system.rb', line 237 def file_rule(pattern, ={}, &procedure) state = FileState.new(pattern, digest, ignore) @rules << Rule.new(state, , &procedure) end |
#ignore(*globs) ⇒ Object
Add paths to be ignored in file rules.
81 82 83 84 |
# File 'lib/fire/system.rb', line 81 def ignore(*globs) @ignore.concat(globs.flatten) @ignore end |
#import(*globs) ⇒ Object
Import from another file, or glob of files, relative to project root.
@todo Should importing be relative the importing file? @return nothing
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/fire/system.rb', line 66 def import(*globs) globs.each do |glob| #if File.relative?(glob) # dir = Dir.pwd #session.root #File.dirname(caller[0]) # glob = File.join(dir, glob) #end Dir[glob].each do |file| next unless File.file?(file) #instance_eval(File.read(file), file) module_eval(File.read(file), file) end end end |
#notify(message, options = {}) ⇒ Object
Issue notification.
201 202 203 204 |
# File 'lib/fire/system.rb', line 201 def notify(, ={}) title = .delete(:title) || 'Fire Notification' Notify.notify(title, .to_s, ) end |
#parse_arrow(argument) ⇒ Array (private)
Split a hash argument into it’s key and value pair. The hash is expected to have only one entry. If the argument is not a hash then returns the argument and an empty array.
215 216 217 218 219 220 221 222 223 224 |
# File 'lib/fire/system.rb', line 215 def parse_arrow(argument) case argument when Hash raise ArgumentError if argument.size > 1 head, tail = *argument.to_a.first return head, Array(tail) else return argument, [] end end |
#rule(state, &procedure) ⇒ Object
Define a rule. Rules are procedures that are tiggered by logical states.
144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/fire/system.rb', line 144 def rule(state, &procedure) state, todo = parse_arrow(state) case state when String, Regexp file_rule(state, :todo=>todo, &procedure) when Symbol # TODO: Is this really the best idea? #@states[state.to_sym] else @rules << Rule.new(state, :todo=>todo, &procedure) end end |
#run(task_name) ⇒ Object
Run a task.
168 169 170 |
# File 'lib/fire/system.rb', line 168 def run(task_name) #, *args) tasks[task_name.to_sym].invoke #call(*args) end |
#state(name = nil, &condition) ⇒ nil, State
Define a named state. States define conditions that are used to trigger rules. Named states are kept in a hash table to ensure that only one state is ever defined for a given name. Calling state again with the same name as a previously defined state will redefine the condition of that state.
@example
state :no_rdocs? do
files = Dir.glob('lib/**/*.rb')
FileUtils.uptodate?('doc', files) ? files : false
end
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/fire/system.rb', line 99 def state(name=nil, &condition) if name if condition @states[name.to_sym] = condition define_method(name) do |*args| state = @states[name.to_sym] State.new{ states[name.to_sym].call(*args) } end else raise ArgumentError end else State.new{ condition.call(*args) } end end |
#state?(name, *args) ⇒ Boolean
Check a name state.
161 162 163 |
# File 'lib/fire/system.rb', line 161 def state?(name, *args) @states[name.to_sym].call(*args) end |
#task(name_and_state, &procedure) ⇒ Task
Define a command line task. A task is special type of rule that is triggered when the command line tool is invoked with the name of the task.
Tasks are an isolated set of rules and suppress the activation of all other rules not specifically given as prerequisites.
task :rdoc do
trip no_rdocs
end
190 191 192 193 194 195 196 |
# File 'lib/fire/system.rb', line 190 def task(name_and_state, &procedure) name, todo = parse_arrow(name_and_state) task = Task.new(name, :todo=>todo, :desc=>@_desc, &procedure) @tasks[name.to_sym] = task @_desc = nil task end |