1require 'rubygems/command'
2require 'rubygems/server'
3
4class Gem::Commands::ServerCommand < Gem::Command
5
6  def initialize
7    super 'server', 'Documentation and gem repository HTTP server',
8          :port => 8808, :gemdir => [], :daemon => false
9
10    OptionParser.accept :Port do |port|
11      if port =~ /\A\d+\z/ then
12        port = Integer port
13        raise OptionParser::InvalidArgument, "#{port}: not a port number" if
14          port > 65535
15
16        port
17      else
18        begin
19          Socket.getservbyname port
20        rescue SocketError
21          raise OptionParser::InvalidArgument, "#{port}: no such named service"
22        end
23      end
24    end
25
26    add_option '-p', '--port=PORT', :Port,
27               'port to listen on' do |port, options|
28      options[:port] = port
29    end
30
31    add_option '-d', '--dir=GEMDIR',
32               'directories from which to serve gems',
33               'multiple directories may be provided' do |gemdir, options|
34      options[:gemdir] << File.expand_path(gemdir)
35    end
36
37    add_option '--[no-]daemon', 'run as a daemon' do |daemon, options|
38      options[:daemon] = daemon
39    end
40
41    add_option '-b', '--bind=HOST,HOST',
42               'addresses to bind', Array do |address, options|
43      options[:addresses] ||= []
44      options[:addresses].push(*address)
45    end
46
47    add_option '-l', '--launch[=COMMAND]',
48               'launches a browser window',
49               "COMMAND defaults to 'start' on Windows",
50               "and 'open' on all other platforms" do |launch, options|
51      launch ||= Gem.win_platform? ? 'start' : 'open'
52      options[:launch] = launch
53    end
54  end
55
56  def defaults_str # :nodoc:
57    "--port 8808 --dir #{Gem.dir} --no-daemon"
58  end
59
60  def description # :nodoc:
61    <<-EOF
62The server command starts up a web server that hosts the RDoc for your
63installed gems and can operate as a server for installation of gems on other
64machines.
65
66The cache files for installed gems must exist to use the server as a source
67for gem installation.
68
69To install gems from a running server, use `gem install GEMNAME --source
70http://gem_server_host:8808`
71
72You can set up a shortcut to gem server documentation using the URL:
73
74  http://localhost:8808/rdoc?q=%s - Firefox
75  http://localhost:8808/rdoc?q=* - LaunchBar
76
77    EOF
78  end
79
80  def execute
81    options[:gemdir] = Gem.path if options[:gemdir].empty?
82    Gem::Server.run options
83  end
84
85end
86
87