railties 5.2.4 → 6.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (159) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +284 -130
  3. data/MIT-LICENSE +1 -1
  4. data/RDOC_MAIN.rdoc +38 -31
  5. data/README.rdoc +2 -2
  6. data/lib/minitest/rails_plugin.rb +7 -11
  7. data/lib/rails.rb +5 -0
  8. data/lib/rails/all.rb +4 -0
  9. data/lib/rails/api/generator.rb +2 -1
  10. data/lib/rails/api/task.rb +16 -0
  11. data/lib/rails/app_loader.rb +2 -2
  12. data/lib/rails/app_updater.rb +3 -1
  13. data/lib/rails/application.rb +71 -29
  14. data/lib/rails/application/bootstrap.rb +2 -10
  15. data/lib/rails/application/configuration.rb +113 -13
  16. data/lib/rails/application/default_middleware_stack.rb +3 -0
  17. data/lib/rails/application/dummy_erb_compiler.rb +18 -0
  18. data/lib/rails/application/finisher.rb +54 -0
  19. data/lib/rails/autoloaders.rb +48 -0
  20. data/lib/rails/backtrace_cleaner.rb +5 -17
  21. data/lib/rails/code_statistics.rb +3 -3
  22. data/lib/rails/command.rb +11 -10
  23. data/lib/rails/command/base.rb +12 -8
  24. data/lib/rails/command/behavior.rb +7 -48
  25. data/lib/rails/command/environment_argument.rb +8 -15
  26. data/lib/rails/command/spellchecker.rb +58 -0
  27. data/lib/rails/commands/console/console_command.rb +6 -0
  28. data/lib/rails/commands/credentials/USAGE +19 -1
  29. data/lib/rails/commands/credentials/credentials_command.rb +52 -19
  30. data/lib/rails/commands/db/system/change/change_command.rb +20 -0
  31. data/lib/rails/commands/dbconsole/dbconsole_command.rb +20 -8
  32. data/lib/rails/commands/dev/dev_command.rb +19 -0
  33. data/lib/rails/commands/encrypted/USAGE +1 -1
  34. data/lib/rails/commands/encrypted/encrypted_command.rb +2 -2
  35. data/lib/rails/commands/help/help_command.rb +1 -1
  36. data/lib/rails/commands/initializers/initializers_command.rb +23 -0
  37. data/lib/rails/commands/new/new_command.rb +2 -2
  38. data/lib/rails/commands/notes/notes_command.rb +39 -0
  39. data/lib/rails/commands/plugin/plugin_command.rb +1 -1
  40. data/lib/rails/commands/routes/routes_command.rb +37 -0
  41. data/lib/rails/commands/runner/runner_command.rb +13 -9
  42. data/lib/rails/commands/secrets/USAGE +3 -3
  43. data/lib/rails/commands/secrets/secrets_command.rb +3 -3
  44. data/lib/rails/commands/server/server_command.rb +113 -50
  45. data/lib/rails/configuration.rb +1 -7
  46. data/lib/rails/engine.rb +24 -16
  47. data/lib/rails/engine/configuration.rb +5 -2
  48. data/lib/rails/gem_version.rb +3 -3
  49. data/lib/rails/generators.rb +11 -10
  50. data/lib/rails/generators/actions.rb +52 -39
  51. data/lib/rails/generators/app_base.rb +52 -92
  52. data/lib/rails/generators/app_name.rb +50 -0
  53. data/lib/rails/generators/base.rb +0 -4
  54. data/lib/rails/generators/database.rb +58 -0
  55. data/lib/rails/generators/erb/mailer/mailer_generator.rb +1 -1
  56. data/lib/rails/generators/erb/scaffold/templates/_form.html.erb.tt +6 -3
  57. data/lib/rails/generators/erb/scaffold/templates/index.html.erb.tt +1 -1
  58. data/lib/rails/generators/erb/scaffold/templates/show.html.erb.tt +9 -1
  59. data/lib/rails/generators/generated_attribute.rb +53 -27
  60. data/lib/rails/generators/migration.rb +1 -2
  61. data/lib/rails/generators/model_helpers.rb +8 -1
  62. data/lib/rails/generators/named_base.rb +1 -5
  63. data/lib/rails/generators/rails/app/app_generator.rb +37 -72
  64. data/lib/rails/generators/rails/app/templates/Gemfile.tt +7 -10
  65. data/lib/rails/generators/rails/app/templates/app/assets/config/manifest.js.tt +0 -3
  66. data/lib/rails/generators/rails/app/templates/app/{assets/javascripts/cable.js.tt → javascript/channels/consumer.js} +2 -9
  67. data/lib/rails/generators/rails/app/templates/app/javascript/channels/index.js +5 -0
  68. data/lib/rails/generators/rails/app/templates/app/javascript/packs/application.js.tt +23 -0
  69. data/lib/rails/generators/rails/app/templates/app/jobs/application_job.rb.tt +5 -0
  70. data/lib/rails/generators/rails/app/templates/app/views/layouts/application.html.erb.tt +4 -4
  71. data/lib/rails/generators/rails/app/templates/bin/setup.tt +7 -7
  72. data/lib/rails/generators/rails/app/templates/config/application.rb.tt +2 -0
  73. data/lib/rails/generators/rails/app/templates/config/cable.yml.tt +1 -1
  74. data/lib/rails/generators/rails/app/templates/config/databases/frontbase.yml.tt +2 -2
  75. data/lib/rails/generators/rails/app/templates/config/databases/ibm_db.yml.tt +2 -2
  76. data/lib/rails/generators/rails/app/templates/config/databases/jdbc.yml.tt +2 -2
  77. data/lib/rails/generators/rails/app/templates/config/databases/jdbcmysql.yml.tt +3 -3
  78. data/lib/rails/generators/rails/app/templates/config/databases/jdbcpostgresql.yml.tt +3 -3
  79. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml.tt +1 -1
  80. data/lib/rails/generators/rails/app/templates/config/databases/mysql.yml.tt +4 -4
  81. data/lib/rails/generators/rails/app/templates/config/databases/oracle.yml.tt +2 -2
  82. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt +6 -6
  83. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt +1 -1
  84. data/lib/rails/generators/rails/app/templates/config/databases/sqlserver.yml.tt +2 -2
  85. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +5 -2
  86. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +28 -12
  87. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +13 -7
  88. data/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +1 -1
  89. data/lib/rails/generators/rails/app/templates/config/initializers/content_security_policy.rb.tt +7 -0
  90. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_6_0.rb.tt +45 -0
  91. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +1 -1
  92. data/lib/rails/generators/rails/app/templates/config/puma.rb.tt +4 -3
  93. data/lib/rails/generators/rails/app/templates/config/routes.rb.tt +1 -1
  94. data/lib/rails/generators/rails/app/templates/config/spring.rb.tt +6 -6
  95. data/lib/rails/generators/rails/app/templates/gitignore.tt +2 -7
  96. data/lib/rails/generators/rails/app/templates/package.json.tt +7 -1
  97. data/lib/rails/generators/rails/app/templates/public/robots.txt +1 -1
  98. data/lib/rails/generators/rails/app/templates/ruby-version.tt +1 -1
  99. data/lib/rails/generators/rails/app/templates/test/channels/application_cable/connection_test.rb.tt +11 -0
  100. data/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +7 -0
  101. data/lib/rails/generators/rails/assets/USAGE +1 -4
  102. data/lib/rails/generators/rails/assets/assets_generator.rb +0 -1
  103. data/lib/rails/generators/rails/controller/controller_generator.rb +11 -1
  104. data/lib/rails/generators/rails/credentials/credentials_generator.rb +7 -8
  105. data/lib/rails/generators/rails/db/system/change/change_generator.rb +65 -0
  106. data/lib/rails/generators/rails/encrypted_file/encrypted_file_generator.rb +4 -5
  107. data/lib/rails/generators/rails/helper/helper_generator.rb +5 -0
  108. data/lib/rails/generators/rails/plugin/plugin_generator.rb +9 -33
  109. data/lib/rails/generators/rails/plugin/templates/app/controllers/%namespaced_name%/application_controller.rb.tt +1 -1
  110. data/lib/rails/generators/rails/plugin/templates/app/helpers/%namespaced_name%/application_helper.rb.tt +1 -1
  111. data/lib/rails/generators/rails/plugin/templates/app/jobs/%namespaced_name%/application_job.rb.tt +1 -1
  112. data/lib/rails/generators/rails/plugin/templates/app/mailers/%namespaced_name%/application_mailer.rb.tt +1 -1
  113. data/lib/rails/generators/rails/plugin/templates/app/models/%namespaced_name%/application_record.rb.tt +1 -1
  114. data/lib/rails/generators/rails/plugin/templates/gitignore.tt +1 -1
  115. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/engine.rb.tt +1 -1
  116. data/lib/rails/generators/rails/plugin/templates/lib/%namespaced_name%/railtie.rb.tt +1 -1
  117. data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +1 -2
  118. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +14 -0
  119. data/lib/rails/generators/rails/scaffold_controller/templates/api_controller.rb.tt +1 -1
  120. data/lib/rails/generators/rails/scaffold_controller/templates/controller.rb.tt +1 -1
  121. data/lib/rails/generators/resource_helpers.rb +1 -6
  122. data/lib/rails/generators/test_unit/integration/integration_generator.rb +6 -0
  123. data/lib/rails/generators/test_unit/job/job_generator.rb +5 -0
  124. data/lib/rails/generators/test_unit/mailer/mailer_generator.rb +1 -1
  125. data/lib/rails/generators/test_unit/model/templates/fixtures.yml.tt +2 -2
  126. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +8 -3
  127. data/lib/rails/generators/test_unit/system/system_generator.rb +5 -0
  128. data/lib/rails/generators/testing/behaviour.rb +3 -0
  129. data/lib/rails/info.rb +2 -2
  130. data/lib/rails/info_controller.rb +1 -1
  131. data/lib/rails/mailers_controller.rb +7 -4
  132. data/lib/rails/paths.rb +19 -9
  133. data/lib/rails/railtie.rb +1 -1
  134. data/lib/rails/ruby_version_check.rb +3 -3
  135. data/lib/rails/secrets.rb +0 -1
  136. data/lib/rails/source_annotation_extractor.rb +138 -117
  137. data/lib/rails/tasks.rb +1 -0
  138. data/lib/rails/tasks/annotations.rake +9 -9
  139. data/lib/rails/tasks/dev.rake +5 -4
  140. data/lib/rails/tasks/framework.rake +5 -1
  141. data/lib/rails/tasks/initializers.rake +5 -4
  142. data/lib/rails/tasks/log.rake +0 -1
  143. data/lib/rails/tasks/routes.rake +4 -26
  144. data/lib/rails/tasks/statistics.rake +4 -0
  145. data/lib/rails/tasks/yarn.rake +1 -1
  146. data/lib/rails/tasks/zeitwerk.rake +66 -0
  147. data/lib/rails/templates/rails/welcome/index.html.erb +2 -2
  148. data/lib/rails/test_help.rb +11 -9
  149. data/lib/rails/test_unit/reporter.rb +1 -1
  150. data/lib/rails/test_unit/runner.rb +5 -5
  151. data/lib/rails/test_unit/testing.rake +1 -1
  152. metadata +32 -23
  153. data/lib/rails/generators/js/assets/assets_generator.rb +0 -15
  154. data/lib/rails/generators/js/assets/templates/javascript.js +0 -2
  155. data/lib/rails/generators/rails/app/templates/app/assets/javascripts/application.js.tt +0 -22
  156. data/lib/rails/generators/rails/app/templates/bin/bundle.tt +0 -2
  157. data/lib/rails/generators/rails/app/templates/bin/update.tt +0 -34
  158. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_5_2.rb.tt +0 -38
  159. data/lib/rails/generators/rails/assets/templates/javascript.js +0 -2
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support/deprecation"
4
+ require "active_support/core_ext/string/filters"
3
5
  require "rails/command/environment_argument"
