Class: Xing::Edicts::LaunchBrowser

Inherits:
Edict::Command
  • Object
show all
Defined in:
lib/xing/edicts/launch-browser.rb

Instance Method Summary collapse

Constructor Details

#initializeLaunchBrowser

Returns a new instance of LaunchBrowser.



12
13
14
15
# File 'lib/xing/edicts/launch-browser.rb', line 12

def initialize
  self.command = ""
  super
end

Instance Method Details

#actionObject



85
86
87
88
89
# File 'lib/xing/edicts/launch-browser.rb', line 85

def action
  fork do
    subprocess_action
  end
end

#check_existing_browserObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/xing/edicts/launch-browser.rb', line 34

def check_existing_browser
  started = Time.now
  changes = {}
  while(Time.now - started < max_wait)
    begin
      changes = JSON.parse(Net::HTTP.get(URI("http://localhost:#{reload_server_port}/changed")))
    rescue Errno::ECONNREFUSED
      puts "LiveReload server abruptly stopped receiving connections. Bailing out."
      exit 2
    end

    if changes["clients"].empty?
      sleep 0.25
    else
      break
    end
  end
  changes["clients"]
end

#check_live_reload_server!Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/xing/edicts/launch-browser.rb', line 17

def check_live_reload_server!
  begin_time = Time.now
  begin
    test_conn = TCPSocket.new 'localhost', reload_server_port
  rescue Errno::ECONNREFUSED
    if Time.now - begin_time > setup_time_limit
      puts "Couldn't connect to test server on localhost:#{reload_server_port} after #{setup_time_limit} seconds - bailing out"
      exit 1
    else
      sleep 0.05
      retry
    end
  ensure
    test_conn.close rescue nil
  end
end

#launch_new_browserObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/xing/edicts/launch-browser.rb', line 54

def launch_new_browser
  puts
  puts "No running development browsers: launching...."

  browser_command = %w{open xdg-open chrome chromium}.find do |command|
    run_command(cmd('which', command)).succeeds?
  end

  if browser_command.nil?
    raise "Can't find any executable to launch a browser with."
  end

  run_command(cmd(browser_command, "http://localhost:#{static_server_port}/")).must_succeed!
end

#subprocess_actionObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/xing/edicts/launch-browser.rb', line 69

def subprocess_action
  require 'net/http'
  require 'json'

  check_live_reload_server!

  existing = check_existing_browser

  if existing.empty?
    launch_new_browser
  else
    puts
    puts "There's already a browser attached to the LiveReload server."
  end
end