#!/usr/bin/env ruby =begin bivouac (c)2007, 2008 Grégoire Lejeune =end require 'rubygems' require 'optparse' require 'ostruct' require 'active_support' require 'fileutils' require 'yaml' require 'bivouac/template' include Bivouac::Template class << Gem; attr_accessor :loaded_specs; end @BIVOUAC_VERSION = Gem.loaded_specs['bivouac'].version @CAMPING_VERSION = Gem.loaded_specs['camping'].version # Default configuration database_configuration = { :adapter => "sqlite3" } @conf = OpenStruct.new( :db => { :adapter => 'sqlite3', :database => 'db/bivouac.db' }, :orgtype => "GOH", :port => 3301, :address => "0.0.0.0", :session => :cookie, :versions => "{ :scripts => '#{@BIVOUAC_VERSION}', :public => '#{@BIVOUAC_VERSION}', :configs => '#{@BIVOUAC_VERSION}', :app => '#{@BIVOUAC_VERSION}' }" ) opts = OptionParser.new do |opts| opts.banner = "Usage: bivouac [options] app" opts.define_head "#{File.basename($0)}, the generator for the microframework ON-button for ruby #{RUBY_VERSION} (#{RUBY_RELEASE_DATE}) [#{RUBY_PLATFORM}]" opts.separator "" opts.separator "Specific options:" opts.on("-d", "--database NAME", "Preconfigure for selected database (options: mysql/oracle/postgresql/sqlite2/sqlite3).") { |dbtype| database_configuration = { :adapter => dbtype } case dbtype when "mysql" database_configuration[:username] = "" database_configuration[:password] = "" database_configuration[:host] = "localhost" when "postgresql" database_configuration[:username] = "root" database_configuration[:password] = "" database_configuration[:host] = "localhost" when "oracle" database_configuration[:username] = "" database_configuration[:password] = "" when "sqlite2" database_configuration[:adapter] = "sqlite" when "sqlite3" else puts opts exit end @conf.db = {:adapter => dbtype, :database => 'db/bivouac.db'} } opts.on("-P", "--port PORT", "Which port to bind to (3301)") { |port| @conf.port = port } opts.on("-a", "--address ADDR", "Address to bind to (0.0.0.0)") { |address| @conf.address = address } opts.on("-S", "--session TYPE", "Session type (db|cookie). Default : cookie") { |session| @conf.session = session.to_sym unless [:db, :cookie].include?( @conf.session ) puts opts exit end } opts.separator "" opts.separator "Common options:" # No argument, shows at tail. This will print an options summary. # Try it and see! opts.on_tail("-?", "--help", "Show this message") do puts opts exit end # Another typical switch to print the version. opts.on_tail("-v", "--version", "Show version") do class << Gem; attr_accessor :loaded_specs; end puts "Bivouac version #{@BIVOUAC_VERSION}" puts "Camping version #{@CAMPING_VERSION}" exit end end # Parse options opts.parse! ARGV if ARGV.length < 1 puts opts exit end # Get APP name name = ARGV.dup # Add APP name to config @conf.appname = name[0].classify # Add APP path to config @conf.appdir = name[0] # @conf.appname.underscore @conf.appfile = @conf.appname.underscore # Add DataBase configuration to config if database_configuration[:adapter] =~ /sqlite/ database_configuration[:database] = "db/" + @conf.appdir + ".db" else database_configuration[:database] = @conf.appdir end @conf.db = database_configuration # Create Application Directories createDir( @conf.appdir ) ## TODO: Replace by createDir( name ) to keep the given name createDir( "#{@conf.appdir}/config" ) createDir( "#{@conf.appdir}/app" ) createDir( "#{@conf.appdir}/app/controllers" ) createDir( "#{@conf.appdir}/app/helpers" ) createDir( "#{@conf.appdir}/app/models" ) createDir( "#{@conf.appdir}/app/views" ) createDir( "#{@conf.appdir}/app/views/layouts" ) createDir( "#{@conf.appdir}/db" ) createDir( "#{@conf.appdir}/db/migrate" ) createDir( "#{@conf.appdir}/public" ) createDir( "#{@conf.appdir}/public/images" ) createDir( "#{@conf.appdir}/public/javascripts" ) createDir( "#{@conf.appdir}/public/stylesheets" ) createDir( "#{@conf.appdir}/log" ) createDir( "#{@conf.appdir}/script" ) createDir( "#{@conf.appdir}/test" ) createDir( "#{@conf.appdir}/lib" ) createDir( "#{@conf.appdir}/lib/tasks" ) createDir( "#{@conf.appdir}/plugins" ) # Create Application Environment file createFile( "#{@conf.appdir}/config/environment.rb" ) { |io| io.puts template( "environment" ) } # Create Database configuration file createFile( "#{@conf.appdir}/config/database.yml" ) { |io| io.puts YAML.dump( @conf.db ) } # Create Application Postamble file createFile( "#{@conf.appdir}/config/postamble.rb" ) { |io| io.puts template( "application/postamble" ) } # Create Application file createFile( "#{@conf.appdir}/app/#{@conf.appfile}.rb" ) { |io| io.puts template( "application_#{@conf.orgtype.downcase}" ) } # Create Application Helpers Loader file createFile( "#{@conf.appdir}/app/helpers/_helpers.rb" ) { |io| io.puts template( "application/helpers_#{@conf.orgtype.downcase}" ) } # Create generate script createFile( "#{@conf.appdir}/script/generate", true ) { |io| io.puts template( "generate" ) } # Create server script createFile( "#{@conf.appdir}/script/server", true ) { |io| io.puts template( "server" ) } # Create console script createFile( "#{@conf.appdir}/script/console", true ) { |io| io.puts template( "console" ) } # Create console.rc view createFile( "#{@conf.appdir}/config/console.rc" ) { |io| io.puts template( "static/console_rc" ) } # Create plugin script createFile( "#{@conf.appdir}/script/plugin", true ) { |io| io.puts template( "plugin" ) } # Create Rakefile createFile( "#{@conf.appdir}/Rakefile" ) { |io| io.puts template( "Rakefile" ) } # Copy statics files copyTemplate( "static/index.html", "#{@conf.appdir}/public" ) copyTemplate( "static/camping.png", "#{@conf.appdir}/public/images" ) %w{builder.js dragdrop.js prototype.js slider.js unittest.js controls.js effects.js sound.js scriptaculous.js}.each do |f| copyTemplate( "static/#{f}", "#{@conf.appdir}/public/javascripts" ) end copyTemplate( "static/autocomplete.css", "#{@conf.appdir}/public/stylesheets" ) # Create index controller createFile( "#{@conf.appdir}/app/controllers/index.rb" ) { |io| io.puts template( "static/index" ) } # Create not_found controller createFile( "#{@conf.appdir}/app/controllers/not_found.rb" ) { |io| io.puts template( "static/not_found_controller" ) } # Create not_found view createFile( "#{@conf.appdir}/app/views/not_found.rb" ) { |io| io.puts template( "static/not_found_view" ) } # Create default_layout view createFile( "#{@conf.appdir}/app/views/layouts/default_layout.rb" ) { |io| io.puts template( "static/default_layout_view" ) } # Create create.rb if we use DB Sessions if @conf.session.to_s == "db" createFile( "#{@conf.appdir}/db/create.rb" ) { |io| io.puts template( "static/create_for_sessions" ) } end