4
6
 
5
7
  module Rails
@@ -75,7 +77,7 @@ module Rails
75
77
  args += ["-P", "#{config['password']}"] if config["password"]
76
78
 
77
79
  if config["host"]
78
- host_arg = "#{config['host']}".dup
80
+ host_arg = +"#{config['host']}"
79
81
  host_arg << ":#{config['port']}" if config["port"]
80
82
  args += ["-S", host_arg]
81
83
  end
@@ -89,15 +91,15 @@ module Rails
89
91
 
90
92
  def config
91
93
  @config ||= begin
92
- # We need to check whether the user passed the connection the
94
+ # We need to check whether the user passed the database the
93
95
  # first time around to show a consistent error message to people
94
96
  # relying on 2-level database configuration.
95
- if @options["connection"] && configurations[connection].blank?
96
- raise ActiveRecord::AdapterNotSpecified, "'#{connection}' connection is not configured. Available configuration: #{configurations.inspect}"
97
- elsif configurations[environment].blank? && configurations[connection].blank?
97
+ if @options["database"] && configurations[database].blank?
98
+ raise ActiveRecord::AdapterNotSpecified, "'#{database}' database is not configured. Available configuration: #{configurations.inspect}"
99
+ elsif configurations[environment].blank? && configurations[database].blank?
98
100
  raise ActiveRecord::AdapterNotSpecified, "'#{environment}' database is not configured. Available configuration: #{configurations.inspect}"
