Class: Fire::System

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

Overview

System stores states and rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

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(options={})
  extend self
  extend ShellUtils

  @ignore  = Array(options[:ignore] || [])
  @files   = Array(options[: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

#digestObject (readonly)

File digest.



60
61
62
# File 'lib/fire/system.rb', line 60

def digest
  @digest
end

#rulesObject (readonly)

Array of defined rules.



54
55
56
# File 'lib/fire/system.rb', line 54

def rules
  @rules
end

#sessionObject (readonly)

Current session.



48
49
50
# File 'lib/fire/system.rb', line 48

def session
  @session
end

#statesObject (readonly)

Array of defined states.



51
52
53
# File 'lib/fire/system.rb', line 51

def states
  @states
end

#tasksObject (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.

Examples:

env('PATH'=>/foo/)

Returns:



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.

Returns:



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, options={}, &procedure)
  state = FileState.new(pattern, digest, ignore)
  @rules << Rule.new(state, options, &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(message, options={})
  title = options.delete(:title) || 'Fire Notification'
  Notify.notify(title, message.to_s, options)
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.

Returns:

  • (Array)

    Returns key and value.



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.

Examples:

rule no_rdocs do |files|
  sh "rdoc --output doc/rdoc " + files.join(" ")
end


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

Returns:

  • (nil)

    Returns nil if state name is given.

  • (State)

    Returns State in no name is given.



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.

Returns:



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

Returns:

  • (Task)

    Returns



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