Class: Kicker

Inherits:
Object
  • Object
show all
Includes:
Growl
Defined in:
lib/kicker.rb,
lib/kicker/growl.rb,
lib/kicker/utils.rb,
lib/kicker/options.rb,
lib/kicker/validate.rb,
lib/kicker/recipes/base.rb,
lib/kicker/recipes/rails.rb,
lib/kicker/callback_chain.rb

Defined Under Namespace

Modules: Recipes Classes: CallbackChain

Constant Summary collapse

GROWL_NOTIFICATIONS =
{
  :change => 'Change occured',
  :succeeded => 'Command succeeded',
  :failed => 'Command failed'
}
GROWL_DEFAULT_CALLBACK =
lambda do
  OSX::NSWorkspace.sharedWorkspace.launchApplication('Terminal')
end
OPTION_PARSER_CALLBACK =
lambda do |options|
  option_parser.on('--[no-]growl', 'Whether or not to use Growl. Default is to use growl.') do |growl|
    options[:growl] = growl
  end
  
  option_parser.on('--growl-command [COMMAND]', 'The command to execute when the Growl succeeded message is clicked.') do |command|
    options[:growl_command] = command
  end
  
  option_parser.on('-l', '--latency [FLOAT]', 'FSEvent grouping latency') do |latency|
    options[:latency] = Float(latency)
  end
  
  option_parser
end

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Kicker

Returns a new instance of Kicker.



33
34
35
36
37
38
39
40
41
# File 'lib/kicker.rb', line 33

def initialize(options)
  @paths = (options[:paths] ? options[:paths] : Kicker.paths).map { |path| File.expand_path(path) }
  
  @latency       = options[:latency] || self.class.latency
  @use_growl     = options[:growl]
  @growl_command = options[:growl_command]
  
  finished_processing!
end

Class Attribute Details

.latencyObject

Returns the value of attribute latency.



15
16
17
# File 'lib/kicker.rb', line 15

def latency
  @latency
end

Instance Attribute Details

#growl_commandObject

Returns the value of attribute growl_command.



6
7
8
# File 'lib/kicker/growl.rb', line 6

def growl_command
  @growl_command
end

#last_event_processed_atObject (readonly)

Returns the value of attribute last_event_processed_at.



31
32
33
# File 'lib/kicker.rb', line 31

def last_event_processed_at
  @last_event_processed_at
end

#latencyObject (readonly)

Returns the value of attribute latency.



31
32
33
# File 'lib/kicker.rb', line 31

def latency
  @latency
end

#pathsObject (readonly)

Returns the value of attribute paths.



31
32
33
# File 'lib/kicker.rb', line 31

def paths
  @paths
end

#use_growlObject

Returns the value of attribute use_growl.



6
7
8
# File 'lib/kicker/growl.rb', line 6

def use_growl
  @use_growl
end

Class Method Details

.full_chainObject



27
28
29
# File 'lib/kicker/callback_chain.rb', line 27

def full_chain
  @full_chain ||= CallbackChain.new([pre_process_chain, process_chain, post_process_chain])
end

.option_parserObject



4
5
6
7
8
# File 'lib/kicker/options.rb', line 4

def self.option_parser
  @option_parser ||= OptionParser.new do |opt|
    opt.banner = "Usage: #{$0} [options] [paths to watch]"
  end
end

.parse_options(argv) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/kicker/options.rb', line 26

def self.parse_options(argv)
  argv = argv.dup
  options = { :growl => true }
  OPTION_PARSER_CALLBACK.call(options).parse!(argv)
  options[:paths] = argv unless argv.empty?
  options
end

.pathsObject



21
22
23
# File 'lib/kicker.rb', line 21

def paths
  @paths ||= %w{ . }
end

.post_process_callback=(callback) ⇒ Object



39
40
41
# File 'lib/kicker/callback_chain.rb', line 39

def post_process_callback=(callback)
  post_process_chain.prepend_callback(callback)
end

.post_process_chainObject



23
24
25
# File 'lib/kicker/callback_chain.rb', line 23

def post_process_chain
  @post_process_chain ||= CallbackChain.new
end

.pre_process_callback=(callback) ⇒ Object



31
32
33
# File 'lib/kicker/callback_chain.rb', line 31

def pre_process_callback=(callback)
  pre_process_chain.append_callback(callback)
end

.pre_process_chainObject



15
16
17
# File 'lib/kicker/callback_chain.rb', line 15

def pre_process_chain
  @pre_process_chain ||= CallbackChain.new
end

.process_callback=(callback) ⇒ Object



35
36
37
# File 'lib/kicker/callback_chain.rb', line 35

def process_callback=(callback)
  process_chain.append_callback(callback)
end

.process_chainObject



19
20
21
# File 'lib/kicker/callback_chain.rb', line 19

def process_chain
  @process_chain ||= CallbackChain.new
end

.run(argv = ARGV) ⇒ Object



25
26
27
28
# File 'lib/kicker.rb', line 25

def run(argv = ARGV)
  load '.kick' if File.exist?('.kick')
  new(parse_options(argv)).start
end

Instance Method Details

#execute_command(command) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/kicker/utils.rb', line 2

def execute_command(command)
  log "Change occured, executing command: #{command}"
  growl(GROWL_NOTIFICATIONS[:change], 'Kicker: Change occured, executing command:', command) if @use_growl
  
  output = `#{command}`
  output.strip.split("\n").each { |line| log "  #{line}" }
  
  log "Command #{last_command_succeeded? ? 'succeeded' : "failed (#{last_command_status})"}"
  
  if @use_growl
    if last_command_succeeded?
      callback = @growl_command.nil? ? GROWL_DEFAULT_CALLBACK : lambda { system(@growl_command) }
      growl(GROWL_NOTIFICATIONS[:succeeded], "Kicker: Command succeeded", output, &callback)
    else
      growl(GROWL_NOTIFICATIONS[:failed], "Kicker: Command failed (#{last_command_status})", output, &GROWL_DEFAULT_CALLBACK)
    end
  end
end

#full_chainObject



56
57
58
# File 'lib/kicker/callback_chain.rb', line 56

def full_chain
  self.class.full_chain
end

#log(message) ⇒ Object



21
22
23
# File 'lib/kicker/utils.rb', line 21

def log(message)
  puts "[#{Time.now}] #{message}"
end

#post_process_chainObject



52
53
54
# File 'lib/kicker/callback_chain.rb', line 52

def post_process_chain
  self.class.post_process_chain
end

#pre_process_chainObject



44
45
46
# File 'lib/kicker/callback_chain.rb', line 44

def pre_process_chain
  self.class.pre_process_chain
end

#process_chainObject



48
49
50
# File 'lib/kicker/callback_chain.rb', line 48

def process_chain
  self.class.process_chain
end

#startObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kicker.rb', line 43

def start
  validate_options!
  
  log "Watching for changes on: #{@paths.join(', ')}"
  log ''
  
  run_watch_dog!
  start_growl! if @use_growl
  
  OSX.CFRunLoopRun
end