99
101
  else
100
- configurations[environment].presence || configurations[connection]
102
+ configurations[database] || configurations[environment].presence
101
103
  end
102
104
  end
103
105
  end
@@ -106,8 +108,8 @@ module Rails
106
108
  Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment
107
109
  end
108
110
 
109
- def connection
110
- @options.fetch(:connection, "primary")
111
+ def database
112
+ @options.fetch(:database, "primary")
111
113
  end
112
114
 
113
115
  private
@@ -156,12 +158,22 @@ module Rails
156
158
  class_option :connection, aliases: "-c", type: :string,
157
159
  desc: "Specifies the connection to use."
158
160
 
161
+ class_option :database, aliases: "--db", type: :string,
162
+ desc: "Specifies the database to use."
163
+
159
164
  def perform
160
165
  extract_environment_option_from_argument
161
166
 
162
167
  # RAILS_ENV needs to be set before config/application is required.
163
168
  ENV["RAILS_ENV"] = options[:environment]
164
169
 
170
+ if options["connection"]
171
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
172
+ `connection` option is deprecated and will be removed in Rails 6.1. Please use `database` option instead.
173
+ MSG
174
+ options["database"] = options["connection"]
175
+ end
176
+
165
177
  require_application_and_environment!
166
178
  Rails::DBConsole.start(options)
