Class: BrowserStakeout::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/browser_stakeout/application.rb

Class Method Summary collapse

Class Method Details

.build_mtimes_hash(arguments) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/browser_stakeout/application.rb', line 53

def build_mtimes_hash(arguments)
  files = {}
  arguments.each do |a|
    Dir[a].each do |file|
      files[file] = File.mtime(file)
    end
  end
  files
end

.run!(*arguments) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/browser_stakeout/application.rb', line 4

def run!(*arguments)
  options = BrowserStakeout::Options.new(arguments)
  
  if options[:invalid_argument]
    $stderr.puts "Invalid argument: #{options[:invalid_argument]}"
    options[:show_help] = true
  end

  if options[:show_help]
    $stderr.puts options.opts
    return 1
  end
  
  unless options.files.size >= 1
    $stderr.puts "broken"
    $stderr.puts options.opts
    return 1
  end

  files = build_mtimes_hash(options.files)

  trap('INT') do
    puts "\nIt's quittin time..."
    exit
  end

  # Check to see if the file being refreshed is on the files list.
  index_file = options.find_index(files)

  puts %{#{File.basename($0)} is reporting for duty! 
  Currently watching #{files.collect {|k,v| File.basename(k)}.join(', ')}\n\n}

  loop do
    sleep 1
  
    changed_file, last_changed = files.find do |file, last_changed|
      File.mtime(file) > last_changed
    end
  
    if changed_file
      files[changed_file] = File.mtime(changed_file)
      puts "#{Time.now} => #{File.basename(changed_file)} changed"
      options.browsers.each do |b|
        system b.refresh(index_file||changed_file)
      end
    end
  end
end