#!/usr/bin/env ruby require 'fileutils' require 'optparse' # This script provides integrated control over your applications' instances. # the name of this script should change according to the APPNAME file in RAILS_ROOT. # The path to the root directory of your application. APP_ROOT = File.join(File.dirname(__FILE__), '..') APP_NAME = File.open("#{APP_ROOT}/APPNAME") { |f| f.readline } APP_VERSION = File.open("#{APP_ROOT}/VERSION") { |f| f.readline } script_name = File.basename($0) # Shows the Header of this script puts "#{APP_NAME}, #{APP_VERSION}" puts "" section = ARGV.shift if ['create', 'server'].include?(ARGV[0]) case section # We provide a way to create instances of our applications. # Even after this, it depends on our app gem. when 'create' OPTIONS = { :db_type => 'sqlite', :instance_path => '.' } ARGV.options do |opts| opts.banner = "Usage: ruby #{script_name} create [options]" opts.separator "" opts.on("-t", "--type=db_type", :REQUIRED, String, "Specifies the db type that will be created into the instance.", "Default: sqlite") { |OPTIONS[:db_type]| } opts.on("-i", "--instance=instance_path", String, "Specifies the directory where the instance will be created.", "Default: current directory") { |OPTIONS[:instance_path]| } opts.separator "" opts.on("-h", "--help", "Show this help message.") { puts opts; exit } opts.parse! end INSTANCE = File.expand_path(OPTIONS[:instance_path]) # 1st determine if we are calling this script from the gem/application path! if File.expand_path(APP_ROOT) == INSTANCE puts 'WARN: Please do not try to create a instance inside your application directory!' exit(1) end puts 'Creating instance structure...' # These are the directories that will be created in the specified instance path. INSTANCE_PATHS = %w( config db log ) # Try to create the directories if they don't exist. begin INSTANCE_PATHS.map { |dir| "#{INSTANCE}/#{dir}" }.select { |dir| Dir.mkdir(dir) unless File.exist?(dir) } rescue puts 'ERROR: Cannot create instance on that path, please verify if it exist and try again.' exit(1) end puts "Generating database configuration file for #{OPTIONS[:db_type]}..." if File.exist?("#{APP_ROOT}/db/db-#{OPTIONS[:db_type]}.sql") # Copies the db structure for later importing FileUtils.copy_file("#{APP_ROOT}/db/db-#{OPTIONS[:db_type]}.sql", "#{INSTANCE}/db/db-#{OPTIONS[:db_type]}.sql") # Now create the database.yml file case OPTIONS[:db_type] when 'sqlite', 'sqlite3' database_yml =< 3000, :ip => "0.0.0.0", :environment => "production", :server_root => File.expand_path(File.dirname(__FILE__) + "/../public/"), :server_type => WEBrick::SimpleServer, :instance_path => '.' } ARGV.options do |opts| script_name = File.basename($0) opts.banner = "Usage: ruby #{script_name} [options]" opts.separator "" opts.on("-p", "--port=port", Integer, "Runs Rails on the specified port.", "Default: 3000") { |OPTIONS[:port]| } opts.on("-b", "--binding=ip", String, "Binds Rails to the specified ip.", "Default: 0.0.0.0") { |OPTIONS[:ip]| } opts.on("-e", "--environment=name", String, "Specifies the environment to run this server under (test/development/production).", "Default: production") { |OPTIONS[:environment]| } opts.on("-d", "--daemon", "Make Rails run as a Daemon (only works if fork is available -- meaning on *nix)." ) { OPTIONS[:server_type] = WEBrick::Daemon } opts.on("-i", "--instance=instance_path", String, "Specifies the directory where the instance will be created.", "Default: current directory") { |OPTIONS[:instance_path]| } opts.separator "" opts.on("-h", "--help", "Show this help message.") { puts opts; exit } opts.parse! end ENV["RAILS_ENV"] = OPTIONS[:environment] ENV["INSTANCE"] = File.expand_path(OPTIONS[:instance_path]) require File.dirname(__FILE__) + "/../config/environment" require 'webrick_server' OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT) puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}" puts "=> Ctrl-C to shutdown server; call with --help for options" if OPTIONS[:server_type] == WEBrick::SimpleServer DispatchServlet.dispatch(OPTIONS) else # Since we don't choose a 'section', show the banner puts "Usage: ruby #{script_name} {command} [options]" puts "" puts "Valid commands are: create (instance) and server (webrick)" end