167
179
  end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/dev_caching"
4
+
5
+ module Rails
6
+ module Command
7
+ class DevCommand < Base # :nodoc:
8
+ no_commands do
9
+ def help
10
+ say "rails dev:cache # Toggle development mode caching on/off."
11
+ end
12
+ end
13
+
14
+ def cache
15
+ Rails::DevCaching.enable_by_file
16
+ end
17
+ end
18
+ end
19
+ end
@@ -6,7 +6,7 @@ See the `Rails.application.encrypted` documentation for using them in your app.
6
6
  === Encryption Keys
7
7
 
8
8
  By default, Rails looks for the encryption key in `config/master.key` or
9
- `ENV["RAILS_MASTER_KEY"]`, but that lookup can be overriden with `--key`:
9
+ `ENV["RAILS_MASTER_KEY"]`, but that lookup can be overridden with `--key`:
10
10
 
11
11
  rails encrypted:edit config/encrypted_file.yml.enc --key config/encrypted_file.key
12
12
 
@@ -77,9 +77,9 @@ module Rails
77
77
 
78
78
  def missing_encrypted_message(key:, key_path:, file_path:)
79
79
  if key.nil?
80
- "Missing '#{key_path}' to decrypt data. See bin/rails encrypted:help"
80
+ "Missing '#{key_path}' to decrypt data. See `rails encrypted:help`"
81
81
  else
82
- "File '#{file_path}' does not exist. Use bin/rails encrypted:edit #{file_path} to change that."
82
+ "File '#{file_path}' does not exist. Use `rails encrypted:edit #{file_path}` to change that."
83
83
  end
84
84
  end
85
85
  end
@@ -6,7 +6,7 @@ module Rails
6
6
  hide_command!
7
7
 
8
8
  def help(*)
9
- puts self.class.desc
9
+ say self.class.desc
10
10
 
11
11
  Rails::Command.print_commands
12
12
  end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/command/environment_argument"
4
+
5
+ module Rails
6
+ module Command
7
+ class InitializersCommand < Base # :nodoc:
8
+ include EnvironmentArgument
9
+
10
+ desc "initializers", "Print out all defined initializers in the order they are invoked by Rails."
11
+ def perform
12
+ extract_environment_option_from_argument
13
+ ENV["RAILS_ENV"] = options[:environment]
14
+
15
+ require_application_and_environment!
16
+
17
+ Rails.application.initializers.tsort_each do |initializer|
18
+ say "#{initializer.context_class}.#{initializer.name}"
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -10,8 +10,8 @@ module Rails
10
10
  end
11
11
 
12
12
  def perform(*)
13
- puts "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\n"
14
- puts "Type 'rails' for help."
13
+ say "Can't initialize a new Rails application within the directory of another, please change to a non-Rails directory first.\n"
14
+ say "Type 'rails' for help."
15
15
  exit 1
16
16
  end
