lib/vegas/runner.rb in vegas-0.1.3 vs lib/vegas/runner.rb in vegas-0.1.4

- old
+ new

@@ -4,11 +4,11 @@ if Vegas::WINDOWS begin require 'win32/process' rescue - puts "Sorry, in order to use Vegas on Windows you need the win32-process gem:\n " + + puts "Sorry, in order to use Vegas on Windows you need the win32-process gem:", "gem install win32-process" end end module Vegas @@ -23,12 +23,12 @@ def initialize(app, app_name, set_options = {}, runtime_args = ARGV, &block) @options = set_options || {} self.class.logger.level = options[:debug] ? Logger::DEBUG : Logger::INFO - @app = app - @app_name = app_name + @app = app + @app_name = app_name @filesystem_friendly_app_name = @app_name.gsub(/\W+/, "_") @quoted_app_name = "'#{app_name}'" @runtime_args = runtime_args @@ -70,32 +70,32 @@ start(path) end def app_dir - File.join(ROOT_DIR, filesystem_friendly_app_name) + options[:app_dir] || File.join(ROOT_DIR, filesystem_friendly_app_name) end def pid_file - File.join(app_dir, "#{filesystem_friendly_app_name}.pid") + options[:pid_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.pid") end def url_file - File.join(app_dir, "#{filesystem_friendly_app_name}.url") + options[:url_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.url") end + def log_file + options[:log_file] || File.join(app_dir, "#{filesystem_friendly_app_name}.log") + end + def url "http://#{host}:#{port}" end - def log_file - File.join(app_dir, "#{filesystem_friendly_app_name}.log") - end - def start(path = nil) logger.info "Running with Windows Settings" if WINDOWS - logger.info "Starting #{quoted_app_name}" + logger.info "Starting #{quoted_app_name}..." begin check_for_running(path) find_port write_url launch!(url, path) @@ -110,11 +110,12 @@ def find_port if @port = options[:port] announce_port_attempted unless port_open? - logger.warn "Port #{port} is already in use. Please try another or don't use -P, for auto-port" + logger.warn "Port #{port} is already in use. Please try another. " + + "You can also omit the port flag, and we'll find one for you." end else @port = PORT announce_port_attempted @@ -124,11 +125,11 @@ end end end def announce_port_attempted - logger.info "Trying to start #{quoted_app_name} on port #{port}" + logger.info "trying port #{port}..." end def port_open?(check_url = nil) begin open(check_url || url) @@ -170,19 +171,20 @@ def daemonize! if RUBY_VERSION < "1.9" logger.debug "Parent Process: #{Process.pid}" exit! if fork logger.debug "Child Process: #{Process.pid}" - Dir.chdir "/" - File.umask 0000 - FileUtils.touch(log_file) - STDIN.reopen log_file - STDOUT.reopen log_file, "a" - STDERR.reopen log_file, "a" else - Process.daemon + Process.daemon(true, true) end + + File.umask 0000 + FileUtils.touch log_file + STDIN.reopen log_file + STDOUT.reopen log_file, "a" + STDERR.reopen log_file, "a" + logger.debug "Child Process: #{Process.pid}" File.open(pid_file, 'w') {|f| f.write("#{Process.pid}") } at_exit { delete_pid! } end @@ -273,15 +275,25 @@ def define_options OptionParser.new("", 24, ' ') do |opts| # TODO instead of app_name, we should determine the name of the script # used to invoke Vegas and use that here - opts.banner = "Usage: your_executable_name [options]" + opts.banner = "Usage: #{$0 || app_name} [options]" opts.separator "" opts.separator "Vegas options:" + opts.on('-K', "--kill", "kill the running process and exit") {|k| + kill! + exit + } + + opts.on('-S', "--status", "display the current running PID and URL then quit") {|s| + status + exit! + } + opts.on("-s", "--server SERVER", "serve using SERVER (thin/mongrel/webrick)") { |s| @rack_handler = Rack::Handler.get(s) } opts.on("-o", "--host HOST", "listen on HOST (default: #{HOST})") { |host| @@ -301,24 +313,30 @@ } opts.on("-L", "--no-launch", "don't launch the browser") { |f| @options[:skip_launch] = true } - - opts.on('-K', "--kill", "kill the running process and exit") {|k| - kill! - exit - } - - opts.on('-S', "--status", "display the current running PID and URL then quit") {|s| - status - exit! - } - + opts.on('-d', "--debug", "raise the log level to :debug (default: :info)") {|s| @options[:debug] = true } - + + opts.on("--app-dir APP_DIR", "set the app dir where files are stored (default: ~/.vegas/#{filesystem_friendly_app_name})/)") {|app_dir| + @options[:app_dir] = app_dir + } + + opts.on("-P", "--pid-file PID_FILE", "set the path to the pid file (default: app_dir/#{filesystem_friendly_app_name}.pid)") {|pid_file| + @options[:pid_file] = pid_file + } + + opts.on("--log-file LOG_FILE", "set the path to the log file (default: app_dir/#{filesystem_friendly_app_name}.log)") {|log_file| + @options[:log_file] = log_file + } + + opts.on("--url-file URL_FILE", "set the path to the URL file (default: app_dir/#{filesystem_friendly_app_name}.url)") {|url_file| + @options[:url_file] = url_file + } + yield opts if block_given? opts.separator "" opts.separator "Common options:"