/custom_app.rb
Ruby | 198 lines | 167 code | 23 blank | 8 comment | 20 complexity | beb58cf92136c3c8f4c3b01a61237c13 MD5 | raw file
- # encoding: UTF-8
- repo = 'https://bitbucket.org/jeffsouza/teste/raw/4741e5e6a033fe9093b1efd93d229f9b1f62fef7/'
- @recipes = ["core", "git", "railsapps", "learn_rails", "rails_bootstrap", "rails_foundation", "rails_omniauth", "rails_devise", "rails_devise_pundit", "rails_signup_download", "rails_mailinglist_signup", "setup", "locale", "readme", "gems", "tests", "email", "devise", "omniauth", "roles", "frontend", "pages", "init", "analytics", "deployment", "extras"]
- @prefs = {}
- @gems = []
- @diagnostics_recipes = [["example"], ["setup"], ["railsapps"], ["gems", "setup"], ["gems", "readme", "setup"], ["extras", "gems", "readme", "setup"], ["example", "git"], ["git", "setup"], ["git", "railsapps"], ["gems", "git", "setup"], ["gems", "git", "readme", "setup"], ["extras", "gems", "git", "readme", "setup"], ["email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["core", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["core", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["core", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["email", "example", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["email", "example", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["email", "example", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["apps4", "core", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["apps4", "core", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "tests"], ["apps4", "core", "deployment", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "testing"], ["apps4", "core", "deployment", "email", "extras", "frontend", "gems", "git", "init", "railsapps", "readme", "setup", "tests"], ["apps4", "core", "deployment", "devise", "email", "extras", "frontend", "gems", "git", "init", "omniauth", "pundit", "railsapps", "readme", "setup", "tests"]]
- @diagnostics_prefs = []
- diagnostics = {}
- # >-------------------------- templates/helpers.erb --------------------------start<
- def recipes; @recipes end
- def recipe?(name); @recipes.include?(name) end
- def prefs; @prefs end
- def prefer(key, value); @prefs[key].eql? value end
- def gems; @gems end
- def diagnostics_recipes; @diagnostics_recipes end
- def diagnostics_prefs; @diagnostics_prefs end
- def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
- def say_loud(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + " #{text}" + "\033[0m" end
- def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
- def say_wizard(text); say_custom(@current_recipe || 'composer', text) end
- def ask_wizard(question)
- ask "\033[1m\033[36m" + ("option").rjust(10) + "\033[1m\033[36m" + " #{question}\033[0m"
- end
- def whisper_ask_wizard(question)
- ask "\033[1m\033[36m" + ("choose").rjust(10) + "\033[0m" + " #{question}"
- end
- def yes_wizard?(question)
- answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
- case answer.downcase
- when "yes", "y"
- true
- when "no", "n"
- false
- else
- yes_wizard?(question)
- end
- end
- def no_wizard?(question); !yes_wizard?(question) end
- def multiple_choice(question, choices)
- say_custom('option', "\033[1m\033[36m" + "#{question}\033[0m")
- values = {}
- choices.each_with_index do |choice,i|
- values[(i + 1).to_s] = choice[1]
- say_custom( (i + 1).to_s + ')', choice[0] )
- end
- answer = whisper_ask_wizard("Enter your selection:") while !values.keys.include?(answer)
- values[answer]
- end
- @current_recipe = nil
- @configs = {}
- @after_blocks = []
- def stage_two(&block); @after_blocks << [@current_recipe, block]; end
- @stage_three_blocks = []
- def stage_three(&block); @stage_three_blocks << [@current_recipe, block]; end
- @before_configs = {}
- def before_config(&block); @before_configs[@current_recipe] = block; end
- def copy_from(source, destination)
- begin
- remove_file destination
- get source, destination
- rescue OpenURI::HTTPError
- say_wizard "Unable to obtain #{source}"
- end
- end
- def copy_from_repo(filename, opts = {})
- repo = 'https://raw.github.com/RailsApps/rails-composer/master/files/'
- repo = opts[:repo] unless opts[:repo].nil?
- if (!opts[:prefs].nil?) && (!prefs.has_value? opts[:prefs])
- return
- end
- source_filename = filename
- destination_filename = if opts[:destination].nil? then filename else opts[:destination] end
- unless opts[:prefs].nil?
- if filename.include? opts[:prefs]
- destination_filename = filename.gsub(/\-#{opts[:prefs]}/, '')
- end
- end
- if (prefer :templates, 'haml') && (filename.include? 'views')
- remove_file destination_filename
- destination_filename = destination_filename.gsub(/.erb/, '.haml')
- end
- if (prefer :templates, 'slim') && (filename.include? 'views')
- remove_file destination_filename
- destination_filename = destination_filename.gsub(/.erb/, '.slim')
- end
- begin
- remove_file destination_filename
- if (prefer :templates, 'haml') && (filename.include? 'views')
- create_file destination_filename, html_to_haml(repo + source_filename)
- elsif (prefer :templates, 'slim') && (filename.include? 'views')
- create_file destination_filename, html_to_slim(repo + source_filename)
- else
- get repo + source_filename, destination_filename
- end
- rescue OpenURI::HTTPError
- say_wizard "Unable to obtain #{source_filename} from the repo #{repo}"
- end
- end
- def html_to_haml(source)
- begin
- html = open(source) {|input| input.binmode.read }
- Haml::HTML.new(html, :erb => true, :xhtml => true).render
- rescue RubyParser::SyntaxError
- say_wizard "Ignoring RubyParser::SyntaxError"
- # special case to accommodate https://github.com/RailsApps/rails-composer/issues/55
- html = open(source) {|input| input.binmode.read }
- say_wizard "applying patch" if html.include? 'card_month'
- say_wizard "applying patch" if html.include? 'card_year'
- html = html.gsub(/, {add_month_numbers: true}, {name: nil, id: "card_month"}/, '')
- html = html.gsub(/, {start_year: Date\.today\.year, end_year: Date\.today\.year\+10}, {name: nil, id: "card_year"}/, '')
- result = Haml::HTML.new(html, :erb => true, :xhtml => true).render
- result = result.gsub(/select_month nil/, "select_month nil, {add_month_numbers: true}, {name: nil, id: \"card_month\"}")
- result = result.gsub(/select_year nil/, "select_year nil, {start_year: Date.today.year, end_year: Date.today.year+10}, {name: nil, id: \"card_year\"}")
- end
- end
- def html_to_slim(source)
- html = open(source) {|input| input.binmode.read }
- haml = Haml::HTML.new(html, :erb => true, :xhtml => true).render
- Haml2Slim.convert!(haml)
- end
- # full credit to @mislav in this StackOverflow answer for the #which() method:
- # - http://stackoverflow.com/a/5471032
- def which(cmd)
- exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
- ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
- exts.each do |ext|
- exe = "#{path}#{File::SEPARATOR}#{cmd}#{ext}"
- return exe if File.executable? exe
- end
- end
- return nil
- end
- # >-------------------------- templates/helpers.erb --------------------------end<
- gem("rspec-rails", :group => "test")
- generate("rspec:install")
- gem("better_errors", :group => "development")
- gem("binding_of_caller", :group => "development")
- gem("simple_form")
- gem("jquery-inputmask-rails")
- gem("bootstrap-generators")
- generate("bootstrap:install -f")
- if yes?("Gostaria de adicionar a gem Unicorn? (y/n)")
- gem("unicorn")
- copy_from_repo 'config/unicorn.rb.example', :repo => repo
- end
- if yes?("Gostaria de adicionar a gem Devise? (y/n)")
- gem("devise")
- #model_name = ask("What would you like the user model to be called? [user]")
- #model_name = "user" if model_name.blank?
- generate("devise:install")
- generate("devise", "Usuario")
- generate("devise:views")
- copy_from_repo 'config/environments/development.rb', :repo => repo
- copy_from_repo 'app/views/devise/passwords/new.html.erb', :repo => repo
- copy_from_repo 'app/views/devise/registrations/edit.html.erb', :repo => repo
- copy_from_repo 'app/views/devise/registrations/new.html.erb', :repo => repo
- copy_from_repo 'app/views/devise/sessions/new.html.erb', :repo => repo
- copy_from_repo 'app/views/devise/shared/_links.erb', :repo => repo
- copy_from_repo 'config/locales/devise.pt-BR.yml', :repo => repo
-
- if yes?("Adicionar a gem Role Model para atribuir papeis aos usuarios? (y/n)")
- gem("role_model")
- copy_from_repo 'app/models/role_model/usuario.rb', :destination => 'app/models/usuario.rb', :repo => repo
- end
- end
- copy_from_repo 'config/database.yml.example', :repo => repo
- copy_from_repo 'app/views/layouts/application.html.erb', :repo => repo
- copy_from_repo 'app/views/layouts/_desktop_menu.html.erb', :repo => repo
- copy_from_repo 'app/assets/stylesheets/application.css', :repo => repo
- copy_from_repo 'app/assets/stylesheets/bootstrap-generators.scss', :repo => repo
- copy_from_repo 'app/assets/stylesheets/bootstrap-variables.scss', :repo => repo
- copy_from_repo 'app/assets/javascripts/application.js', :repo => repo
- copy_from_repo 'config/locales/pt-BR.yml', :repo => repo
- copy_from_repo 'app/assets/images/brasao-large.png', :repo => repo
- copy_from_repo 'app/assets/images/loading2.gif', :repo => repo
- copy_from_repo 'app/assets/images/loading.gif', :repo => repo