#!/usr/bin/env ruby require "thor" require 'find' require 'erb' module RailsMongoBackboneBootstrapTmpl class Rmbb_Generator < Thor include Thor::Actions desc "new", "Auto create new rails app based on mongo , devise, backboneJs, bootstrap" method_option :app_path, :type => :string, :aliases => "-p", :desc => "where you want your rails application placed to" method_option :db_name, :type => :string, :aliases => "-d", :desc => "set database name" method_option :rvmrc, :type => :string, :aliases => "-r", :desc => "create .rvmrc file with rvmrc string value" def new(app_name) app_name = app_name.gsub(/[\s]+/,'_').capitalize say "Your app name will be : #{app_name} \n" app_path = options[:app_path] || "./" app_whole_path = "#{File.expand_path(app_path,__FILE__)}/#{app_name}" template_path = File.expand_path('../../template',__FILE__) say "Your app name will be : #{app_name} , path to : #{app_whole_path} \n" db_name = options[:db_name] || app_name.downcase if Dir.exists? app_whole_path result = ask "#{app_whole_path} existed ! Are you overwritten it ? [yes/no]" if !result.eql? 'no' self.copy_file(app_whole_path,template_path) self.replace_file(app_whole_path, app_name, db_name) self.create_rvmrc_file(app_whole_path,options[:rvmrc]) if options[:rvmrc] say "create new app successfully!\n" else say "command terminated by user.\n" end else self.copy_file(app_whole_path,template_path) self.replace_file(app_whole_path, app_name, db_name) self.create_rvmrc_file(app_whole_path,options[:rvmrc]) if options[:rvmrc] say "create new app successfully!\n" end end protected def copy_file(app_whole_path,template_path) FileUtils.mkdir_p "#{app_whole_path}" FileUtils.cp_r "#{template_path}/.", "#{app_whole_path}" end def replace_file(target_path,app_name,db_name) Find.find(target_path) do |f| if File.file?(f) and f.to_s.match(/(.rb$)|(.ru$)|(.slim$)|(Rakefile$)|(.yml$)/) content = ERB.new(File.new(f).read).result(binding) File.open(f, 'w') { |p| p.write(content) } end end end def create_rvmrc_file(target_path,rvmrc_string) File.open("#{target_path}/.rvmrc","w+"){|f| f.write(rvmrc_string)} end end end RailsMongoBackboneBootstrapTmpl::Rmbb_Generator.start