Class: Polygon::Script::Open

Inherits:
Object
  • Object
show all
Defined in:
lib/polygon/script/open.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#bundleObject

Returns the value of attribute bundle.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def bundle
  @bundle
end

#maxObject

Returns the value of attribute max.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def max
  @max
end

#modeObject

Returns the value of attribute mode.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def mode
  @mode
end

#open_browserObject

Returns the value of attribute open_browser.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def open_browser
  @open_browser
end

#refresh_repoObject

Returns the value of attribute refresh_repo.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def refresh_repo
  @refresh_repo
end

#sync_repoObject

Returns the value of attribute sync_repo.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def sync_repo
  @sync_repo
end

#tryObject

Returns the value of attribute try.



5
6
7
# File 'lib/polygon/script/open.rb', line 5

def try
  @try
end

Instance Method Details

#config_ruObject



7
8
9
# File 'lib/polygon/script/open.rb', line 7

def config_ru
  root/:config/"#{mode}.ru"
end

#execute(argv) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/polygon/script/open.rb', line 53

def execute(argv)
  abort options.to_s unless argv.size <= 1
  self.mode = (argv.first || "development").to_sym
  abort "Invalid mode #{mode}" unless config_ru.exist?

  info "Refreshing repository info..." do
    Process.wait spawn("git remote update")
    Process.wait spawn("git fetch origin")
  end if refresh_repo

  info "Syncing repository..." do
    Process.wait spawn("git pull origin master")
  end if sync_repo

  info "Bundling..." do
    Process.wait spawn("bundle")
  end if bundle

  thinpid = nil
  info "Starting the web server..." do
    thinpid = spawn("thin -R \"#{config_ru}\" start")
  end

  info "Waiting for the server to start" do
    require 'launchy'
    require 'http'
    wait_and_open
  end if open_browser

rescue => ex
  info "Lauching failed: #{ex.message}"
  info ex.backtrace.join("\n")
  Process.kill("SIGHUP", thinpid) if thinpid
ensure
  Process.wait(thinpid) if thinpid
  flush_logs
end

#wait_and_openObject

Tries to access the website



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/polygon/script/open.rb', line 41

def wait_and_open
  info "Attempting to connect to the web site..."
  Http.head "http://127.0.0.1:3000/"
rescue Errno::ECONNREFUSED
  sleep(0.5)
  retry if (self.try += 1) < max
  info "Server not found, sorry."
  raise
else
  Launchy.open("http://127.0.0.1:3000/")
end