123456789_123456789_123456789_123456789_123456789_

Class: Rails::Generators::ARGVScrubber

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: railties/lib/rails/generators/rails/app/app_generator.rb

Overview

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

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

Class Method Summary

Instance Method Summary

Constructor Details

.new(argv = ARGV) ⇒ ARGVScrubber

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 612

def initialize(argv = ARGV)
  @argv = argv
end

Class Method Details

.default_rc_file

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 623

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

Instance Method Details

#handle_invalid_command!(argument, argv) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 642

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

#handle_rails_rc!(argv) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 650

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

#handle_version_request!(argument) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 634

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

#insert_railsrc_into_argv!(argv, railsrc) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 673

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

#prepare!

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 616

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

#railsrc(argv) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 658

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

#read_rc_file(railsrc) (private)

[ GitHub ]

  
# File 'railties/lib/rails/generators/rails/app/app_generator.rb', line 667

def read_rc_file(railsrc)
  extra_args = File.readlines(railsrc).flat_map.each { |line| line.split("#", 2).first.split }
  puts "Using #{extra_args.join(" ")} from #{railsrc}"
  extra_args
end