class Rails::Generators::ARGVScrubber

:nodoc:
since it configures and mutates ARGV correctly.
This class should be called before the AppGenerator is required and started
options).
requested, and also constructs the railsrc file (used for extra configuration
called. The class provides version or help information if they were
This class handles preparation of the arguments before the AppGenerator is

def self.default_rc_file

def self.default_rc_file
  xdg_config_home = ENV["XDG_CONFIG_HOME"].presence || "~/.config"
  xdg_railsrc = File.expand_path("rails/railsrc", xdg_config_home)
  if File.exist?(xdg_railsrc)
    xdg_railsrc
  else
    File.expand_path("~/.railsrc")
  end
end

def handle_invalid_command!(argument, argv)

def handle_invalid_command!(argument, argv)
  if argument == "new"
    yield
  else
    ["--help"] + argv.drop(1)
  end
end

def handle_rails_rc!(argv)

def handle_rails_rc!(argv)
  if argv.find { |arg| arg == "--no-rc" }
    argv.reject { |arg| arg == "--no-rc" }
  else
    railsrc(argv) { |rc_argv, rc| insert_railsrc_into_argv!(rc_argv, rc) }
  end
end

def handle_version_request!(argument)

def handle_version_request!(argument)
  if ["--version", "-v"].include?(argument)
    require "rails/version"
    puts "Rails #{Rails::VERSION::STRING}"
    exit(0)
  end
end

def initialize(argv = ARGV)

:nodoc:
since it configures and mutates ARGV correctly.
This class should be called before the AppGenerator is required and started

options).
requested, and also constructs the railsrc file (used for extra configuration
called. The class provides version or help information if they were
This class handles preparation of the arguments before the AppGenerator is
def initialize(argv = ARGV)
  @argv = argv
end

def insert_railsrc_into_argv!(argv, railsrc)

def insert_railsrc_into_argv!(argv, railsrc)
  return argv unless File.exist?(railsrc)
  extra_args = read_rc_file railsrc
  argv.take(1) + extra_args + argv.drop(1)
end

def prepare!

def prepare!
  handle_version_request!(@argv.first)
  handle_invalid_command!(@argv.first, @argv) do
    handle_rails_rc!(@argv.drop(1))
  end
end

def railsrc(argv)

def railsrc(argv)
  if (customrc = argv.index { |x| x.include?("--rc=") })
    fname = File.expand_path(argv[customrc].gsub(/--rc=/, ""))
    yield(argv.take(customrc) + argv.drop(customrc + 1), fname)
  else
    yield argv, self.class.default_rc_file
  end
end

def read_rc_file(railsrc)

def read_rc_file(railsrc)
  extra_args = File.readlines(railsrc).flat_map(&:split)
  puts "Using #{extra_args.join(" ")} from #{railsrc}"
  extra_args
end