require 'optparse' require "yaml" require "readline" =begin script/generate model post title:string body:text published:boolean owner:references --svn Modify files with subversion. (Note: svn must be in path) --git Modify files with git. (Note: git must be in path) --skip-timestamps Don't add timestamps to the migration file for this model --skip-migration Don't generate a migration file for this model --skip-fixture Don't generation a fixture file for this model script/generate rspec_model Blog name:string =end class Sqld4r_options < Hash @@version = Sqld4r::VERSION::STRING @@product_name = "sqld4r" @@command_line_params = {} def initialize super() # default values self["sqld_path"] = "" self["sqld4r_conf_path"] = ENV["HOME"] + "/.sqld4r" self["sqld4r_conf_path"] = ENV['SQLD4R_CONF'] unless ENV['SQLD4R_CONF'].nil? self["init"] = false self.merge(Sqld4r_options::static_default_values) end def parse @@opts = OptionParser.new do |opts| opts.banner = "Usage: #{@@product_name} SQLDesigner_xml_path [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($HOME/.sqld4r < conf < command line params)' ) do |p| self["sqld4r_conf_path"] = p end opts.on( '--save=/save/to/xmlfile', 'save xml file path(../docs/sqldesigner.xml < conf < command line params)' ) do |p| self["copy_sqld_to"] = p end opts.on( '--svn', 'Modify files with subversion. (Note: svn must be in path/for script/generate)') do @@command_line_params["svn"] = true end opts.on( '--git', 'Modify files with git. (Note: git must be in path/for script/generate)') do @@command_line_params["git"] = true end opts.on( '--skip-timestamps', "Don't add timestamps to the migration file for this model") do @@command_line_params["skip-timestamps"] = true end opts.on( '--skip-migration', "Don't generate a migration file for this model") do @@command_line_params["skip-migration"] = true end opts.on( '--skip-fixture', "Don't generation a fixture file for this model") do @@command_line_params["skip-fixture"] = true end opts.on( '--rspec', 'generate model with rspec') do @@command_line_params["rspec"] = true end opts.on( '--skip-relation', "Don't add belongs_to/has_one/has_many to a model") do @@command_line_params["skip-relation"] = true end opts.on( '--skip-index', "Don't add index to migration files") do @@command_line_params["skip-index"] = 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. default is ../docs/sqld4r.log' ) do |p| @@command_line_params["log_file"] = p end opts.on( '--command_list=/path/to/listfile', 'Set path to a command list for executing it before finish. default is $HOME/sqld4r_execute_commands' ) do |p| @@command_line_params["command_list_path"] = p end opts.on( '--skip_command_list', "Don't execute commands on a command list" ) do @@command_line_params["skip_command_list"] = true 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 "The model generator for SQLDesigner: #{@@product_name} version #{@@version}" exit end opts.on( '--init', 'setup a template of conf file and execute_command_list' ) 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 SQLD4R_CONF for a sqld4r_conf_file" opts.separator "conf path priority: env < command line" opts.separator "I tested with Rails 2.1.0" end validate_sqld_path @@opts.parse! marge_values validate_options if self["check"] or "--check" == self["sqld_path"] puts "current variables" self.each_pair do |key, value| puts "#{key}:#{value}" end exit end if self["init"] or "--init" == self["sqld_path"] put_conf_file put_commandlist_file exit end self end def validate_options error_with_show_usage "Can not use --svn option with --git" if self["git"] and self["svn"] end # write a template conf file to sqld4r_conf_path def put_conf_file # check put path Sqld4r_options::error_with_show_usage "conf path is empty. set SQL4R_CONF environment variable or use --conf paramater." if self["sqld4r_conf_path"].empty? unless "y" == Readline.readline("Would a template file put to #{self["sqld4r_conf_path"]}? [y/N] > ").downcase puts "Set your conf path to SQLD4R_CONF environment variable or use --conf paramater." exit end begin File.open(self["sqld4r_conf_path"]) do |yamlfile| # check overwrite? exit unless "y" == Readline.readline("Overwrite? [y/N]").downcase end rescue end begin File.open(self["sqld4r_conf_path"], "w") do |yamlfile| yamlfile.puts "# sqld4r conf file" yamlfile.puts "" yamlfile.puts Sqld4r_options::static_default_values.to_yaml end puts "Put template yaml file to #{self["sqld4r_conf_path"]}" rescue puts "Warning:Could not open for writing. check parmitions." end end # write a template command list file to command_list_path def put_commandlist_file # check put path Sqld4r_options::error_with_show_usage "command list path is empty. set --command_list paramater." if self["command_list_path"].empty? unless "y" == Readline.readline("Would a template file put to #{self["command_list_path"]}? [y/N] > ").downcase puts "Set your command list path to --command_list paramater." exit end begin File.open(self["command_list_path"]) do |textfile| # check overwrite? exit unless "y" == Readline.readline("Overwrite? [y/N]").downcase end rescue end begin File.open(self["command_list_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 executable commands:" textfile.puts "rake db:migrate" textfile.puts "capify ." textfile.puts "# NOTE:recommend 'rake db:migrate' command to write here." end puts "Put template command list file to #{self["command_list_path"]}" rescue puts "Warning:Could not open for writing. check parmitions." end end def self.error_with_show_usage(message) puts "Error:" + message Sqld4r_options::show_usage exit(1) end def self.show_usage puts @@opts end def validate_sqld_path temp = ARGV.first unless temp.nil? or temp.empty? @@command_line_params["sqld_path"] = temp.chomp else Sqld4r_options::error_with_show_usage("Need SQLDesigner_xml_path.") end end # default values if not use .sqld4r file def self.static_default_values defaults = {} defaults["sqld4r_conf_path"] = ENV["HOME"] + "/.sqld4r" defaults["copy_sqld_to"] = "../../docs/sqldesigner.xml" defaults["svn"] = false defaults["git"] = false defaults["skip-timestamps"] = false defaults["skip-migration"] = false defaults["skip-fixture"] = false defaults["rspec"] = false defaults["skip-relation"] = false defaults["skip-index"] = false defaults["command_list_path"] = ENV["HOME"] + "/sqld4r_execute_commands" defaults["skip_command_list"] = false defaults["verbose"] = false defaults["sudo"] = false defaults["logging"] = false defaults["log_file"] = "../../docs/sqld4r.log" defaults["emulate"] = false defaults end # priority static < conf < command line params def marge_values conf_params = {} # can open? begin File::open(self["sqld4r_conf_path"]) do |conf_file| temp = YAML.load(conf_file.read) conf_params = temp if temp end rescue conf_params = {} end self.merge!(conf_params.merge!(@@command_line_params)) end end