require 'rubygems' require File.dirname(__FILE__) + "/rocket_starter_options" class Rocket_starter_core # not use this version number in this class @@version = [ 0, 0, 1 ] def initialize(frontend) @options = frontend @logfile = File::new( @options[:log_file], "a" ) if @options[:logging] end =begin for subversion cd current_dir rocket_starter projname scmuri mkdir projname cd projname mkdir trunk branches tags svn import . scmuri [--passowrd=scmpass] -m "create basic directories" rmdir trunk branches tags svn checkout scmuri/trunk . [--passowrd=scmpass] svn mkdir docs rails cd rails rails projname -c [--database=name] svn commit . [--passowrd=scmpass] -m "generate rails app" cd projname trim app set svn::ignore for git cd current_dir rocket_starter projname scmuri mkdir projname cd projname git init mkdir docs rails cd rails rails projname -g [--database=name] git add . git commit -m "generate rails app" cd projname trim app put gitignore files =end def run notice "Rocket_starter: Starting at " + Time.now.strftime("%Y/%m/%d %H:%M:%S") get_current_directory set_scm_password if "svn" == @options[:scmtype] and not @options[:scmuri].empty? subversion_basic_dirs subversion_import subversion_checkout create_basic_dirs_and_project subversion_commit "generate rails app" trim_project subversion_commit "delete not use files" subversion_propset subversion_propset_for_netbeans unless @options[:skip_netbeans] subversion_commit "set svn:ignore" unless @options[:skip_plugins] or @options[:pluginlist_path].empty? setup_plugins subversion_commit "install rails plugins" end elsif "git" == @options[:scmtype] git_project_dirs_and_init create_basic_dirs_and_project git_commit "generate rails app" trim_project git_commit "delete not use files" git_ignore git_ignore_for_netbeans unless @options[:skip_netbeans] git_commit "put .gitignore files" unless @options[:skip_plugins] or @options[:pluginlist_path].empty? setup_plugins git_commit "install rails plugins" end else Rocket_starter_options::error_with_show_usage "irigal SCM type or scmuri is empty." end notice "Rocket_starter: Complete at " + Time.now.strftime("%Y/%m/%d %H:%M:%S") @logfile.close if @logfile end def git_project_dirs_and_init notice "Create project directory and initialize for git" exec_shell_command "mkdir #{@options[:project]}" managed_chdir("#{@scm_root}") exec_shell_command "git init" end def git_commit(message) notice "Commiting to local-git for " + message exec_shell_command "git add ." exec_shell_command "git commit -a -m '#{message}'" end def git_ignore notice "Configuring .gitignore files." exec_shell_command "echo '*.log' >> log/.gitignore" exec_shell_command "echo '*.pid' >> log/.gitignore" exec_shell_command "echo '*' >> tmp/.gitignore" exec_shell_command "echo '*.db' >> db/.gitignore" exec_shell_command "echo '*.sqlite' >> db/.gitignore" exec_shell_command "echo '*.sqlite3' >> db/.gitignore" exec_shell_command "echo 'schema.rb' >> db/.gitignore" exec_shell_command "echo 'schema.sql' >> db/.gitignore" exec_shell_command "echo 'database.yml' >> config/.gitignore" if @options[:ignoredatabase] end def git_ignore_for_netbeans notice "Setting .gitignore file for netbeans." exec_shell_command "mkdir nbproject" exec_shell_command "echo '*' >> nbproject/.gitignore" end def subversion_basic_dirs notice "Create basic directories." exec_shell_command "mkdir #{@options[:project]}" managed_chdir("#{@scm_root}") exec_shell_command "mkdir trunk branches tags" exec_shell_command "pwd" end def subversion_import notice "Importing project to subvserion." exec_shell_command "svn import . #{@options[:scmuri]} #{@scm_password} -m \"Initial import\"" exec_shell_command "rmdir trunk branches tags" end def subversion_checkout notice "Checking out trunk of the project from subversion." exec_shell_command "svn checkout #{@options[:scmuri]}/trunk . #{@scm_password}" end def create_basic_dirs_and_project exec_shell_command "pwd" case @options[:scmtype] when "svn" exec_shell_command "svn mkdir rails docs" managed_chdir("#{@scm_root}/rails") rails_option = " -c" when "git" exec_shell_command "mkdir rails docs" managed_chdir("#{@scm_root}/rails") rails_option = " -g" else exec_shell_command "mkdir rails docs" managed_chdir("#{@scm_root}/rails") rails_option = "" end rails_option << " --database=" + @options[:database] unless "" == @options[:database] if exec_shell_command "rails #{@options[:project]}" + rails_option notice "Rails Project Folder Successfully Created." else notice "Error creating the Rails project folder: exiting." exit end end def subversion_commit(message) notice "Commiting to subversion for " + message exec_shell_command "svn commit #{@scm_root} #{@scm_password} -m '#{message}'" end def trim_project notice "Triming initial project." exec_shell_command "pwd" managed_chdir("#{@scm_root}/rails/#{@options[:project]}") exec_shell_command "svn mv config/database.yml config/database.yml.sample" if @options[:ignoredatabase] case @options[:scmtype] when "svn" exec_shell_command "svn rm doc/README_FOR_APP" exec_shell_command "svn rm README" exec_shell_command "svn rm -r log/*" exec_shell_command "svn rm -r tmp/*" when "git" exec_shell_command "rm doc/README_FOR_APP" exec_shell_command "rm README" exec_shell_command "rm -rf log/*" exec_shell_command "rm -rf tmp/*" end end def subversion_propset notice "Configuring svn:ignore properties." exec_shell_command "svn propset svn:ignore \"*.log\n*.pid\" log/" exec_shell_command "svn propset svn:ignore \"*\" tmp/" exec_shell_command "svn propset svn:ignore \"*.db\n*.sqlite\n*.sqlite3\nschema.rb\nschema.sql\" db/" if @options[:ignoredatabase] exec_shell_command "cp config/database.yml.sample config/database.yml" exec_shell_command "svn propset svn:ignore \"database.yml\" config/" end exec_shell_command "svn update . #{@scm_password}" end def subversion_propset_for_netbeans notice "Setting svn:ignore for netbeans." exec_shell_command "mkdir nbproject" exec_shell_command "svn propset svn:ignore \"*\" nbproject/" exec_shell_command "svn update nbproject/ #{@scm_password}" exec_shell_command "rmdir nbproject" end def setup_plugins begin plugin_option = "" plugin_option << " -x " if @options[:external] File.open(@options[:pluginlist_path]) do |plugin_file| plugin_file.each do |line| if @options[:rapt] exec_shell_command "rapt install #{plugin_option} #{line}" else exec_shell_command "script/plugin install #{plugin_option} #{line}" end end end rescue notice "Can not open #{@options[:pluginlist_path]}" Rocket_starter_options::show_usage end end protected def managed_chdir(path) notice("change directory to " + path) Dir::chdir(path) end def set_scm_password @scm_password = "" @scm_password = '--password=' + @options[:scmpassword] if @options[:scmpassword] end def get_current_directory @current_dir = `pwd`.chomp notice "current directory is " + @current_dir @scm_root = @current_dir + "/" + @options[:project] notice "scm_root is " + @scm_root Rocket_starter_options::error_with_show_usage "irigal directory name." if (@current_dir + "/") == @scm_root end def notice(message) puts "* " + message if @options[:verbose] @logfile.puts "* " + message if @options[:logging] end def exec_shell_command(command) command << " > /dev/null" unless @options[:verbose] command = "sudo " + command if @options[:sudo] puts '->' + command if @options[:verbose] @logfile.puts '->' + command if @options[:logging] #system command unless @options[:emulate] result = `#{command}`.chomp unless @options[:emulate] puts '-->' + result if @options[:verbose] and not result.nil? and not result.empty? @logfile.puts '-->' + result if @options[:logging] and not result.nil? and not result.empty? Rocket_starter_options::error_with_show_usage "receive error from command." unless 0 == $? $? end end