2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-05-28 16:04:13 -04:00
|
|
|
require "active_support"
|
2019-03-03 18:12:05 -05:00
|
|
|
require "active_support/core_ext/class/attribute"
|
2016-05-28 16:04:13 -04:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Command
|
|
|
|
module EnvironmentArgument #:nodoc:
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2019-04-12 19:37:00 -04:00
|
|
|
no_commands do
|
|
|
|
class_attribute :environment_desc, default: "Specifies the environment to run this #{self.command_name} under (test/development/production)."
|
|
|
|
end
|
2019-03-03 18:12:05 -05:00
|
|
|
class_option :environment, aliases: "-e", type: :string, desc: environment_desc
|
2016-05-28 16:04:13 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2019-03-03 18:12:05 -05:00
|
|
|
def extract_environment_option_from_argument(default_environment: Rails::Command.environment)
|
2019-01-16 20:47:39 -05:00
|
|
|
if options[:environment]
|
2017-07-16 08:54:04 -04:00
|
|
|
self.options = options.merge(environment: acceptable_environment(options[:environment]))
|
|
|
|
else
|
2019-03-03 18:12:05 -05:00
|
|
|
self.options = options.merge(environment: default_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
|