2016-05-28 16:04:13 -04:00
|
|
|
require "active_support"
|
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Command
|
|
|
|
module EnvironmentArgument #:nodoc:
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
argument :environment, optional: true, banner: "environment"
|
2017-07-16 08:54:04 -04:00
|
|
|
|
|
|
|
class_option :environment, aliases: "-e", type: :string,
|
|
|
|
desc: "Specifies the environment to run this console under (test/development/production)."
|
2016-05-28 16:04:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def extract_environment_option_from_argument
|
|
|
|
if environment
|
|
|
|
self.options = options.merge(environment: acceptable_environment(environment))
|
2017-06-08 18:15:41 -04:00
|
|
|
|
|
|
|
ActiveSupport::Deprecation.warn "Passing the environment's name as a " \
|
|
|
|
"regular argument is deprecated and " \
|
|
|
|
"will be removed in the next Rails " \
|
|
|
|
"version. Please, use the -e option " \
|
|
|
|
"instead."
|
2017-07-16 08:54:04 -04:00
|
|
|
elsif options[:environment]
|
|
|
|
self.options = options.merge(environment: acceptable_environment(options[:environment]))
|
|
|
|
else
|
2016-09-15 15:56:03 -04:00
|
|
|
self.options = options.merge(environment: Rails::Command.environment)
|
2016-05-28 16:04:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def acceptable_environment(env = nil)
|
|
|
|
if available_environments.include? env
|
|
|
|
env
|
|
|
|
else
|
2016-09-07 16:45:41 -04:00
|
|
|
%w( production development test ).detect { |e| e =~ /^#{env}/ } || env
|
2016-05-28 16:04:13 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def available_environments
|
|
|
|
Dir["config/environments/*.rb"].map { |fname| File.basename(fname, ".*") }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|