1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/lib/rails/command/environment_argument.rb

41 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-05-28 16:04:13 -04:00
require "active_support"
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
no_commands do
class_attribute :environment_desc, default: "Specifies the environment to run this #{self.command_name} under (test/development/production)."
end
class_option :environment, aliases: "-e", type: :string, desc: environment_desc
2016-05-28 16:04:13 -04:00
end
private
def extract_environment_option_from_argument(default_environment: Rails::Command.environment)
if options[:environment]
self.options = options.merge(environment: acceptable_environment(options[:environment]))
else
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
%w( production development test ).detect { |e| /^#{env}/.match?(e) } || 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