require 'optparse' require 'ostruct' module Atchoum # Make the website script run as a standalone script. # Hit the script with -? or without any arguments to get usage. class AutoRunner def self.run new.run end def initialize websites = [] ObjectSpace.each_object(Class) do |klass| websites << klass if(klass < Website) end # TODO handled more then one website ? warn 'Supports only one website per script' if websites.size > 1 @website = websites.first end # Runs the script. Try -? to get help and usage. def run conf = OpenStruct.new conf.port = 2000 opts = OptionParser.new do |opts| opts.banner = "Usage: ruby #{$PROGRAM_NAME} [options]" opts.define_head "Because static websites need attention too!" opts.separator "" opts.separator "Options:" opts.on("-d", "--dump", "Dumps the website to HTML files") do conf.dump = true end opts.on("-s", "--server", "Starts webrick server") do conf.server = true end opts.on("-p", "--port NUM", "The webrick port to run on") { |conf.port| } opts.on_tail("-?", "--help", "Show this message") do puts opts return 0 end end opts.parse! ARGV if conf.dump @website.new.to_files if conf.dump elsif conf.server Server.new($PROGRAM_NAME, conf.port).start if conf.server else puts opts return -1 end 0 end end end at_exit do unless $! exit Atchoum::AutoRunner.run end end