17
17
  end
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/source_annotation_extractor"
4
+
5
+ module Rails
6
+ module Command
7
+ class NotesCommand < Base # :nodoc:
8
+ class_option :annotations, aliases: "-a", desc: "Filter by specific annotations, e.g. Foobar TODO", type: :array, default: Rails::SourceAnnotationExtractor::Annotation.tags
9
+
10
+ def perform(*)
11
+ require_application_and_environment!
12
+
13
+ deprecation_warning
14
+ display_annotations
15
+ end
16
+
17
+ private
18
+ def display_annotations
19
+ annotations = options[:annotations]
20
+ tag = (annotations.length > 1)
21
+
22
+ Rails::SourceAnnotationExtractor.enumerate annotations.join("|"), tag: tag, dirs: directories
23
+ end
24
+
25
+ def directories
26
+ Rails::SourceAnnotationExtractor::Annotation.directories + source_annotation_directories
27
+ end
28
+
29
+ def deprecation_warning
30
+ return if source_annotation_directories.empty?
31
+ ActiveSupport::Deprecation.warn("`SOURCE_ANNOTATION_DIRECTORIES` is deprecated and will be removed in Rails 6.1. You can add default directories by using config.annotations.register_directories instead.")
32
+ end
33
+
34
+ def source_annotation_directories
35
+ ENV["SOURCE_ANNOTATION_DIRECTORIES"].to_s.split(",")
36
+ end
37
+ end
38
+ end
39
+ end
@@ -26,7 +26,7 @@ module Rails
26
26
 
27
27
  if File.exist?(railsrc)
28
28
  extra_args = File.read(railsrc).split(/\n+/).flat_map(&:split)
29
- puts "Using #{extra_args.join(" ")} from #{railsrc}"
29
+ say "Using #{extra_args.join(" ")} from #{railsrc}"
30
30
  plugin_args.insert(1, *extra_args)
31
31
  end
32
32
  end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rails/command"
4
+
5
+ module Rails
6
+ module Command
7
+ class RoutesCommand < Base # :nodoc:
8
+ class_option :controller, aliases: "-c", desc: "Filter by a specific controller, e.g. PostsController or Admin::PostsController."
9
+ class_option :grep, aliases: "-g", desc: "Grep routes by a specific pattern."
10
+ class_option :expanded, type: :boolean, aliases: "-E", desc: "Print routes expanded vertically with parts explained."
11
+
12
+ def perform(*)
13
+ require_application_and_environment!
14
+ require "action_dispatch/routing/inspector"
15
+
16
+ say inspector.format(formatter, routes_filter)
17
+ end
18
+
19
+ private
20
+ def inspector
21
+ ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes)
22
+ end
23
+
24
+ def formatter
25
+ if options.key?("expanded")
26
+ ActionDispatch::Routing::ConsoleFormatter::Expanded.new
27
+ else
28
+ ActionDispatch::Routing::ConsoleFormatter::Sheet.new
29
+ end
30
+ end
31
+
32
+ def routes_filter
33
+ options.symbolize_keys.slice(:controller, :grep)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -1,16 +1,18 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "rails/command/environment_argument"
4
+
3
5
  module Rails
4
6
  module Command
5
7
  class RunnerCommand < Base # :nodoc:
6
- class_option :environment, aliases: "-e", type: :string,
7
- default: Rails::Command.environment.dup,
8
- desc: "The environment for the runner to operate under (test/development/production)"
8
+ include EnvironmentArgument
9
+
10
+ self.environment_desc = "The environment for the runner to operate under (test/development/production)"
9
11
 
10
12
  no_commands do
11
13
  def help
12
14
  super
13
- puts self.class.desc
15
+ say self.class.desc
14
16
  end
15
17
  end
16
18
 
@@ -19,6 +21,8 @@ module Rails
19
21
  end
20
22
 
21
23
  def perform(code_or_file = nil, *command_argv)
24
+ extract_environment_option_from_argument
25
+
22
26
  unless code_or_file
23
27
  help
24
28
  exit 1
@@ -39,11 +43,11 @@ module Rails
39
43
  else
40
44
  begin
41
45
  eval(code_or_file, TOPLEVEL_BINDING, __FILE__, __LINE__)
42
- rescue SyntaxError, NameError => error
43
- $stderr.puts "Please specify a valid ruby command or the path of a script to run."
44
- $stderr.puts "Run '#{self.class.executable} -h' for help."
45
- $stderr.puts
46
- $stderr.puts error
46
+ rescue SyntaxError, NameError => e
47
+ error "Please specify a valid ruby command or the path of a script to run."
48
+ error "Run '#{self.class.executable} -h' for help."
49
+ error ""
50
+ error e
47
51
  exit 1
48
52
  end
49
53
  end
@@ -7,7 +7,7 @@ with the code.
7
7
 
8
8
  === Setup
9
9
 
10
- Run `bin/rails secrets:setup` to opt in and generate the `config/secrets.yml.key`
10
+ Run `rails secrets:setup` to opt in and generate the `config/secrets.yml.key`
11
11
  and `config/secrets.yml.enc` files.
12
12
 
13
13
  The latter contains all the keys to be encrypted while the former holds the
@@ -45,12 +45,12 @@ the key. Add this:
45
45
 
46
46
  config.read_encrypted_secrets = true
47
47
 
