Skip to content

Commit

Permalink
added gem server --launch option to launch a browser pointing at gem …
Browse files Browse the repository at this point in the history
…server
  • Loading branch information
vertiginous committed Dec 5, 2010
1 parent b7db394 commit 28822b5
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions lib/rubygems/commands/server_command.rb
Expand Up @@ -43,6 +43,10 @@ def initialize
options[:addresses] ||= []
options[:addresses].push(*address)
end

add_option '--launch', 'launch a browser window' do |launch, options|
options[:launch] = launch
end
end

def defaults_str # :nodoc:
Expand Down
16 changes: 14 additions & 2 deletions lib/rubygems/server.rb
Expand Up @@ -429,18 +429,19 @@ class Gem::Server

def self.run(options)
new(options[:gemdir], options[:port], options[:daemon],
options[:addresses]).run
options[:launch],options[:addresses]).run
end

##
# Only the first directory in gem_dirs is used for serving gems

def initialize(gem_dirs, port, daemon, addresses = nil)
def initialize(gem_dirs, port, daemon, launch, addresses = nil)
Socket.do_not_reverse_lookup = true

@gem_dirs = Array gem_dirs
@port = port
@daemon = daemon
@launch = launch
@addresses = addresses
logger = WEBrick::Log.new nil, WEBrick::BasicLog::FATAL
@server = WEBrick::HTTPServer.new :DoNotListen => true, :Logger => logger
Expand Down Expand Up @@ -802,6 +803,8 @@ def run

trap("INT") { @server.shutdown; exit! }
trap("TERM") { @server.shutdown; exit! }

launch if @launch

@server.start
end
Expand Down Expand Up @@ -854,6 +857,15 @@ def yaml(req, res)

res.body << index
end

def launch
listeners = @server.listeners.map{|l| l.addr[2] }

host = listeners.any?{|l| l == '0.0.0.0'} ? 'localhost' : listeners.first
cmd = Gem.win_platform? ? 'start' : 'open'

system("#{cmd} http://#{host}:#{@port}")
end

end

0 comments on commit 28822b5

Please sign in to comment.