PageRenderTime 63ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/custom_app.rb

https://bitbucket.org/jeffsouza/teste
Ruby | 198 lines | 167 code | 23 blank | 8 comment | 20 complexity | beb58cf92136c3c8f4c3b01a61237c13 MD5 | raw file
  1. # encoding: UTF-8
  2. repo = 'https://bitbucket.org/jeffsouza/teste/raw/4741e5e6a033fe9093b1efd93d229f9b1f62fef7/'
  3. @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"]
  4. @prefs = {}
  5. @gems = []
  6. @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"]]
  7. @diagnostics_prefs = []
  8. diagnostics = {}
  9. # >-------------------------- templates/helpers.erb --------------------------start<
  10. def recipes; @recipes end
  11. def recipe?(name); @recipes.include?(name) end
  12. def prefs; @prefs end
  13. def prefer(key, value); @prefs[key].eql? value end
  14. def gems; @gems end
  15. def diagnostics_recipes; @diagnostics_recipes end
  16. def diagnostics_prefs; @diagnostics_prefs end
  17. def say_custom(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + "\033[0m" + " #{text}" end
  18. def say_loud(tag, text); say "\033[1m\033[36m" + tag.to_s.rjust(10) + " #{text}" + "\033[0m" end
  19. def say_recipe(name); say "\033[1m\033[36m" + "recipe".rjust(10) + "\033[0m" + " Running #{name} recipe..." end
  20. def say_wizard(text); say_custom(@current_recipe || 'composer', text) end
  21. def ask_wizard(question)
  22. ask "\033[1m\033[36m" + ("option").rjust(10) + "\033[1m\033[36m" + " #{question}\033[0m"
  23. end
  24. def whisper_ask_wizard(question)
  25. ask "\033[1m\033[36m" + ("choose").rjust(10) + "\033[0m" + " #{question}"
  26. end
  27. def yes_wizard?(question)
  28. answer = ask_wizard(question + " \033[33m(y/n)\033[0m")
  29. case answer.downcase
  30. when "yes", "y"
  31. true
  32. when "no", "n"
  33. false
  34. else
  35. yes_wizard?(question)
  36. end
  37. end
  38. def no_wizard?(question); !yes_wizard?(question) end
  39. def multiple_choice(question, choices)
  40. say_custom('option', "\033[1m\033[36m" + "#{question}\033[0m")
  41. values = {}
  42. choices.each_with_index do |choice,i|
  43. values[(i + 1).to_s] = choice[1]
  44. say_custom( (i + 1).to_s + ')', choice[0] )
  45. end
  46. answer = whisper_ask_wizard("Enter your selection:") while !values.keys.include?(answer)
  47. values[answer]
  48. end
  49. @current_recipe = nil
  50. @configs = {}
  51. @after_blocks = []
  52. def stage_two(&block); @after_blocks << [@current_recipe, block]; end
  53. @stage_three_blocks = []
  54. def stage_three(&block); @stage_three_blocks << [@current_recipe, block]; end
  55. @before_configs = {}
  56. def before_config(&block); @before_configs[@current_recipe] = block; end
  57. def copy_from(source, destination)
  58. begin
  59. remove_file destination
  60. get source, destination
  61. rescue OpenURI::HTTPError
  62. say_wizard "Unable to obtain #{source}"
  63. end
  64. end
  65. def copy_from_repo(filename, opts = {})
  66. repo = 'https://raw.github.com/RailsApps/rails-composer/master/files/'
  67. repo = opts[:repo] unless opts[:repo].nil?
  68. if (!opts[:prefs].nil?) && (!prefs.has_value? opts[:prefs])
  69. return
  70. end
  71. source_filename = filename
  72. destination_filename = if opts[:destination].nil? then filename else opts[:destination] end
  73. unless opts[:prefs].nil?
  74. if filename.include? opts[:prefs]
  75. destination_filename = filename.gsub(/\-#{opts[:prefs]}/, '')
  76. end
  77. end
  78. if (prefer :templates, 'haml') && (filename.include? 'views')
  79. remove_file destination_filename
  80. destination_filename = destination_filename.gsub(/.erb/, '.haml')
  81. end
  82. if (prefer :templates, 'slim') && (filename.include? 'views')
  83. remove_file destination_filename
  84. destination_filename = destination_filename.gsub(/.erb/, '.slim')
  85. end
  86. begin
  87. remove_file destination_filename
  88. if (prefer :templates, 'haml') && (filename.include? 'views')
  89. create_file destination_filename, html_to_haml(repo + source_filename)
  90. elsif (prefer :templates, 'slim') && (filename.include? 'views')
  91. create_file destination_filename, html_to_slim(repo + source_filename)
  92. else
  93. get repo + source_filename, destination_filename
  94. end
  95. rescue OpenURI::HTTPError
  96. say_wizard "Unable to obtain #{source_filename} from the repo #{repo}"
  97. end
  98. end
  99. def html_to_haml(source)
  100. begin
  101. html = open(source) {|input| input.binmode.read }
  102. Haml::HTML.new(html, :erb => true, :xhtml => true).render
  103. rescue RubyParser::SyntaxError
  104. say_wizard "Ignoring RubyParser::SyntaxError"
  105. # special case to accommodate https://github.com/RailsApps/rails-composer/issues/55
  106. html = open(source) {|input| input.binmode.read }
  107. say_wizard "applying patch" if html.include? 'card_month'
  108. say_wizard "applying patch" if html.include? 'card_year'
  109. html = html.gsub(/, {add_month_numbers: true}, {name: nil, id: "card_month"}/, '')
  110. html = html.gsub(/, {start_year: Date\.today\.year, end_year: Date\.today\.year\+10}, {name: nil, id: "card_year"}/, '')
  111. result = Haml::HTML.new(html, :erb => true, :xhtml => true).render
  112. result = result.gsub(/select_month nil/, "select_month nil, {add_month_numbers: true}, {name: nil, id: \"card_month\"}")
  113. 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\"}")
  114. end
  115. end
  116. def html_to_slim(source)
  117. html = open(source) {|input| input.binmode.read }
  118. haml = Haml::HTML.new(html, :erb => true, :xhtml => true).render
  119. Haml2Slim.convert!(haml)
  120. end
  121. # full credit to @mislav in this StackOverflow answer for the #which() method:
  122. # - http://stackoverflow.com/a/5471032
  123. def which(cmd)
  124. exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  125. ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
  126. exts.each do |ext|
  127. exe = "#{path}#{File::SEPARATOR}#{cmd}#{ext}"
  128. return exe if File.executable? exe
  129. end
  130. end
  131. return nil
  132. end
  133. # >-------------------------- templates/helpers.erb --------------------------end<
  134. gem("rspec-rails", :group => "test")
  135. generate("rspec:install")
  136. gem("better_errors", :group => "development")
  137. gem("binding_of_caller", :group => "development")
  138. gem("simple_form")
  139. gem("jquery-inputmask-rails")
  140. gem("bootstrap-generators")
  141. generate("bootstrap:install -f")
  142. if yes?("Gostaria de adicionar a gem Unicorn? (y/n)")
  143. gem("unicorn")
  144. copy_from_repo 'config/unicorn.rb.example', :repo => repo
  145. end
  146. if yes?("Gostaria de adicionar a gem Devise? (y/n)")
  147. gem("devise")
  148. #model_name = ask("What would you like the user model to be called? [user]")
  149. #model_name = "user" if model_name.blank?
  150. generate("devise:install")
  151. generate("devise", "Usuario")
  152. generate("devise:views")
  153. copy_from_repo 'config/environments/development.rb', :repo => repo
  154. copy_from_repo 'app/views/devise/passwords/new.html.erb', :repo => repo
  155. copy_from_repo 'app/views/devise/registrations/edit.html.erb', :repo => repo
  156. copy_from_repo 'app/views/devise/registrations/new.html.erb', :repo => repo
  157. copy_from_repo 'app/views/devise/sessions/new.html.erb', :repo => repo
  158. copy_from_repo 'app/views/devise/shared/_links.erb', :repo => repo
  159. copy_from_repo 'config/locales/devise.pt-BR.yml', :repo => repo
  160. if yes?("Adicionar a gem Role Model para atribuir papeis aos usuarios? (y/n)")
  161. gem("role_model")
  162. copy_from_repo 'app/models/role_model/usuario.rb', :destination => 'app/models/usuario.rb', :repo => repo
  163. end
  164. end
  165. copy_from_repo 'config/database.yml.example', :repo => repo
  166. copy_from_repo 'app/views/layouts/application.html.erb', :repo => repo
  167. copy_from_repo 'app/views/layouts/_desktop_menu.html.erb', :repo => repo
  168. copy_from_repo 'app/assets/stylesheets/application.css', :repo => repo
  169. copy_from_repo 'app/assets/stylesheets/bootstrap-generators.scss', :repo => repo
  170. copy_from_repo 'app/assets/stylesheets/bootstrap-variables.scss', :repo => repo
  171. copy_from_repo 'app/assets/javascripts/application.js', :repo => repo
  172. copy_from_repo 'config/locales/pt-BR.yml', :repo => repo
  173. copy_from_repo 'app/assets/images/brasao-large.png', :repo => repo
  174. copy_from_repo 'app/assets/images/loading2.gif', :repo => repo
  175. copy_from_repo 'app/assets/images/loading.gif', :repo => repo