/
environment_argument.rb
56 lines (44 loc) · 1.52 KB
/
environment_argument.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# frozen_string_literal: true
require "active_support"
require "active_support/core_ext/class/attribute"
module Rails
module Command
module EnvironmentArgument # :nodoc:
extend ActiveSupport::Concern
included do
class_option :environment, aliases: "-e", type: :string,
desc: "The environment to run `#{self.command_name}` in (e.g. test / development / production)."
end
def initialize(...)
super
@environment_specified = options[:environment].present?
if !@environment_specified
self.options = options.merge(environment: Rails::Command.environment)
elsif !available_environments.include?(options[:environment])
self.options = options.merge(environment: expand_environment_name(options[:environment]))
end
end
private
def require_application!
ENV["RAILS_ENV"] = environment
super
end
def environment
@environment ||= options[:environment]
end
def environment=(environment)
@environment = environment
end
def environment_specified?
@environment_specified
end
def available_environments
@available_environments ||=
Dir["config/environments/*.rb"].map { |filename| File.basename(filename, ".*") }
end
def expand_environment_name(name)
%w[production development test].find { |full_name| full_name.start_with?(name) } || name
end
end
end
end