require 'optparse' require "yaml" require "readline" class Rocket_starter_options < Hash @@version = Rocketstarter::VERSION::STRING @@product_name = "rocket_starter" @@command_line_params = {} def initialize super() # default values self[:project] = "" self[:rocket_starter_conf_path] = ENV["HOME"] + "/.rocketst_starter" self[:rocket_starter_conf_path] = ENV['ROCKET_STARTER_CONF'] unless ENV['ROCKET_STARTER_CONF'].nil? self[:init] = false self.merge(Rocket_starter_options::static_default_values) end def parse @@opts = OptionParser.new do |opts| opts.banner = "Usage: #{@@product_name} project-name [options]" opts.separator "Usage: #{@@product_name} --init [options]" opts.separator "Usage: #{@@product_name} --check [options]" opts.separator "version #{@@version}" opts.separator "" opts.separator "Useful Options:" opts.separator "" opts.on( '--conf=/path/to/conffile', 'change conf file path(static < conf < command line params)' ) do |p| self[:rocket_starter_conf_path] = p end opts.on( '--scmtype=[svn|git]', 'choose SCM type. default is svn' ) do |p| @@command_line_params[:scmtype] = p end opts.on( '--scmuri=URI', 'use provided URI for SCM(if need, use with "username@")' ) do |p| @@command_line_params[:scmuri] = p end opts.on( '--scmpassword=PASSWORD', 'use supplied password for SCM' ) do |p| @@command_line_params[:scmpassword] = p end opts.on( '--database=name', 'paramater for rails. default is ""' ) do |p| @@command_line_params[:database] = p end opts.on( '--ignoredatabase', 'set ingore for database.yml') do @@command_line_params[:ignoredatabase] = true end opts.on( '--verbose', 'provide extra output while running') do @@command_line_params[:verbose] = true end opts.on( '--sudo', 'use sudo command') do @@command_line_params[:sudo] = true end opts.on( '--log', 'create a log of command activity' ) do @@command_line_params[:logging] = true end opts.on( '--logfile=/path/to/logfile', 'Set path to logfile' ) do |p| @@command_line_params[:log_file] = p end opts.on( '--skip-netbeans', 'skip to setup propset for netbeans ide' ) do @@command_line_params[:skip_netbeans] = true end opts.on( '--listpath=/path/to/listfile', 'plugin url list path for setup' ) do |p| @@command_line_params[:pluginlist_path] = p end opts.on( '--skip-plugins', 'skip to setup plugins' ) do @@command_line_params[:skip_plugins] = true end opts.on( '--rapt', 'use RaPT gem for plugins' ) do @@command_line_params[:rapt] = true end opts.on( '--externals', 'use svn:externals for plugins' ) do @@command_line_params[:external] = true end opts.on( '--createdb', 'execute rake db:create:all at last' ) do @@command_line_params[:createdb] = true end opts.on( '--dbpassword', 'write password to config/database.yml file' ) do |p| @@command_line_params[:dbpassword] = p end opts.on( '--emulate', 'enable emulation mode' ) do @@command_line_params[:emulate] = true end opts.separator "" opts.separator "General Options:" opts.separator "" opts.on( '-h', '--help', 'display this information' ) do puts opts exit end opts.on( '-v', '--version', 'display version information' ) do puts "Ruby on Rails environment setup tool: #{@@product_name} version #{@@version}" exit end opts.on( '--init', 'setup a template of conf file and plugin list file' ) do self[:init] = true end self[:check] = false opts.on( '--check', 'check current variables' ) do self[:check] = true end opts.separator "" opts.separator "Notice:" opts.separator "" opts.separator "Paramater priority: static < conf < command line" opts.separator "You can use a environment variable ROCKET_STARTER_CONF for a rocket_starter_conf_file" opts.separator "conf path priority: env < command line" end validate_project_name @@opts.parse! marge_values if self[:check] or "--check" == self[:project] puts "current variables" self.each_pair do |key, value| puts "#{key}:#{value}" end exit end if self[:init] or "--init" == self[:project] put_conf_file put_pluginlist_file exit end self end # write a template conf file to rocket_starter_conf_path def put_conf_file # check put path Rocket_starter_options::error_with_show_usage "conf path is empty. set ROCKET_STARTER_CONF environment variable or use --conf paramater." if self[:rocket_starter_conf_path].empty? unless "y" == Readline.readline("would a template file put to #{self[:rocket_starter_conf_path]}? [y/N] > ").downcase puts "Set your conf path to ROCKET_STARTER_CONF environment variable or use --conf paramater." exit end begin File.open(self[:rocket_starter_conf_path]) do |yamlfile| # check overwrite? exit unless "y" == Readline.readline("Overwrite? [y/N]").downcase end rescue end begin File.open(self[:rocket_starter_conf_path], "w") do |yamlfile| yamlfile.puts "# Rocket_starter conf file" yamlfile.puts "" yamlfile.puts Rocket_starter_options::static_default_values.to_yaml end puts "Put template yaml file to #{self[:rocket_starter_conf_path]}" rescue puts "Warning:Could not open for writing. check parmitions." end end # write a template plugin list file to :pluginlist_path def put_pluginlist_file # check put path Sqld4r_options::error_with_show_usage "plugin list path is empty. set --pluginlist_path paramater." if self[:pluginlist_path].empty? unless "y" == Readline.readline("Would a template of plugin list file put to #{self[:pluginlist_path]}? [y/N] > ").downcase puts "Set your command list path to --pluginlist_path paramater." exit end begin File.open(self[:pluginlist_path]) do |textfile| # check overwrite? exit unless "y" == Readline.readline("Overwrite? [y/N]").downcase end rescue end begin File.open(self[:pluginlist_path], "w") do |textfile| textfile.puts "# command list file for executing it before finish" textfile.puts "" textfile.puts "# <- this line is comment." textfile.puts "# next line is plugin URI:" textfile.puts "http://svn.cardboardrocket.com/paginating_find" textfile.puts "http://svn.techno-weenie.net/projects/plugins/attachment_fu/" textfile.puts "http://svn.s21g.com/public/rails/plugins/annotate_models_with_index/" end puts "Put template of plugin list file to #{self[:pluginlist_path]}" rescue puts "Warning:Could not open for writing. check parmitions." end end def self.error_with_show_usage(message) puts "Error:" + message Rocket_starter_options::show_usage exit(1) end def self.show_usage puts @@opts end def validate_project_name temp = ARGV.first unless temp.nil? or temp.empty? @@command_line_params[:project] = temp.chomp else Rocket_starter_options::error_with_show_usage("Need project name.") end end # default values if not use .rocket_starter file def Rocket_starter_options.static_default_values defaults = {} defaults[:scmtype] = "svn" defaults[:scmuri] = "" defaults[:scmpassword] = "" defaults[:database] = "" defaults[:ignoredatabase] = false defaults[:verbose] = false defaults[:sudo] = false defaults[:logging] = false defaults[:log_file] = ENV["HOME"] + "/rocket_starter.log" defaults[:skip_netbeans] = false defaults[:pluginlist_path] = ENV["HOME"] + "/useful_plugins" defaults[:skip_plugins] = false defaults[:rapt] = false defaults[:external] = false defaults[:createdb] = false defaults[:dbpassword] = "" defaults[:emulate] = false defaults end # priority static < conf < command line params def marge_values conf_params = {} # can open? begin File::open(self[:rocket_starter_conf_path]) do |conf_file| temp = YAML.load(conf_file.read) conf_params = temp if temp end rescue end self.merge!(conf_params.merge!(@@command_line_params)) end end