48
- to the environment you'd like to read encrypted secrets. `bin/rails secrets:setup`
48
+ to the environment you'd like to read encrypted secrets. `rails secrets:setup`
49
49
  inserts this into the production environment by default.
50
50
 
51
51
  === Editing Secrets
52
52
 
53
- After `bin/rails secrets:setup`, run `bin/rails secrets:edit`.
53
+ After `rails secrets:setup`, run `rails secrets:edit`.
54
54
 
55
55
  That command opens a temporary file in `$EDITOR` with the decrypted contents of
56
56
  `config/secrets.yml.enc` to edit the encrypted secrets.
@@ -22,7 +22,7 @@ module Rails
22
22
  if ENV["EDITOR"].to_s.empty?
23
23
  say "No $EDITOR to open decrypted secrets in. Assign one like this:"
24
24
  say ""
25
- say %(EDITOR="mate --wait" bin/rails secrets:edit)
25
+ say %(EDITOR="mate --wait" rails secrets:edit)
26
26
  say ""
27
27
  say "For editors that fork and exit immediately, it's important to pass a wait flag,"
28
28
  say "otherwise the secrets will be saved immediately with no chance to edit."
@@ -42,7 +42,7 @@ module Rails
42
42
  rescue Rails::Secrets::MissingKeyError => error
43
43
  say error.message
44
44
  rescue Errno::ENOENT => error
45
- if error.message =~ /secrets\.yml\.enc/
45
+ if /secrets\.yml\.enc/.match?(error.message)
46
46
  deprecate_in_favor_of_credentials_and_exit
47
47
  else
48
48
  raise
@@ -56,7 +56,7 @@ module Rails
56
56
  private
57
57
  def deprecate_in_favor_of_credentials_and_exit
58
58
  say "Encrypted secrets is deprecated in favor of credentials. Run:"
59
- say "bin/rails credentials:help"
59
+ say "rails credentials:help"
60
60
 
61
61
  exit 1
62
62
  end
@@ -1,12 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "fileutils"
4
- require "optparse"
5
4
  require "action_dispatch"
6
5
  require "rails"
7
6
  require "active_support/deprecation"
8
7
  require "active_support/core_ext/string/filters"
9
8
  require "rails/dev_caching"
9
+ require "rails/command/environment_argument"
10
10
 
11
11
  module Rails
12
12
  class Server < ::Rack::Server
@@ -22,19 +22,6 @@ module Rails
22
22
  set_environment
23
23
  end
24
24
 
25
- def app
26
- @app ||= begin
27
- app = super
28
- if app.is_a?(Class)
29
- ActiveSupport::Deprecation.warn(<<-MSG.squish)
30
- Using `Rails::Application` subclass to start the server is deprecated and will be removed in Rails 6.0.
31
- Please change `run #{app}` to `run Rails.application` in config.ru.
32
- MSG
33
- end
34
- app.respond_to?(:to_app) ? app.to_app : app
35
- end
36
- end
37
-
38
25
  def opt_parser
39
26
  Options.new
40
27
  end
@@ -43,18 +30,22 @@ module Rails
43
30
  ENV["RAILS_ENV"] ||= options[:environment]
44
31
  end
45
32
 
46
- def start
47
- print_boot_information
33
+ def start(after_stop_callback = nil)
48
34
  trap(:INT) { exit }
49
35
  create_tmp_directories
50
36
  setup_dev_caching
51
37
  log_to_stdout if options[:log_stdout]
52
38
 
53
- super
39
+ super()
54
40
  ensure
55
- # The '-h' option calls exit before @options is set.
56
- # If we call 'options' with it unset, we get double help banners.
57
- puts "Exiting" unless @options && options[:daemonize]
41
+ after_stop_callback.call if after_stop_callback
42
+ end
43
+
44
+ def serveable? # :nodoc:
45
+ server
46
+ true
47
+ rescue LoadError, NameError
48
+ false
58
49
  end
59
50
 
60
51
  def middleware
@@ -65,6 +56,10 @@ module Rails
65
56
  super.merge(@default_options)
66
57
  end
67
58
 
59
+ def served_url
60
+ "#{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" unless use_puma?
61
+ end
62
+
68
63
  private
69
64
  def setup_dev_caching
70
65
  if options[:environment] == "development"
@@ -72,13 +67,6 @@ module Rails
72
67
  end
73
68
  end
74
69
 
75
- def print_boot_information
76
- url = "on #{options[:SSLEnable] ? 'https' : 'http'}://#{options[:Host]}:#{options[:Port]}" unless use_puma?
77
- puts "=> Booting #{ActiveSupport::Inflector.demodulize(server)}"
78
- puts "=> Rails #{Rails.version} application starting in #{Rails.env} #{url}"
79
- puts "=> Run `rails server -h` for more startup options"
80
- end
81
-
82
70
  def create_tmp_directories
