lib/rails/generators/actions.rb in railties-5.1.7 vs lib/rails/generators/actions.rb in railties-5.2.0.beta1

- old
+ new

@@ -1,5 +1,7 @@ +# frozen_string_literal: true + module Rails module Generators module Actions def initialize(*) # :nodoc: super @@ -9,21 +11,26 @@ # Adds an entry into +Gemfile+ for the supplied gem. # # gem "rspec", group: :test # gem "technoweenie-restful-authentication", lib: "restful-authentication", source: "http://gems.github.com/" - # gem "rails", "3.0", git: "git://github.com/rails/rails" + # gem "rails", "3.0", git: "https://github.com/rails/rails" + # gem "RedCloth", ">= 4.1.0", "< 4.2.0" def gem(*args) options = args.extract_options! - name, version = args + name, *versions = args # Set the message to be shown in logs. Uses the git repo if one is given, # otherwise use name (version). parts, message = [ quote(name) ], name.dup - if version ||= options.delete(:version) - parts << quote(version) - message << " (#{version})" + + if versions = versions.any? ? versions : options.delete(:version) + _versions = Array(versions) + _versions.each do |version| + parts << quote(version) + end + message << " (#{_versions.join(", ")})" end message = options[:git] if options[:git] log :gemfile, message @@ -95,20 +102,20 @@ # # environment(nil, env: "development") do # "config.action_controller.asset_host = 'localhost:3000'" # end def environment(data = nil, options = {}) - sentinel = /class [a-z_:]+ < Rails::Application/i - env_file_sentinel = /Rails\.application\.configure do/ - data = yield if !data && block_given? + sentinel = "class Application < Rails::Application\n" + env_file_sentinel = "Rails.application.configure do\n" + data ||= yield if block_given? in_root do if options[:env].nil? - inject_into_file "config/application.rb", "\n #{data}", after: sentinel, verbose: false + inject_into_file "config/application.rb", optimize_indentation(data, 4), after: sentinel, verbose: false else Array(options[:env]).each do |env| - inject_into_file "config/environments/#{env}.rb", "\n #{data}", after: env_file_sentinel, verbose: false + inject_into_file "config/environments/#{env}.rb", optimize_indentation(data, 2), after: env_file_sentinel, verbose: false end end end end alias :application :environment @@ -135,26 +142,28 @@ # sekrit_salt = "#{Time.now}--#{3.years.ago}--#{rand}--" # "salt = '#{sekrit_salt}'" # end # # vendor("foreign.rb", "# Foreign code is fun") - def vendor(filename, data = nil, &block) + def vendor(filename, data = nil) log :vendor, filename - create_file("vendor/#{filename}", data, verbose: false, &block) + data ||= yield if block_given? + create_file("vendor/#{filename}", optimize_indentation(data), verbose: false) end - # Create a new file in the lib/ directory. Code can be specified + # Create a new file in the <tt>lib/</tt> directory. Code can be specified # in a block or a data string can be given. # # lib("crypto.rb") do # "crypted_special_value = '#{rand}--#{Time.now}--#{rand(1337)}--'" # end # # lib("foreign.rb", "# Foreign code is fun") - def lib(filename, data = nil, &block) + def lib(filename, data = nil) log :lib, filename - create_file("lib/#{filename}", data, verbose: false, &block) + data ||= yield if block_given? + create_file("lib/#{filename}", optimize_indentation(data), verbose: false) end # Create a new +Rakefile+ with the provided code (either in a block or a string). # # rakefile("bootstrap.rake") do @@ -168,13 +177,14 @@ # end # TASK # end # # rakefile('seed.rake', 'puts "Planting seeds"') - def rakefile(filename, data = nil, &block) + def rakefile(filename, data = nil) log :rakefile, filename - create_file("lib/tasks/#{filename}", data, verbose: false, &block) + data ||= yield if block_given? + create_file("lib/tasks/#{filename}", optimize_indentation(data), verbose: false) end # Create a new initializer with the provided code (either in a block or a string). # # initializer("globals.rb") do @@ -186,13 +196,14 @@ # # data # end # # initializer("api.rb", "API_KEY = '123456'") - def initializer(filename, data = nil, &block) + def initializer(filename, data = nil) log :initializer, filename - create_file("config/initializers/#{filename}", data, verbose: false, &block) + data ||= yield if block_given? + create_file("config/initializers/#{filename}", optimize_indentation(data), verbose: false) end # Generate something using a generator from Rails or a plugin. # The second parameter is the argument string that is passed to # the generator or an Array that is joined. @@ -208,27 +219,30 @@ # Runs the supplied rake task (invoked with 'rake ...') # # rake("db:migrate") # rake("db:migrate", env: "production") # rake("gems:install", sudo: true) + # rake("gems:install", capture: true) def rake(command, options = {}) execute_command :rake, command, options end # Runs the supplied rake task (invoked with 'rails ...') # - # rails("db:migrate") - # rails("db:migrate", env: "production") - # rails("gems:install", sudo: true) + # rails_command("db:migrate") + # rails_command("db:migrate", env: "production") + # rails_command("gems:install", sudo: true) + # rails_command("gems:install", capture: true) def rails_command(command, options = {}) execute_command :rails, command, options end # Just run the capify command in root # # capify! def capify! + ActiveSupport::Deprecation.warn("`capify!` is deprecated and will be removed in the next version of Rails.") log :capify, "" in_root { run("#{extify(:capify)} .", verbose: false) } end # Make an entry in Rails routing file <tt>config/routes.rb</tt> @@ -237,11 +251,11 @@ def route(routing_code) log :route, routing_code sentinel = /\.routes\.draw do\s*\n/m in_root do - inject_into_file "config/routes.rb", " #{routing_code}\n", after: sentinel, verbose: false, force: false + inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false end end # Reads the given file at the source root and prints it in the console. # @@ -278,11 +292,15 @@ # based on the executor parameter provided. def execute_command(executor, command, options = {}) # :doc: log executor, command env = options[:env] || ENV["RAILS_ENV"] || "development" sudo = options[:sudo] && !Gem.win_platform? ? "sudo " : "" - in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", verbose: false) } + config = { verbose: false } + + config.merge!(capture: options[:capture]) if options[:capture] + + in_root { run("#{sudo}#{extify(executor)} #{command} RAILS_ENV=#{env}", config) } end # Add an extension to the given name based on the platform. def extify(name) # :doc: if Gem.win_platform? @@ -299,9 +317,20 @@ if value.include?("'") value.inspect else "'#{value}'" + end + end + + # Returns optimized string with indentation + def optimize_indentation(value, amount = 0) # :doc: + return "#{value}\n" unless value.is_a?(String) + + if value.lines.size > 1 + value.strip_heredoc.indent(amount) + else + "#{value.strip.indent(amount)}\n" end end end end end