#!/usr/bin/env ruby $: << File.dirname(__FILE__) + '/../lib' require 'rubygems' require 'optparse' require 'awestruct/commands/init' require 'awestruct/commands/generate' require 'awestruct/commands/server' def parse(args) options = OpenStruct.new( { :generate=>true, :server=>false, :port=>4242, :bind_addr=>'0.0.0.0', :auto=>false, :force=>false, :init=>false, :framework=>'compass', :scaffold=>true, :base_url=>nil, } ) opts = OptionParser.new do |opts| opts.on( '-i', '--init', 'Initialize a new project in the current directory' ) do |init| options.init = init options.generate = false end opts.on( '-f', '--framework FRAMEWORK', 'Specify a framework during initialization' ) do |framework| options.framework = framework end opts.on( '--[no-]scaffold', 'Create scaffolding during initialization (default: true)' ) do |s| options.scaffold = s end opts.on( '--force', 'Force a regeneration' ) do |force| options.force = force end opts.on( '-s', '--server', 'Serve generated site' ) do |s| options.server = s end opts.on( '-u', '--url URL', 'Set site.base_url' ) do |url| options.base_url = url end opts.on( '-d', '--dev', 'Run in development mode (--auto, --server and --url http://localhost:4242)' ) do |url| options.base_url = 'http://localhost:4242' options.server = true options.auto = true options.port = 4242 end opts.on( '-a', '--auto', 'Auto-generate when changes are noticed' ) do |a| options.auto = a end opts.on( '-p', '--port PORT', Integer, 'Server port (default: 4242)' ) do |port| options.port = port end opts.on( '-b', '--bind ADDR', 'Server address (default: 0.0.0.0)' ) do |bind_addr| options.bind_addr = bind_addr end opts.on( '-g', '--[no-]generate', 'Generated site' ) do |g| options.generate = g end opts.separator '' opts.separator "Common options:" opts.on_tail("-h", "--help", "Show this message") do puts opts exit end end opts.parse!(args) options end options = parse(ARGV) if ( options.init ) if ( options.generate || options.auto || options.server ) $stderr.puts "--init may not be used with --generate, --auto or --server" end cmd = Awestruct::Commands::Init.new( Dir.pwd, options.framework, options.scaffold ) cmd.run exit end threads = [] if ( options.generate ) cmd = Awestruct::Commands::Generate.new( Dir.pwd, options.base_url, options.force ) cmd.run end if ( options.auto ) threads << Thread.new { generate_cmd = Awestruct::Commands::Generate.new( Dir.pwd, options.base_url ) while ( true ) generate_cmd.run sleep(1) end } end if ( options.server ) threads << Thread.new { server_cmd = Awestruct::Commands::Server.new( File.join( Dir.pwd, '_site' ), options.bind_addr, options.port ) server_cmd.run } end threads.each do |thr| thr.join end