#!/usr/bin/env ruby $: << File.dirname(__FILE__) + '/../lib' require 'rubygems' require 'optparse' require 'awestruct/commands/init' require 'awestruct/commands/generate' require 'awestruct/commands/server' require 'awestruct/commands/deploy' require 'yaml' 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, :profile=>nil, :deploy=>false, :script=>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 -profile development)' ) do |url| options.server = true options.auto = true options.port = 4242 options.profile = 'development' end opts.on( '-P', '--profile PROFILE', 'Specify a profile' ) do |profile| options.profile = profile end opts.on( '--deploy', 'Deploy site' ) do |deploy| options.deploy = deploy 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.on( '--run SCRIPT', 'Force a regeneration' ) do |script| options.script = script 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 def site_path dir = (@profiles_data.nil? || @profile_data.nil? || @profile_data['output_dir'].nil?) ? '_site' : @profile_data['output_dir'] path = File.join( Dir.pwd, dir ) end options = parse(ARGV) unless options.init site_yaml_file = File.join( Dir.pwd, '_config', 'site.yml' ) site_yaml = YAML.load( File.read( site_yaml_file ) ) if site_yaml @profiles_data = site_yaml['profiles'] || {} @profile_data = if @profiles_data.nil? nil else if options.profile @profiles_data[options.profile] else # if no profile given, pick the first with deploy config options.profile, profile_data = @profiles_data.select { |k,v| v['deploy'] }.first profile_data end end end end config = Awestruct::Config.new(Dir.pwd) # config.input_dir = File.join(Dir.pwd, '_root') if ( options.init ) if ( options.generate || options.auto || options.server ) $stderr.puts "--init may not be used with --generate, --auto or --server" exit end cmd = Awestruct::Commands::Init.new( Dir.pwd, options.framework, options.scaffold ) cmd.run exit end default_base_url = 'http://localhost:4242' def camelize(str) str.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase } end if ( options.script ) puts "Running #{options.script} and exiting" require "awestruct/scripts/#{options.script}" script_class = eval "Awestruct::Scripts::#{camelize(options.script)}" script = script_class.new(*ARGV) script.run exit end if ( options.server ) hostname = ( options.bind_addr == '0.0.0.0' ? 'localhost' : options.bind_addr ) default_base_url = "http://#{hostname}:#{options.port}" end if ( options.generate ) cmd = Awestruct::Commands::Generate.new( config, options.profile, options.base_url, default_base_url, options.force ) cmd.run end if ( options.deploy ) if ( options.auto || options.server ) $stderr.puts "--deploy may not be used with --auto or --server" exit end if ( ! options.profile ) $stderr.puts "You must specify a --profile when using --deploy" exit end if ( @profiles_data.nil? ) $stderr.puts "_config/site.yml must define a 'profiles' section" exit end if ( @profile_data.nil? ) $stderr.puts "Unknown profile '#{options.profile}'" exit end if ( ! File.exist?( site_path ) ) $stderr.puts "No #{site_path}/ generated yet." exit end deploy_data = @profile_data['deploy'] if ( deploy_data.nil? ) $stderr.puts "No configuration for 'deploy'" exit end puts "deploying to '#{options.profile}'" cmd = Awestruct::Commands::Deploy.new( site_path, deploy_data ) cmd.run puts "deploy finished" exit end threads = [] if ( options.auto ) threads << Thread.new { generate_cmd = Awestruct::Commands::Generate.new( config, options.profile, options.base_url, default_base_url ) while ( true ) generate_cmd.run sleep(1) end } end if ( options.server ) threads << Thread.new { puts "Serving site at #{site_path}" server_cmd = Awestruct::Commands::Server.new( site_path, options.bind_addr, options.port ) server_cmd.run } end threads.each do |thr| thr.join end