lib/rails/generators/actions.rb in railties-6.0.6.1 vs lib/rails/generators/actions.rb in railties-6.1.0.rc1
- old
+ new
@@ -1,7 +1,9 @@
# frozen_string_literal: true
+require "shellwords"
+require "active_support/core_ext/kernel/reporting"
require "active_support/core_ext/string/strip"
module Rails
module Generators
module Actions
@@ -38,12 +40,11 @@
parts << quote(options) unless options.empty?
in_root do
str = "gem #{parts.join(", ")}"
str = indentation + str
- str = "\n" + str
- append_file "Gemfile", str, verbose: false
+ append_file_with_newline "Gemfile", str, verbose: false
end
end
# Wraps gem entries inside a group.
#
@@ -56,26 +57,30 @@
str << quote(options) unless options.empty?
str = str.join(", ")
log :gemfile, "group #{str}"
in_root do
- append_file "Gemfile", "\ngroup #{str} do", force: true
+ append_file_with_newline "Gemfile", "\ngroup #{str} do", force: true
with_indentation(&block)
- append_file "Gemfile", "\nend\n", force: true
+ append_file_with_newline "Gemfile", "end", force: true
end
end
def github(repo, options = {}, &block)
str = [quote(repo)]
str << quote(options) unless options.empty?
str = str.join(", ")
log :github, "github #{str}"
in_root do
- append_file "Gemfile", "\n#{indentation}github #{str} do", force: true
+ if @indentation.zero?
+ append_file_with_newline "Gemfile", "\ngithub #{str} do", force: true
+ else
+ append_file_with_newline "Gemfile", "#{indentation}github #{str} do", force: true
+ end
with_indentation(&block)
- append_file "Gemfile", "\n#{indentation}end", force: true
+ append_file_with_newline "Gemfile", "#{indentation}end", force: true
end
end
# Add the given source to +Gemfile+
#
@@ -89,13 +94,13 @@
def add_source(source, options = {}, &block)
log :source, source
in_root do
if block
- append_file "Gemfile", "\nsource #{quote(source)} do", force: true
+ append_file_with_newline "Gemfile", "\nsource #{quote(source)} do", force: true
with_indentation(&block)
- append_file "Gemfile", "\nend\n", force: true
+ append_file_with_newline "Gemfile", "end", force: true
else
prepend_file "Gemfile", "source #{quote(source)}\n", verbose: false
end
end
end
@@ -220,14 +225,13 @@
# generate(:authenticated, "user session")
def generate(what, *args)
log :generate, what
options = args.extract_options!
- options[:without_rails_env] = true
- argument = args.flat_map(&:to_s).join(" ")
+ options[:abort_on_failure] = !options[:inline]
- execute_command :rails, "generate #{what} #{argument}", options
+ rails_command "generate #{what} #{args.join(" ")}", options
end
# Runs the supplied rake task (invoked with 'rake ...')
#
# rake("db:migrate")
@@ -243,17 +247,32 @@
# 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
+ if options[:inline]
+ log :rails, command
+ command, *args = Shellwords.split(command)
+ in_root do
+ silence_warnings do
+ ::Rails::Command.invoke(command, args, **options)
+ end
+ end
+ else
+ execute_command :rails, command, options
+ end
end
# Make an entry in Rails routing file <tt>config/routes.rb</tt>
#
# route "root 'welcome#index'"
- def route(routing_code)
+ # route "root 'admin#index'", namespace: :admin
+ def route(routing_code, namespace: nil)
+ routing_code = Array(namespace).reverse.reduce(routing_code) do |code, ns|
+ "namespace :#{ns} do\n#{indent(code, 2)}\nend"
+ end
+
log :route, routing_code
sentinel = /\.routes\.draw do\s*\n/m
in_root do
inject_into_file "config/routes.rb", optimize_indentation(routing_code, 2), after: sentinel, verbose: false, force: false
@@ -282,19 +301,19 @@
# Runs the supplied command using either "rake ..." or "rails ..."
# based on the executor parameter provided.
def execute_command(executor, command, options = {}) # :doc:
log executor, command
- env = options[:env] || ENV["RAILS_ENV"] || "development"
- rails_env = " RAILS_ENV=#{env}" unless options[:without_rails_env]
sudo = options[:sudo] && !Gem.win_platform? ? "sudo " : ""
- config = { verbose: false }
+ config = {
+ env: { "RAILS_ENV" => (options[:env] || ENV["RAILS_ENV"] || "development") },
+ verbose: false,
+ capture: options[:capture],
+ abort_on_failure: options[:abort_on_failure],
+ }
- config[:capture] = options[:capture] if options[:capture]
- config[:abort_on_failure] = options[:abort_on_failure] if options[:abort_on_failure]
-
- in_root { run("#{sudo}#{extify(executor)} #{command}#{rails_env}", config) }
+ in_root { run("#{sudo}#{extify(executor)} #{command}", config) }
end
# Add an extension to the given name based on the platform.
def extify(name) # :doc:
if Gem.win_platform?
@@ -322,16 +341,11 @@
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
+ "#{value.strip_heredoc.indent(amount).chomp}\n"
end
# Indent the +Gemfile+ to the depth of @indentation
def indentation # :doc:
" " * @indentation
@@ -341,9 +355,16 @@
def with_indentation(&block) # :doc:
@indentation += 1
instance_eval(&block)
ensure
@indentation -= 1
+ end
+
+ # Append string to a file with a newline if necessary
+ def append_file_with_newline(path, str, options = {})
+ gsub_file path, /\n?\z/, options do |match|
+ match.end_with?("\n") ? "" : "\n#{str}\n"
+ end
end
end
end
end