#!/usr/bin/env ruby require 'delegate' require 'optparse' require 'ostruct' require 'stringio' require 'yaml' require 'liars' conf = OpenStruct.new(:host => '0.0.0.0', :port => 2008, :db => Liars::DbEngine.db_name) if conf.rc and File.exists?( conf.rc ) YAML.load_file(conf.rc).each do |k,v| conf.send("#{k}=", v) end end opts = OptionParser.new do |opts| opts.banner = "Liars, a tiny MVC web framework. " # opts.define_head "Ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" opts.separator "usage: liars -s test.rb && liars test.rb" opts.separator "see http://github.com/luonet/liars for more details" opts.separator "" opts.separator "options:" opts.on("-h", "--host HOSTNAME", "web server host name(default #{conf.host})") { |conf.host| } opts.on("-p", "--port NUM", "Web server binding port(default #{conf.port})") { |conf.port| } opts.on("-d", "--database FILE", "database file (default #{conf.db})") { |conf.db| } opts.on("-c", "--console", "running in irb mode") { conf.server = :console } opts.on("-s", "--scaffold FILE", "generates a scaffold file") do |conf.scaffold| conf.scaffold =~ /^(\w+)(\.rb)?$/ File.open("#{Dir.pwd}/#{$1}.rb", 'w') do |f| class_name = $1.singularize.camelize f.puts <<-EOF module Liars # Model ====================================================================== class #{class_name} < M end # Controller ================================================================= class #{class_name}C < C route_of :index, '/' def index render end end # View ======================================================================= class #{class_name}V < V def layout html do head { title 'Liars never tell lies!' } body do yield end end end def index h1 { a '#{class_name}', :href => "/" } end end end EOF end puts "template #{$1}.rb generated" exit end opts.separator "" opts.separator "other options:" opts.on_tail("-?", "--help", "show help messages") do puts opts exit end opts.on_tail("-v", "--version", "show version") do class << Gem; attr_accessor :loaded_specs; end puts Gem.loaded_specs['liars'].version exit end end opts.parse! ARGV if ARGV.length < 1 puts opts exit else ARGV.each {|f| require "#{Dir.pwd}/#{f}" } end if conf.server == :console ARGV.clear require 'irb' require 'irb/completion' ENV['IRBRC'] = ".irbrc" if File.exists? ".irbrc" IRB.start else Liars::DbEngine.db_name = conf.db Liars::DbEngine.create_db # Mount the root s = WEBrick::HTTPServer.new(:BindAddress => conf.host, :Port => conf.port) s.mount "/", Liars::LiarsHandler, Liars::C # Server up trap(:INT) do s.shutdown Liars::DbEngine.write_db end s.start end