83
71
  %w(cache pids sockets).each do |dir_to_make|
84
72
  FileUtils.mkdir_p(File.join(Rails.root, "tmp", dir_to_make))
@@ -97,10 +85,6 @@ module Rails
97
85
  end
98
86
  end
99
87
 
100
- def restart_command
101
- "bin/rails server #{ARGV.join(' ')}"
102
- end
103
-
104
88
  def use_puma?
105
89
  server.to_s == "Rack::Handler::Puma"
106
90
  end
@@ -108,8 +92,16 @@ module Rails
108
92
 
109
93
  module Command
110
94
  class ServerCommand < Base # :nodoc:
95
+ include EnvironmentArgument
96
+
97
+ # Hard-coding a bunch of handlers here as we don't have a public way of
98
+ # querying them from the Rack::Handler registry.
99
+ RACK_SERVERS = %w(cgi fastcgi webrick lsws scgi thin puma unicorn)
100
+
111
101
  DEFAULT_PORT = 3000
112
- DEFAULT_PID_PATH = "tmp/pids/server.pid".freeze
102
+ DEFAULT_PID_PATH = "tmp/pids/server.pid"
103
+
104
+ argument :using, optional: true
113
105
 
114
106
  class_option :port, aliases: "-p", type: :numeric,
115
107
  desc: "Runs Rails on the specified port - defaults to 3000.", banner: :port
@@ -120,31 +112,42 @@ module Rails
120
112
  desc: "Uses a custom rackup configuration.", banner: :file
121
113
  class_option :daemon, aliases: "-d", type: :boolean, default: false,
122
114
  desc: "Runs server as a Daemon."
123
- class_option :environment, aliases: "-e", type: :string,
124
- desc: "Specifies the environment to run this server under (development/test/production).", banner: :name
115
+ class_option :using, aliases: "-u", type: :string,
116
+ desc: "Specifies the Rack server used to run the application (thin/puma/webrick).", banner: :name
125
117
  class_option :pid, aliases: "-P", type: :string, default: DEFAULT_PID_PATH,
126
118
  desc: "Specifies the PID file."
127
- class_option "dev-caching", aliases: "-C", type: :boolean, default: nil,
119
+ class_option :dev_caching, aliases: "-C", type: :boolean, default: nil,
128
120
  desc: "Specifies whether to perform caching in development."
129
- class_option "restart", type: :boolean, default: nil, hide: true
130
- class_option "early_hints", type: :boolean, default: nil, desc: "Enables HTTP/2 early hints."
121
+ class_option :restart, type: :boolean, default: nil, hide: true
122
+ class_option :early_hints, type: :boolean, default: nil, desc: "Enables HTTP/2 early hints."
123
+ class_option :log_to_stdout, type: :boolean, default: nil, optional: true,
124
+ desc: "Whether to log to stdout. Enabled by default in development when not daemonized."
131
125
 
132
- def initialize(args = [], local_options = {}, config = {})
133
- @original_options = local_options
126
+ def initialize(args, local_options, *)
134
127
  super
135
- @server = self.args.shift
136
- @log_stdout = options[:daemon].blank? && (options[:environment] || Rails.env) == "development"
128
+
129
+ @original_options = local_options - %w( --restart )
130
+ deprecate_positional_rack_server_and_rewrite_to_option(@original_options)
137
131
  end
138
132
 
139
133
  def perform
134
+ extract_environment_option_from_argument
140
135
  set_application_directory!
141
136
  prepare_restart
137
+
142
138
  Rails::Server.new(server_options).tap do |server|
143
139
  # Require application after server sets environment to propagate
144
140
  # the --environment option.
145
141
  require APP_PATH
146
142
  Dir.chdir(Rails.application.root)
147
- server.start
143
+
144
+ if server.serveable?
145
+ print_boot_information(server.server, server.served_url)
146
+ after_stop_callback = -> { say "Exiting" unless options[:daemon] }
147
+ server.start(after_stop_callback)
148
+ else
149
+ say rack_server_suggestion(using)
150
+ end
148
151
  end
149
152
  end
150
153
 
@@ -152,8 +155,8 @@ module Rails
152
155
  def server_options
