PageRenderTime 40ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/bundle/gems/railties-4.2.6/lib/rails/generators/app_base.rb

https://gitlab.com/Renatafsouza/EP3OO
Ruby | 365 lines | 309 code | 43 blank | 13 comment | 18 complexity | d9b79610dbb1c3474dd602684537370b MD5 | raw file
  1. require 'digest/md5'
  2. require 'active_support/core_ext/string/strip'
  3. require 'rails/version' unless defined?(Rails::VERSION)
  4. require 'open-uri'
  5. require 'uri'
  6. require 'rails/generators'
  7. require 'active_support/core_ext/array/extract_options'
  8. module Rails
  9. module Generators
  10. class AppBase < Base # :nodoc:
  11. DATABASES = %w( mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver )
  12. JDBC_DATABASES = %w( jdbcmysql jdbcsqlite3 jdbcpostgresql jdbc )
  13. DATABASES.concat(JDBC_DATABASES)
  14. attr_accessor :rails_template
  15. add_shebang_option!
  16. argument :app_path, type: :string
  17. def self.strict_args_position
  18. false
  19. end
  20. def self.add_shared_options_for(name)
  21. class_option :template, type: :string, aliases: '-m',
  22. desc: "Path to some #{name} template (can be a filesystem path or URL)"
  23. class_option :skip_gemfile, type: :boolean, default: false,
  24. desc: "Don't create a Gemfile"
  25. class_option :skip_bundle, type: :boolean, aliases: '-B', default: false,
  26. desc: "Don't run bundle install"
  27. class_option :skip_git, type: :boolean, aliases: '-G', default: false,
  28. desc: 'Skip .gitignore file'
  29. class_option :skip_keeps, type: :boolean, default: false,
  30. desc: 'Skip source control .keep files'
  31. class_option :skip_active_record, type: :boolean, aliases: '-O', default: false,
  32. desc: 'Skip Active Record files'
  33. class_option :skip_sprockets, type: :boolean, aliases: '-S', default: false,
  34. desc: 'Skip Sprockets files'
  35. class_option :skip_spring, type: :boolean, default: false,
  36. desc: "Don't install Spring application preloader"
  37. class_option :database, type: :string, aliases: '-d', default: 'sqlite3',
  38. desc: "Preconfigure for selected database (options: #{DATABASES.join('/')})"
  39. class_option :javascript, type: :string, aliases: '-j', default: 'jquery',
  40. desc: 'Preconfigure for selected JavaScript library'
  41. class_option :skip_javascript, type: :boolean, aliases: '-J', default: false,
  42. desc: 'Skip JavaScript files'
  43. class_option :dev, type: :boolean, default: false,
  44. desc: "Setup the #{name} with Gemfile pointing to your Rails checkout"
  45. class_option :edge, type: :boolean, default: false,
  46. desc: "Setup the #{name} with Gemfile pointing to Rails repository"
  47. class_option :skip_turbolinks, type: :boolean, default: false,
  48. desc: 'Skip turbolinks gem'
  49. class_option :skip_test_unit, type: :boolean, aliases: '-T', default: false,
  50. desc: 'Skip Test::Unit files'
  51. class_option :rc, type: :string, default: false,
  52. desc: "Path to file containing extra configuration options for rails command"
  53. class_option :no_rc, type: :boolean, default: false,
  54. desc: 'Skip loading of extra configuration options from .railsrc file'
  55. class_option :help, type: :boolean, aliases: '-h', group: :rails,
  56. desc: 'Show this help message and quit'
  57. end
  58. def initialize(*args)
  59. @gem_filter = lambda { |gem| true }
  60. @extra_entries = []
  61. super
  62. convert_database_option_for_jruby
  63. end
  64. protected
  65. def gemfile_entry(name, *args)
  66. options = args.extract_options!
  67. version = args.first
  68. github = options[:github]
  69. path = options[:path]
  70. if github
  71. @extra_entries << GemfileEntry.github(name, github)
  72. elsif path
  73. @extra_entries << GemfileEntry.path(name, path)
  74. else
  75. @extra_entries << GemfileEntry.version(name, version)
  76. end
  77. self
  78. end
  79. def gemfile_entries
  80. [rails_gemfile_entry,
  81. database_gemfile_entry,
  82. assets_gemfile_entry,
  83. javascript_gemfile_entry,
  84. jbuilder_gemfile_entry,
  85. sdoc_gemfile_entry,
  86. psych_gemfile_entry,
  87. @extra_entries].flatten.find_all(&@gem_filter)
  88. end
  89. def add_gem_entry_filter
  90. @gem_filter = lambda { |next_filter, entry|
  91. yield(entry) && next_filter.call(entry)
  92. }.curry[@gem_filter]
  93. end
  94. def builder
  95. @builder ||= begin
  96. builder_class = get_builder_class
  97. builder_class.send(:include, ActionMethods)
  98. builder_class.new(self)
  99. end
  100. end
  101. def build(meth, *args)
  102. builder.send(meth, *args) if builder.respond_to?(meth)
  103. end
  104. def create_root
  105. valid_const?
  106. empty_directory '.'
  107. FileUtils.cd(destination_root) unless options[:pretend]
  108. end
  109. def apply_rails_template
  110. apply rails_template if rails_template
  111. rescue Thor::Error, LoadError, Errno::ENOENT => e
  112. raise Error, "The template [#{rails_template}] could not be loaded. Error: #{e}"
  113. end
  114. def set_default_accessors!
  115. self.destination_root = File.expand_path(app_path, destination_root)
  116. self.rails_template = case options[:template]
  117. when /^https?:\/\//
  118. options[:template]
  119. when String
  120. File.expand_path(options[:template], Dir.pwd)
  121. else
  122. options[:template]
  123. end
  124. end
  125. def database_gemfile_entry
  126. return [] if options[:skip_active_record]
  127. gem_name, gem_version = gem_for_database
  128. GemfileEntry.version gem_name, gem_version,
  129. "Use #{options[:database]} as the database for Active Record"
  130. end
  131. def include_all_railties?
  132. !options[:skip_active_record] && !options[:skip_test_unit] && !options[:skip_sprockets]
  133. end
  134. def comment_if(value)
  135. options[value] ? '# ' : ''
  136. end
  137. def sqlite3?
  138. !options[:skip_active_record] && options[:database] == 'sqlite3'
  139. end
  140. class GemfileEntry < Struct.new(:name, :version, :comment, :options, :commented_out)
  141. def initialize(name, version, comment, options = {}, commented_out = false)
  142. super
  143. end
  144. def self.github(name, github, branch = nil, comment = nil)
  145. if branch
  146. new(name, nil, comment, github: github, branch: branch)
  147. else
  148. new(name, nil, comment, github: github)
  149. end
  150. end
  151. def self.version(name, version, comment = nil)
  152. new(name, version, comment)
  153. end
  154. def self.path(name, path, comment = nil)
  155. new(name, nil, comment, path: path)
  156. end
  157. def version
  158. version = super
  159. if version.is_a?(Array)
  160. version.join("', '")
  161. else
  162. version
  163. end
  164. end
  165. end
  166. def rails_gemfile_entry
  167. if options.dev?
  168. [GemfileEntry.path('rails', Rails::Generators::RAILS_DEV_PATH)]
  169. elsif options.edge?
  170. [GemfileEntry.github('rails', 'rails/rails', '4-2-stable')]
  171. else
  172. [GemfileEntry.version('rails',
  173. Rails::VERSION::STRING,
  174. "Bundle edge Rails instead: gem 'rails', github: 'rails/rails'")]
  175. end
  176. end
  177. def gem_for_database
  178. # %w( mysql oracle postgresql sqlite3 frontbase ibm_db sqlserver jdbcmysql jdbcsqlite3 jdbcpostgresql )
  179. case options[:database]
  180. when "oracle" then ["ruby-oci8", nil]
  181. when "postgresql" then ["pg", ["~> 0.15"]]
  182. when "frontbase" then ["ruby-frontbase", nil]
  183. when "mysql" then ["mysql2", [">= 0.3.13", "< 0.5"]]
  184. when "sqlserver" then ["activerecord-sqlserver-adapter", nil]
  185. when "jdbcmysql" then ["activerecord-jdbcmysql-adapter", nil]
  186. when "jdbcsqlite3" then ["activerecord-jdbcsqlite3-adapter", nil]
  187. when "jdbcpostgresql" then ["activerecord-jdbcpostgresql-adapter", nil]
  188. when "jdbc" then ["activerecord-jdbc-adapter", nil]
  189. else [options[:database], nil]
  190. end
  191. end
  192. def convert_database_option_for_jruby
  193. if defined?(JRUBY_VERSION)
  194. case options[:database]
  195. when "oracle" then options[:database].replace "jdbc"
  196. when "postgresql" then options[:database].replace "jdbcpostgresql"
  197. when "mysql" then options[:database].replace "jdbcmysql"
  198. when "sqlite3" then options[:database].replace "jdbcsqlite3"
  199. end
  200. end
  201. end
  202. def assets_gemfile_entry
  203. return [] if options[:skip_sprockets]
  204. gems = []
  205. gems << GemfileEntry.version('sass-rails', '~> 5.0',
  206. 'Use SCSS for stylesheets')
  207. gems << GemfileEntry.version('uglifier',
  208. '>= 1.3.0',
  209. 'Use Uglifier as compressor for JavaScript assets')
  210. gems
  211. end
  212. def jbuilder_gemfile_entry
  213. comment = 'Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder'
  214. GemfileEntry.version('jbuilder', '~> 2.0', comment)
  215. end
  216. def sdoc_gemfile_entry
  217. comment = 'bundle exec rake doc:rails generates the API under doc/api.'
  218. GemfileEntry.new('sdoc', '~> 0.4.0', comment, group: :doc)
  219. end
  220. def coffee_gemfile_entry
  221. comment = 'Use CoffeeScript for .coffee assets and views'
  222. GemfileEntry.version 'coffee-rails', '~> 4.1.0', comment
  223. end
  224. def javascript_gemfile_entry
  225. if options[:skip_javascript]
  226. []
  227. else
  228. gems = [coffee_gemfile_entry, javascript_runtime_gemfile_entry]
  229. gems << GemfileEntry.version("#{options[:javascript]}-rails", nil,
  230. "Use #{options[:javascript]} as the JavaScript library")
  231. unless options[:skip_turbolinks]
  232. gems << GemfileEntry.version("turbolinks", nil,
  233. "Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks")
  234. end
  235. gems
  236. end
  237. end
  238. def javascript_runtime_gemfile_entry
  239. comment = 'See https://github.com/rails/execjs#readme for more supported runtimes'
  240. if defined?(JRUBY_VERSION)
  241. GemfileEntry.version 'therubyrhino', nil, comment
  242. else
  243. GemfileEntry.new 'therubyracer', nil, comment, { platforms: :ruby }, true
  244. end
  245. end
  246. def psych_gemfile_entry
  247. return [] unless defined?(Rubinius)
  248. comment = 'Use Psych as the YAML engine, instead of Syck, so serialized ' \
  249. 'data can be read safely from different rubies (see http://git.io/uuLVag)'
  250. GemfileEntry.new('psych', '~> 2.0', comment, platforms: :rbx)
  251. end
  252. def bundle_command(command)
  253. say_status :run, "bundle #{command}"
  254. # We are going to shell out rather than invoking Bundler::CLI.new(command)
  255. # because `rails new` loads the Thor gem and on the other hand bundler uses
  256. # its own vendored Thor, which could be a different version. Running both
  257. # things in the same process is a recipe for a night with paracetamol.
  258. #
  259. # We use backticks and #print here instead of vanilla #system because it
  260. # is easier to silence stdout in the existing test suite this way. The
  261. # end-user gets the bundler commands called anyway, so no big deal.
  262. #
  263. # We unset temporary bundler variables to load proper bundler and Gemfile.
  264. #
  265. # Thanks to James Tucker for the Gem tricks involved in this call.
  266. _bundle_command = Gem.bin_path('bundler', 'bundle')
  267. require 'bundler'
  268. Bundler.with_clean_env do
  269. output = `"#{Gem.ruby}" "#{_bundle_command}" #{command}`
  270. print output unless options[:quiet]
  271. end
  272. end
  273. def bundle_install?
  274. !(options[:skip_gemfile] || options[:skip_bundle] || options[:pretend])
  275. end
  276. def spring_install?
  277. !options[:skip_spring] && Process.respond_to?(:fork) && !RUBY_PLATFORM.include?("cygwin")
  278. end
  279. def run_bundle
  280. bundle_command('install') if bundle_install?
  281. end
  282. def generate_spring_binstubs
  283. if bundle_install? && spring_install?
  284. bundle_command("exec spring binstub --all")
  285. end
  286. end
  287. def empty_directory_with_keep_file(destination, config = {})
  288. empty_directory(destination, config)
  289. keep_file(destination)
  290. end
  291. def keep_file(destination)
  292. create_file("#{destination}/.keep") unless options[:skip_keeps]
  293. end
  294. end
  295. end
  296. end