Skip to content

Commit

Permalink
[#42] Option to open web browser, [#38] Don't use which -s
Browse files Browse the repository at this point in the history
  • Loading branch information
sj26 committed Apr 6, 2012
1 parent 73562c7 commit 20b5635
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions lib/mail_catcher.rb
Expand Up @@ -15,6 +15,10 @@ module MailCatcher

module_function

def which command
IO.popen(["which", command], "r", :err => :close).read.chomp.presence
end

def mac?
RbConfig::CONFIG['host_os'] =~ /darwin/
end
Expand All @@ -28,17 +32,23 @@ def macruby?
end

def growlnotify?
system "which", "-s", "growlnotify"
which "growlnotify"
end

def growlframework?
macruby? and
# TODO: Look for growl framework accessible
false
def growl?
growlnotify?
end

def growl?
growlnotify? or growlframework?
def browse?
windows? or which "open"
end

def browse url
if windows?
system "start", "/b", url
elsif which "open"
system "open", url
end
end

@@defaults = {
Expand All @@ -49,6 +59,7 @@ def growl?
:verbose => false,
:daemon => !windows?,
:growl => growlnotify?,
:browse => false,
}

def parse! arguments=ARGV, defaults=@@defaults
Expand Down Expand Up @@ -96,6 +107,12 @@ def parse! arguments=ARGV, defaults=@@defaults
end
end

if browse?
parser.on('-b', '--browse', 'Open web browser') do
options[:browse] = true
end
end

parser.on('-v', '--verbose', 'Be more verbose') do
options[:verbose] = true
end
Expand Down Expand Up @@ -123,19 +140,27 @@ def run! options=nil
# Get our lion on if asked
MailCatcher::Growl.start if options[:growl]

# TODO: DRY this up
smtp_url = "smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"
http_url = "http://#{options[:http_ip]}:#{options[:http_port]}"

# Set up an SMTP server to run within EventMachine
rescue_port options[:smtp_port] do
EventMachine.start_server options[:smtp_ip], options[:smtp_port], Smtp
puts "==> smtp://#{options[:smtp_ip]}:#{options[:smtp_port]}"
puts "==> #{smtp_url}"
end

# Let Thin set itself up inside our EventMachine loop
# (Skinny/WebSockets just works on the inside)
rescue_port options[:http_port] do
Thin::Server.start options[:http_ip], options[:http_port], Web
puts "==> http://#{options[:http_ip]}:#{options[:http_port]}"
puts "==> #{http_url}"
end

# Open the web browser before detatching console
if options[:browse]
EventMachine.next_tick do
browse http_url
end
end

# Daemonize, if we should, but only after the servers have started.
Expand Down

0 comments on commit 20b5635

Please sign in to comment.