153
156
  {
154
157
  user_supplied_options: user_supplied_options,
155
- server: @server,
156
- log_stdout: @log_stdout,
158
+ server: using,
159
+ log_stdout: log_to_stdout?,
157
160
  Port: port,
158
161
  Host: host,
159
162
  DoNotReverseLookup: true,
@@ -161,7 +164,7 @@ module Rails
161
164
  environment: environment,
162
165
  daemonize: options[:daemon],
163
166
  pid: pid,
164
- caching: options["dev-caching"],
167
+ caching: options[:dev_caching],
165
168
  restart_cmd: restart_command,
166
169
  early_hints: early_hints
167
170
  }
@@ -194,7 +197,7 @@ module Rails
194
197
  name = :Port
195
198
  when :binding
196
199
  name = :Host
197
- when :"dev-caching"
200
+ when :dev_caching
198
201
  name = :caching
199
202
  when :daemonize
200
203
  name = :daemon
@@ -202,7 +205,7 @@ module Rails
202
205
  user_supplied_options << name
203
206
  end
204
207
  end
205
- user_supplied_options << :Host if ENV["HOST"]
208
+ user_supplied_options << :Host if ENV["HOST"] || ENV["BINDING"]
206
209
  user_supplied_options << :Port if ENV["PORT"]
207
210
  user_supplied_options.uniq
208
211
  end
@@ -217,7 +220,17 @@ module Rails
217
220
  options[:binding]
218
221
  else
219
222
  default_host = environment == "development" ? "localhost" : "0.0.0.0"
220
- ENV.fetch("HOST", default_host)
223
+
224
+ if ENV["HOST"] && !ENV["BINDING"]
225
+ ActiveSupport::Deprecation.warn(<<-MSG.squish)
226
+ Using the `HOST` environment variable to specify the IP is deprecated and will be removed in Rails 6.1.
227
+ Please use `BINDING` environment variable instead.
228
+ MSG
229
+
230
+ return ENV["HOST"]
231
+ end
232
+
233
+ ENV.fetch("BINDING", default_host)
221
234
  end
222
235
  end
223
236
 
@@ -226,24 +239,74 @@ module Rails
226
239
  end
227
240
 
228
241
  def restart_command
229
- "bin/rails server #{@server} #{@original_options.join(" ")} --restart"
242
+ "bin/rails server #{@original_options.join(" ")} --restart"
230
243
  end
231
244
 
232
245
  def early_hints
233
246
  options[:early_hints]
234
247
  end
235
248
 
249
+ def log_to_stdout?
250
+ options.fetch(:log_to_stdout) do
251
+ options[:daemon].blank? && environment == "development"
252
+ end
253
+ end
254
+
236
255
  def pid
237
256
  File.expand_path(options[:pid])
238
257
  end
239
258
 
240
259
  def self.banner(*)
241
- "rails server [puma, thin etc] [options]"
260
+ "rails server -u [thin/puma/webrick] [options]"
242
261
  end
243
262
 
244
263
  def prepare_restart
245
264
  FileUtils.rm_f(options[:pid]) if options[:restart]
246
265
  end
266
+
267
+ def deprecate_positional_rack_server_and_rewrite_to_option(original_options)
268
+ if using
269
+ ActiveSupport::Deprecation.warn(<<~MSG.squish)
270
+ Passing the Rack server name as a regular argument is deprecated
271
+ and will be removed in the next Rails version. Please, use the -u
272
+ option instead.
273
+ MSG
274
+
275
+ original_options.concat [ "-u", using ]
276
+ else
277
+ # Use positional internally to get around Thor's immutable options.
278
+ # TODO: Replace `using` occurrences with `options[:using]` after deprecation removal.
279
+ @using = options[:using]
280
+ end
281
+ end
282
+
283
+ def rack_server_suggestion(server)
284
+ if server.in?(RACK_SERVERS)
285
+ <<~MSG
286
+ Could not load server "#{server}". Maybe you need to the add it to the Gemfile?
287
+
288
+ gem "#{server}"
289
+
290
+ Run `rails server --help` for more options.
291
+ MSG
292
+ else
293
+ suggestion = Rails::Command::Spellchecker.suggest(server, from: RACK_SERVERS)
294
+ suggestion_msg = "Maybe you meant #{suggestion.inspect}?" if suggestion
295
+
296
+ <<~MSG
297
+ Could not find server "#{server}". #{suggestion_msg}
298
+ Run `rails server --help` for more options.
299
+ MSG
300
+ end
301
+ end
302
+
303
+ def print_boot_information(server, url)
304
+ say <<~MSG
305
+ => Booting #{ActiveSupport::Inflector.demodulize(server)}
306
+ => Rails #{Rails.version} application starting in #{Rails.env} #{url}
307
+ => Run `rails server --help` for more startup options
308
+ MSG
309
+ end
247
310
  end
248
311
  end
249
312
  end