railties 7.0.8.6 → 7.1.0.beta1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +572 -226
  3. data/MIT-LICENSE +1 -1
  4. data/RDOC_MAIN.md +99 -0
  5. data/README.rdoc +4 -4
  6. data/lib/minitest/rails_plugin.rb +63 -0
  7. data/lib/rails/api/task.rb +35 -4
  8. data/lib/rails/app_updater.rb +1 -1
  9. data/lib/rails/application/bootstrap.rb +12 -3
  10. data/lib/rails/application/configuration.rb +179 -67
  11. data/lib/rails/application/default_middleware_stack.rb +8 -2
  12. data/lib/rails/application/dummy_config.rb +19 -0
  13. data/lib/rails/application/finisher.rb +40 -33
  14. data/lib/rails/application.rb +112 -24
  15. data/lib/rails/backtrace_cleaner.rb +1 -1
  16. data/lib/rails/cli.rb +5 -2
  17. data/lib/rails/command/actions.rb +10 -12
  18. data/lib/rails/command/base.rb +55 -53
  19. data/lib/rails/command/environment_argument.rb +32 -16
  20. data/lib/rails/command/helpers/editor.rb +17 -12
  21. data/lib/rails/command.rb +84 -33
  22. data/lib/rails/commands/about/about_command.rb +14 -0
  23. data/lib/rails/commands/application/application_command.rb +2 -0
  24. data/lib/rails/commands/console/console_command.rb +14 -14
  25. data/lib/rails/commands/credentials/USAGE +53 -55
  26. data/lib/rails/commands/credentials/credentials_command/diffing.rb +5 -3
  27. data/lib/rails/commands/credentials/credentials_command.rb +64 -70
  28. data/lib/rails/commands/db/system/change/change_command.rb +2 -1
  29. data/lib/rails/commands/dbconsole/dbconsole_command.rb +25 -115
  30. data/lib/rails/commands/destroy/destroy_command.rb +3 -2
  31. data/lib/rails/commands/dev/dev_command.rb +1 -6
  32. data/lib/rails/commands/encrypted/USAGE +15 -20
  33. data/lib/rails/commands/encrypted/encrypted_command.rb +46 -35
  34. data/lib/rails/commands/gem_help/USAGE +16 -0
  35. data/lib/rails/commands/gem_help/gem_help_command.rb +13 -0
  36. data/lib/rails/commands/generate/generate_command.rb +2 -2
  37. data/lib/rails/commands/help/USAGE +13 -13
  38. data/lib/rails/commands/help/help_command.rb +21 -2
  39. data/lib/rails/commands/initializers/initializers_command.rb +1 -4
  40. data/lib/rails/commands/middleware/middleware_command.rb +17 -0
  41. data/lib/rails/commands/new/new_command.rb +2 -0
  42. data/lib/rails/commands/notes/notes_command.rb +2 -1
  43. data/lib/rails/commands/plugin/plugin_command.rb +2 -0
  44. data/lib/rails/commands/rake/rake_command.rb +25 -22
  45. data/lib/rails/commands/restart/restart_command.rb +14 -0
  46. data/lib/rails/commands/routes/routes_command.rb +13 -1
  47. data/lib/rails/commands/runner/USAGE +14 -12
  48. data/lib/rails/commands/runner/runner_command.rb +32 -20
  49. data/lib/rails/commands/secret/secret_command.rb +13 -0
  50. data/lib/rails/commands/secrets/USAGE +44 -49
  51. data/lib/rails/commands/secrets/secrets_command.rb +19 -38
  52. data/lib/rails/commands/server/server_command.rb +32 -31
  53. data/lib/rails/commands/test/USAGE +14 -0
  54. data/lib/rails/commands/test/test_command.rb +56 -14
  55. data/lib/rails/commands/unused_routes/unused_routes_command.rb +75 -0
  56. data/lib/rails/commands/version/version_command.rb +1 -0
  57. data/lib/rails/configuration.rb +5 -5
  58. data/lib/rails/console/app.rb +1 -4
  59. data/lib/rails/deprecator.rb +7 -0
  60. data/lib/rails/engine/configuration.rb +5 -0
  61. data/lib/rails/engine.rb +32 -11
  62. data/lib/rails/gem_version.rb +4 -4
  63. data/lib/rails/generators/actions.rb +6 -15
  64. data/lib/rails/generators/active_model.rb +2 -2
  65. data/lib/rails/generators/app_base.rb +354 -83
  66. data/lib/rails/generators/app_name.rb +3 -14
  67. data/lib/rails/generators/base.rb +12 -4
  68. data/lib/rails/generators/database.rb +19 -1
  69. data/lib/rails/generators/erb/mailer/templates/layout.html.erb.tt +1 -1
  70. data/lib/rails/generators/generated_attribute.rb +2 -0
  71. data/lib/rails/generators/migration.rb +1 -2
  72. data/lib/rails/generators/model_helpers.rb +2 -1
  73. data/lib/rails/generators/rails/app/USAGE +15 -6
  74. data/lib/rails/generators/rails/app/app_generator.rb +84 -60
  75. data/lib/rails/generators/rails/app/templates/Dockerfile.tt +107 -0
  76. data/lib/rails/generators/rails/app/templates/Gemfile.tt +8 -10
  77. data/lib/rails/generators/rails/app/templates/app/views/layouts/mailer.html.erb.tt +1 -1
  78. data/lib/rails/generators/rails/app/templates/bin/setup.tt +10 -1
  79. data/lib/rails/generators/rails/app/templates/config/application.rb.tt +4 -17
  80. data/lib/rails/generators/rails/app/templates/config/databases/jdbcsqlite3.yml.tt +3 -3
  81. data/lib/rails/generators/rails/app/templates/config/databases/postgresql.yml.tt +0 -2
  82. data/lib/rails/generators/rails/app/templates/config/databases/sqlite3.yml.tt +3 -3
  83. data/lib/rails/generators/rails/app/templates/config/databases/trilogy.yml.tt +59 -0
  84. data/lib/rails/generators/rails/app/templates/config/environments/development.rb.tt +10 -2
  85. data/lib/rails/generators/rails/app/templates/config/environments/production.rb.tt +28 -24
  86. data/lib/rails/generators/rails/app/templates/config/environments/test.rb.tt +11 -7
  87. data/lib/rails/generators/rails/app/templates/config/initializers/assets.rb.tt +2 -0
  88. data/lib/rails/generators/rails/app/templates/config/initializers/content_security_policy.rb.tt +2 -2
  89. data/lib/rails/generators/rails/app/templates/config/initializers/cors.rb.tt +1 -1
  90. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_1.rb.tt +223 -0
  91. data/lib/rails/generators/rails/app/templates/config/initializers/permissions_policy.rb.tt +11 -9
  92. data/lib/rails/generators/rails/app/templates/config/locales/en.yml +11 -13
  93. data/lib/rails/generators/rails/app/templates/config/puma.rb.tt +10 -19
  94. data/lib/rails/generators/rails/app/templates/config/routes.rb.tt +4 -0
  95. data/lib/rails/generators/rails/app/templates/db/seeds.rb.tt +6 -4
  96. data/lib/rails/generators/rails/app/templates/docker-entrypoint.tt +10 -0
  97. data/lib/rails/generators/rails/app/templates/dockerignore.tt +43 -0
  98. data/lib/rails/generators/rails/app/templates/gitignore.tt +1 -9
  99. data/lib/rails/generators/rails/app/templates/node-version.tt +1 -0
  100. data/lib/rails/generators/rails/app/templates/test/channels/application_cable/connection_test.rb.tt +10 -8
  101. data/lib/rails/generators/rails/app/templates/test/test_helper.rb.tt +9 -7
  102. data/lib/rails/generators/rails/application_record/application_record_generator.rb +4 -0
  103. data/lib/rails/generators/rails/benchmark/benchmark_generator.rb +2 -1
  104. data/lib/rails/generators/rails/controller/USAGE +12 -4
  105. data/lib/rails/generators/rails/controller/controller_generator.rb +5 -0
  106. data/lib/rails/generators/rails/controller/templates/controller.rb.tt +1 -1
  107. data/lib/rails/generators/rails/credentials/credentials_generator.rb +29 -24
  108. data/lib/rails/generators/rails/credentials/templates/credentials.yml.tt +8 -0
  109. data/lib/rails/generators/rails/encryption_key_file/encryption_key_file_generator.rb +1 -2
  110. data/lib/rails/generators/rails/migration/USAGE +21 -11
  111. data/lib/rails/generators/rails/model/model_generator.rb +4 -0
  112. data/lib/rails/generators/rails/plugin/USAGE +17 -6
  113. data/lib/rails/generators/rails/plugin/plugin_generator.rb +5 -15
  114. data/lib/rails/generators/rails/plugin/templates/Gemfile.tt +2 -2
  115. data/lib/rails/generators/rails/plugin/templates/MIT-LICENSE.tt +1 -1
  116. data/lib/rails/generators/rails/plugin/templates/bin/rails.tt +1 -17
  117. data/lib/rails/generators/rails/plugin/templates/gitignore.tt +0 -2
  118. data/lib/rails/generators/rails/plugin/templates/test/test_helper.rb.tt +4 -4
  119. data/lib/rails/generators/rails/resource/resource_generator.rb +6 -0
  120. data/lib/rails/generators/rails/scaffold/scaffold_generator.rb +2 -1
  121. data/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb +1 -1
  122. data/lib/rails/generators/test_case.rb +2 -2
  123. data/lib/rails/generators/test_unit/scaffold/scaffold_generator.rb +1 -1
  124. data/lib/rails/generators/testing/{behaviour.rb → behavior.rb} +4 -1
  125. data/lib/rails/generators.rb +5 -13
  126. data/lib/rails/health_controller.rb +55 -0
  127. data/lib/rails/info.rb +1 -1
  128. data/lib/rails/info_controller.rb +31 -11
  129. data/lib/rails/mailers_controller.rb +15 -5
  130. data/lib/rails/paths.rb +13 -10
  131. data/lib/rails/rack/logger.rb +15 -12
  132. data/lib/rails/rackup/server.rb +15 -0
  133. data/lib/rails/railtie/configuration.rb +14 -1
  134. data/lib/rails/railtie.rb +18 -18
  135. data/lib/rails/ruby_version_check.rb +2 -0
  136. data/lib/rails/source_annotation_extractor.rb +67 -18
  137. data/lib/rails/tasks/engine.rake +8 -8
  138. data/lib/rails/tasks/framework.rake +4 -10
  139. data/lib/rails/tasks/log.rake +1 -1
  140. data/lib/rails/tasks/misc.rake +3 -14
  141. data/lib/rails/tasks/statistics.rake +5 -4
  142. data/lib/rails/tasks/tmp.rake +5 -5
  143. data/lib/rails/tasks/zeitwerk.rake +1 -1
  144. data/lib/rails/tasks.rb +0 -2
  145. data/lib/rails/templates/rails/mailers/email.html.erb +25 -0
  146. data/lib/rails/templates/rails/mailers/index.html.erb +14 -7
  147. data/lib/rails/templates/rails/mailers/mailer.html.erb +11 -5
  148. data/lib/rails/templates/rails/welcome/index.html.erb +1 -0
  149. data/lib/rails/test_help.rb +7 -7
  150. data/lib/rails/test_unit/line_filtering.rb +1 -1
  151. data/lib/rails/test_unit/reporter.rb +6 -2
  152. data/lib/rails/test_unit/runner.rb +36 -18
  153. data/lib/rails/test_unit/test_parser.rb +88 -0
  154. data/lib/rails/test_unit/testing.rake +13 -33
  155. data/lib/rails/version.rb +1 -1
  156. data/lib/rails.rb +15 -15
  157. metadata +69 -34
  158. data/RDOC_MAIN.rdoc +0 -97
  159. data/lib/rails/application/dummy_erb_compiler.rb +0 -18
  160. data/lib/rails/generators/rails/app/templates/config/initializers/new_framework_defaults_7_0.rb.tt +0 -143
  161. data/lib/rails/generators/rails/model/USAGE +0 -113
  162. data/lib/rails/tasks/middleware.rake +0 -9
  163. data/lib/rails/tasks/restart.rake +0 -9
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "active_support/core_ext/string/filters"
4
- require "active_support/deprecation"
5
4
  require "rails/command/environment_argument"
6
5
 
7
6
  module Rails
@@ -12,86 +11,13 @@ module Rails
12
11
 
13
12
  def initialize(options = {})
14
13
  @options = options
14
+ @options[:environment] ||= Rails::Command.environment
15
15
  end
16
16
 
17
17
  def start
18
- ENV["RAILS_ENV"] ||= @options[:environment] || environment
19
- config = db_config.configuration_hash
20
-
21
- case db_config.adapter
22
- when /^(jdbc)?mysql/
23
- args = {
24
- host: "--host",
25
- port: "--port",
26
- socket: "--socket",
27
- username: "--user",
28
- encoding: "--default-character-set",
29
- sslca: "--ssl-ca",
30
- sslcert: "--ssl-cert",
31
- sslcapath: "--ssl-capath",
32
- sslcipher: "--ssl-cipher",
33
- sslkey: "--ssl-key"
34
- }.filter_map { |opt, arg| "#{arg}=#{config[opt]}" if config[opt] }
35
-
36
- if config[:password] && @options[:include_password]
37
- args << "--password=#{config[:password]}"
38
- elsif config[:password] && !config[:password].to_s.empty?
39
- args << "-p"
40
- end
41
-
42
- args << db_config.database
43
-
44
- find_cmd_and_exec(["mysql", "mysql5"], *args)
45
-
46
- when /^postgres|^postgis/
47
- ENV["PGUSER"] = config[:username] if config[:username]
48
- ENV["PGHOST"] = config[:host] if config[:host]
49
- ENV["PGPORT"] = config[:port].to_s if config[:port]
50
- ENV["PGPASSWORD"] = config[:password].to_s if config[:password] && @options[:include_password]
51
- ENV["PGSSLMODE"] = config[:sslmode].to_s if config[:sslmode]
52
- ENV["PGSSLCERT"] = config[:sslcert].to_s if config[:sslcert]
53
- ENV["PGSSLKEY"] = config[:sslkey].to_s if config[:sslkey]
54
- ENV["PGSSLROOTCERT"] = config[:sslrootcert].to_s if config[:sslrootcert]
55
- find_cmd_and_exec("psql", db_config.database)
56
-
57
- when "sqlite3"
58
- args = []
59
-
60
- args << "-#{@options[:mode]}" if @options[:mode]
61
- args << "-header" if @options[:header]
62
- args << File.expand_path(db_config.database, Rails.respond_to?(:root) ? Rails.root : nil)
63
-
64
- find_cmd_and_exec("sqlite3", *args)
65
-
66
- when "oracle", "oracle_enhanced"
67
- logon = ""
68
-
69
- if config[:username]
70
- logon = config[:username].dup
71
- logon << "/#{config[:password]}" if config[:password] && @options[:include_password]
72
- logon << "@#{db_config.database}" if db_config.database
73
- end
74
-
75
- find_cmd_and_exec("sqlplus", logon)
76
-
77
- when "sqlserver"
78
- args = []
79
-
80
- args += ["-d", "#{db_config.database}"] if db_config.database
81
- args += ["-U", "#{config[:username]}"] if config[:username]
82
- args += ["-P", "#{config[:password]}"] if config[:password]
83
-
84
- if config[:host]
85
- host_arg = +"tcp:#{config[:host]}"
86
- host_arg << ",#{config[:port]}" if config[:port]
87
- args += ["-S", host_arg]
88
- end
89
-
90
- find_cmd_and_exec("sqlcmd", *args)
91
-
92
- else
93
- abort "Unknown command-line client for #{db_config.database}."
94
- end
18
+ adapter_class.dbconsole(db_config, @options)
19
+ rescue NotImplementedError
20
+ abort "Unknown command-line client for #{db_config.database}."
95
21
  end
96
22
 
97
23
  def db_config
@@ -114,15 +40,23 @@ module Rails
114
40
  @db_config
115
41
  end
116
42
 
117
- def environment
118
- Rails.respond_to?(:env) ? Rails.env : Rails::Command.environment
119
- end
120
-
121
43
  def database
122
44
  @options[:database]
123
45
  end
124
46
 
47
+ def environment
48
+ @options[:environment]
49
+ end
50
+
125
51
  private
52
+ def adapter_class
53
+ if ActiveRecord::Base.respond_to?(db_config.adapter_class_method)
54
+ ActiveRecord::Base.public_send(db_config.adapter_class_method)
55
+ else
56
+ ActiveRecord::ConnectionAdapters::AbstractAdapter
57
+ end
58
+ end
59
+
126
60
  def configurations # :doc:
127
61
  require APP_PATH
128
62
  ActiveRecord::Base.configurations = Rails.application.config.database_configuration
@@ -130,31 +64,11 @@ module Rails
130
64
  end
131
65
 
132
66
  def find_cmd_and_exec(commands, *args) # :doc:
133
- commands = Array(commands)
134
-
135
- dirs_on_path = ENV["PATH"].to_s.split(File::PATH_SEPARATOR)
136
- unless (ext = RbConfig::CONFIG["EXEEXT"]).empty?
137
- commands = commands.map { |cmd| "#{cmd}#{ext}" }
138
- end
139
-
140
- full_path_command = nil
141
- found = commands.detect do |cmd|
142
- dirs_on_path.detect do |path|
143
- full_path_command = File.join(path, cmd)
144
- begin
145
- stat = File.stat(full_path_command)
146
- rescue SystemCallError
147
- else
148
- stat.file? && stat.executable?
149
- end
150
- end
151
- end
152
-
153
- if found
154
- exec full_path_command, *args
155
- else
156
- abort("Couldn't find database client: #{commands.join(', ')}. Check your $PATH and try again.")
157
- end
67
+ Rails.deprecator.warn(<<~MSG.squish)
68
+ Rails::DBConsole#find_cmd_and_exec is deprecated and will be removed in Rails 7.2.
69
+ Please use find_cmd_and_exec on the connection adapter class instead.
70
+ MSG
71
+ ActiveRecord::Base.connection.find_cmd_and_exec(commands, *args)
158
72
  end
159
73
  end
160
74
 
@@ -166,20 +80,16 @@ module Rails
166
80
  desc: "Automatically provide the password from database.yml"
167
81
 
168
82
  class_option :mode, enum: %w( html list line column ), type: :string,
169
- desc: "Automatically put the sqlite3 database in the specified mode (html, list, line, column)."
83
+ desc: "Automatically put the sqlite3 database in the specified mode"
170
84
 
171
85
  class_option :header, type: :boolean
172
86
 
173
87
  class_option :database, aliases: "--db", type: :string,
174
- desc: "Specifies the database to use."
88
+ desc: "Specify the database to use."
175
89
 
90
+ desc "dbconsole", "Start a console for the database specified in config/database.yml"
176
91
  def perform
177
- extract_environment_option_from_argument
178
-
179
- # RAILS_ENV needs to be set before config/application is required.
180
- ENV["RAILS_ENV"] = options[:environment]
181
-
182
- require_application_and_environment!
92
+ boot_application!
183
93
  Rails::DBConsole.start(options)
184
94
  end
185
95
  end
@@ -7,18 +7,19 @@ module Rails
7
7
  class DestroyCommand < Base # :nodoc:
8
8
  no_commands do
9
9
  def help
10
- require_application_and_environment!
10
+ boot_application!
11
11
  load_generators
12
12
 
13
13
  Rails::Generators.help self.class.command_name
14
14
  end
15
15
  end
16
16
 
17
+ desc "destroy GENERATOR", "Remove code generated by `bin/rails generate`"
17
18
  def perform(*)
18
19
  generator = args.shift
19
20
  return help unless generator
20
21
 
21
- require_application_and_environment!
22
+ boot_application!
22
23
  load_generators
23
24
 
24
25
  Rails::Generators.invoke generator, args, behavior: :revoke, destination_root: Rails::Command.root
@@ -5,12 +5,7 @@ require "rails/dev_caching"
5
5
  module Rails
6
6
  module Command
7
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
-
8
+ desc "cache", "Toggle development mode caching on/off"
14
9
  def cache
15
10
  Rails::DevCaching.enable_by_file
16
11
  end
@@ -1,28 +1,23 @@
1
- === Storing Encrypted Files in Source Control
1
+ Description:
2
+ The Rails `encrypted` commands provide access to encrypted files or configurations.
3
+ See the `Rails.application.encrypted` documentation for using them in your app.
2
4
 
3
- The Rails `encrypted` commands provide access to encrypted files or configurations.
4
- See the `Rails.application.encrypted` documentation for using them in your app.
5
+ Encryption Keys:
6
+ By default, Rails looks for the encryption key in `ENV["RAILS_MASTER_KEY"]` or
7
+ `config/master.key`, but that lookup can be overridden with `--key`:
5
8
 
6
- === Encryption Keys
9
+ <%= executable(:edit) %> config/encrypted_file.yml.enc --key config/encrypted_file.key
7
10
 
8
- By default, Rails looks for the encryption key in `config/master.key` or
9
- `ENV["RAILS_MASTER_KEY"]`, but that lookup can be overridden with `--key`:
11
+ Don't commit the key! Add it to your source control's ignore file. If you use
12
+ Git, Rails handles this for you.
10
13
 
11
- rails encrypted:edit config/encrypted_file.yml.enc --key config/encrypted_file.key
14
+ Examples:
15
+ To edit or create an encrypted file use:
12
16
 
13
- Don't commit the key! Add it to your source control's ignore file. If you use
14
- Git, Rails handles this for you.
17
+ <%= executable(:edit) %> config/encrypted_file.yml.enc
15
18
 
16
- === Editing Files
19
+ This opens a temporary file in `$VISUAL` or `$EDITOR` with the decrypted contents for editing.
17
20
 
18
- To edit or create an encrypted file use:
21
+ To print the decrypted contents of an encrypted file use:
19
22
 
20
- rails encrypted:edit config/encrypted_file.yml.enc
21
-
22
- This opens a temporary file in `$EDITOR` with the decrypted contents for editing.
23
-
24
- === Viewing Files
25
-
26
- To print the decrypted contents of an encrypted file use:
27
-
28
- rails encrypted:show config/encrypted_file.yml.enc
23
+ <%= executable(:show) %> config/encrypted_file.yml.enc
@@ -12,54 +12,65 @@ module Rails
12
12
  class_option :key, aliases: "-k", type: :string,
13
13
  default: "config/master.key", desc: "The Rails.root relative path to the encryption key"
14
14
 
15
- no_commands do
16
- def help
17
- say "Usage:\n #{self.class.banner}"
18
- say ""
19
- say self.class.desc
20
- end
21
- end
22
-
23
- def edit(file_path)
24
- require_application!
25
- encrypted = Rails.application.encrypted(file_path, key_path: options[:key])
15
+ desc "edit", "Open the decrypted file in `$VISUAL` or `$EDITOR` for editing"
16
+ def edit(*)
17
+ load_environment_config!
26
18
 
27
- ensure_editor_available(command: "bin/rails encrypted:edit") || (return)
28
- ensure_encryption_key_has_been_added(options[:key]) if encrypted.key.nil?
29
- ensure_encrypted_file_has_been_added(file_path, options[:key])
19
+ ensure_encryption_key_has_been_added
20
+ ensure_encrypted_configuration_has_been_added
30
21
 
31
- catch_editing_exceptions do
32
- change_encrypted_file_in_system_editor(file_path, options[:key])
33
- end
34
-
35
- say "File encrypted and saved."
36
- rescue ActiveSupport::MessageEncryptor::InvalidMessage
37
- say "Couldn't decrypt #{file_path}. Perhaps you passed the wrong key?"
22
+ change_encrypted_configuration_in_system_editor
38
23
  end
39
24
 
40
- def show(file_path)
41
- require_application!
42
- encrypted = Rails.application.encrypted(file_path, key_path: options[:key])
25
+ desc "show", "Show the decrypted contents of the file"
26
+ def show(*)
27
+ load_environment_config!
43
28
 
44
- say encrypted.read.presence || missing_encrypted_message(key: encrypted.key, key_path: options[:key], file_path: file_path)
29
+ say encrypted_configuration.read.presence || missing_encrypted_configuration_message
45
30
  end
46
31
 
47
32
  private
48
- def ensure_encryption_key_has_been_added(key_path)
33
+ def content_path
34
+ @content_path ||= args[0]
35
+ end
36
+
37
+ def key_path
38
+ options[:key]
39
+ end
40
+
41
+ def encrypted_configuration
42
+ @encrypted_configuration ||= Rails.application.encrypted(content_path, key_path: key_path)
43
+ end
44
+
45
+ def ensure_encryption_key_has_been_added
46
+ return if encrypted_configuration.key?
49
47
  encryption_key_file_generator.add_key_file(key_path)
50
48
  encryption_key_file_generator.ignore_key_file(key_path)
51
49
  end
52
50
 
53
- def ensure_encrypted_file_has_been_added(file_path, key_path)
54
- encrypted_file_generator.add_encrypted_file_silently(file_path, key_path)
51
+ def ensure_encrypted_configuration_has_been_added
52
+ encrypted_file_generator.add_encrypted_file_silently(content_path, key_path)
55
53
  end
56
54
 
57
- def change_encrypted_file_in_system_editor(file_path, key_path)
58
- Rails.application.encrypted(file_path, key_path: key_path).change do |tmp_path|
59
- system("#{ENV["EDITOR"]} #{tmp_path}")
55
+ def change_encrypted_configuration_in_system_editor
56
+ using_system_editor do
57
+ encrypted_configuration.change { |tmp_path| system_editor(tmp_path) }
58
+ say "File encrypted and saved."
59
+ warn_if_encrypted_configuration_is_invalid
60
60
  end
61
+ rescue ActiveSupport::EncryptedFile::MissingKeyError => error
62
+ say error.message
63
+ rescue ActiveSupport::MessageEncryptor::InvalidMessage
64
+ say "Couldn't decrypt #{content_path}. Perhaps you passed the wrong key?"
61
65
  end
62
66
 
67
+ def warn_if_encrypted_configuration_is_invalid
68
+ encrypted_configuration.validate!
69
+ rescue ActiveSupport::EncryptedConfiguration::InvalidContentError => error
70
+ say "WARNING: #{error.message}", :red
71
+ say ""
72
+ say "Your application will not be able to load '#{content_path}' until the error has been fixed.", :red
73
+ end
63
74
 
64
75
  def encryption_key_file_generator
65
76
  require "rails/generators"
@@ -75,11 +86,11 @@ module Rails
75
86
  Rails::Generators::EncryptedFileGenerator.new
76
87
  end
77
88
 
78
- def missing_encrypted_message(key:, key_path:, file_path:)
79
- if key.nil?
80
- "Missing '#{key_path}' to decrypt data. See `bin/rails encrypted:help`"
89
+ def missing_encrypted_configuration_message
90
+ if !encrypted_configuration.key?
91
+ "Missing '#{key_path}' to decrypt data. See `#{executable(:help)}`"
81
92
  else
82
- "File '#{file_path}' does not exist. Use `bin/rails encrypted:edit #{file_path}` to change that."
93
+ "File '#{content_path}' does not exist. Use `#{executable(:edit)} #{content_path}` to change that."
83
94
  end
84
95
  end
85
96
  end
@@ -0,0 +1,16 @@
1
+ Usage:
2
+ rails COMMAND [options]
3
+
4
+ You must specify a command:
5
+
6
+ new Create a new Rails application. "rails new my_app" creates a
7
+ new application called MyApp in "./my_app"
8
+ plugin new Create a new Rails railtie or engine
9
+
10
+ All commands can be run with -h (or --help) for more information.
11
+
12
+ Inside a Rails application directory, some common commands are:
13
+
14
+ console Start the Rails console
15
+ server Start the Rails server
16
+ test Run tests except system tests
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class GemHelpCommand < Base # :nodoc:
6
+ hide_command!
7
+
8
+ def perform
9
+ say self.class.class_usage
10
+ end
11
+ end
12
+ end
13
+ end
@@ -7,7 +7,7 @@ module Rails
7
7
  class GenerateCommand < Base # :nodoc:
8
8
  no_commands do
9
9
  def help
10
- require_application_and_environment!
10
+ boot_application!
11
11
  load_generators
12
12
 
13
13
  Rails::Generators.help self.class.command_name
@@ -18,7 +18,7 @@ module Rails
18
18
  generator = args.shift
19
19
  return help unless generator
20
20
 
21
- require_application_and_environment!
21
+ boot_application!
22
22
  load_generators
23
23
 
24
24
  ARGV.replace(args) # set up ARGV for third-party libraries
@@ -1,17 +1,17 @@
1
- The most common rails commands are:
2
- generate Generate new code (short-cut alias: "g")
3
- console Start the Rails console (short-cut alias: "c")
4
- server Start the Rails server (short-cut alias: "s")
5
- test Run tests except system tests (short-cut alias: "t")
6
- test:system Run system tests
7
- dbconsole Start a console for the database specified in config/database.yml
8
- (short-cut alias: "db")
1
+ Usage:
2
+ bin/rails COMMAND [options]
3
+
4
+ You must specify a command. The most common commands are:
5
+
6
+ generate Generate new code (short-cut alias: "g")
7
+ console Start the Rails console (short-cut alias: "c")
8
+ server Start the Rails server (short-cut alias: "s")
9
+ test Run tests except system tests (short-cut alias: "t")
10
+ test:system Run system tests
11
+ dbconsole Start a console for the database specified in config/database.yml
12
+ (short-cut alias: "db")
9
13
  <% unless engine? -%>
10
- new Create a new Rails application. "rails new my_app" creates a
11
- new application called MyApp in "./my_app"
12
- plugin new Create a new Rails railtie or engine
14
+ plugin new Create a new Rails railtie or engine
13
15
  <% end -%>
14
16
 
15
17
  All commands can be run with -h (or --help) for more information.
16
- In addition to those commands, there are:
17
-
@@ -6,10 +6,29 @@ module Rails
6
6
  hide_command!
7
7
 
8
8
  def help(*)
9
- say self.class.desc
9
+ say self.class.class_usage
10
+ end
11
+
12
+ def help_extended(*)
13
+ help
14
+
15
+ say ""
16
+ say "In addition to those commands, there are:"
17
+ say ""
10
18
 
11
- Rails::Command.print_commands
19
+ extended_commands = printing_commands_not_in_usage.sort_by(&:first)
20
+ print_table(extended_commands, truncate: true)
12
21
  end
22
+
23
+ private
24
+ COMMANDS_IN_USAGE = %w(generate console server test test:system dbconsole new)
25
+ private_constant :COMMANDS_IN_USAGE
26
+
27
+ def printing_commands_not_in_usage # :nodoc:
28
+ Rails::Command.printing_commands.reject do |command, _|
29
+ command.in?(COMMANDS_IN_USAGE)
30
+ end
31
+ end
13
32
  end
14
33
  end
15
34
  end
@@ -9,10 +9,7 @@ module Rails
9
9
 
10
10
  desc "initializers", "Print out all defined initializers in the order they are invoked by Rails."
11
11
  def perform
12
- extract_environment_option_from_argument
13
- ENV["RAILS_ENV"] = options[:environment]
14
-
15
- require_application_and_environment!
12
+ boot_application!
16
13
 
17
14
  Rails.application.initializers.tsort_each do |initializer|
18
15
  say "#{initializer.context_class}.#{initializer.name}"
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class MiddlewareCommand < Base # :nodoc:
6
+ desc "middleware", "Print out your Rack middleware stack"
7
+ def perform
8
+ boot_application!
9
+
10
+ Rails.configuration.middleware.each do |middleware|
11
+ say "use #{middleware.inspect}"
12
+ end
13
+ say "run #{Rails.application.class.name}.routes"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -3,6 +3,8 @@
3
3
  module Rails
4
4
  module Command
5
5
  class NewCommand < Base # :nodoc:
6
+ self.bin = "rails"
7
+
6
8
  no_commands do
7
9
  def help
8
10
  Rails::Command.invoke :application, [ "--help" ]
@@ -7,8 +7,9 @@ module Rails
7
7
  class NotesCommand < Base # :nodoc:
8
8
  class_option :annotations, aliases: "-a", desc: "Filter by specific annotations, e.g. Foobar TODO", type: :array
9
9
 
10
+ desc "notes", "Show comments in your code annotated with FIXME, OPTIMIZE, and TODO"
10
11
  def perform(*)
11
- require_application_and_environment!
12
+ boot_application!
12
13
 
13
14
  display_annotations
14
15
  end
@@ -5,6 +5,8 @@ module Rails
5
5
  class PluginCommand < Base # :nodoc:
6
6
  hide_command!
7
7
 
8
+ self.bin = "rails"
9
+
8
10
  def help
9
11
  run_plugin_generator %w( --help )
10
12
  end
@@ -9,42 +9,45 @@ module Rails
9
9
 
10
10
  class << self
11
11
  def printing_commands
12
- formatted_rake_tasks.map(&:first)
12
+ rake_tasks.filter_map do |task|
13
+ if task.comment && task.locations.any?(non_app_file_pattern)
14
+ [task.name_with_args, task.comment]
15
+ end
16
+ end
13
17
  end
14
18
 
15
19
  def perform(task, args, config)
16
- require_rake
17
-
18
- Rake.with_application do |rake|
19
- rake.init("rails", [task, *args])
20
- rake.load_rakefile
21
- if Rails.respond_to?(:root)
22
- rake.options.suppress_backtrace_pattern = /\A(?!#{Regexp.quote(Rails.root.to_s)})/
20
+ with_rake(task, *args) do |rake|
21
+ if unrecognized_task = rake.top_level_tasks.find { |task| !rake.lookup(task[/[^\[]+/]) }
22
+ @rake_tasks = rake.tasks
23
+ raise UnrecognizedCommandError.new(unrecognized_task)
23
24
  end
25
+
26
+ rake.options.suppress_backtrace_pattern = non_app_file_pattern
24
27
  rake.standard_exception_handling { rake.top_level }
25
28
  end
26
29
  end
27
30
 
28
31
  private
29
- def rake_tasks
30
- require_rake
31
-
32
- return @rake_tasks if defined?(@rake_tasks)
33
-
34
- require_application!
32
+ def non_app_file_pattern
33
+ /\A(?!#{Regexp.quote Rails::Command.root.to_s})/
34
+ end
35
35
 
36
+ def with_rake(*args, &block)
37
+ require "rake"
36
38
  Rake::TaskManager.record_task_metadata = true
37
- Rake.application.instance_variable_set(:@name, "rails")
38
- load_tasks
39
- @rake_tasks = Rake.application.tasks.select(&:comment)
40
- end
41
39
 
42
- def formatted_rake_tasks
43
- rake_tasks.map { |t| [ t.name_with_args, t.comment ] }
40
+ result = nil
41
+ Rake.with_application do |rake|
42
+ rake.init(bin, args) unless args.empty?
43
+ rake.load_rakefile
44
+ result = block.call(rake)
45
+ end
46
+ result
44
47
  end
45
48
 
46
- def require_rake
47
- require "rake" # Defer booting Rake until we know it's needed.
49
+ def rake_tasks
50
+ @rake_tasks ||= with_rake(&:tasks)
48
51
  end
49
52
  end
50
53
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rails
4
+ module Command
5
+ class RestartCommand < Base # :nodoc:
6
+ desc "restart", "Restart app by touching tmp/restart.txt"
7
+ def perform
8
+ require "fileutils"
9
+ FileUtils.mkdir_p Rails::Command.application_root.join("tmp")
10
+ FileUtils.touch Rails::Command.application_root.join("tmp/restart.txt")
11
+ end
12
+ end
13
+ end
14
+ end
@@ -8,9 +8,21 @@ module Rails
8
8
  class_option :controller, aliases: "-c", desc: "Filter by a specific controller, e.g. PostsController or Admin::PostsController."
9
9
  class_option :grep, aliases: "-g", desc: "Grep routes by a specific pattern."
10
10
  class_option :expanded, type: :boolean, aliases: "-E", desc: "Print routes expanded vertically with parts explained."
11
+ class_option :unused, type: :boolean, aliases: "-u", desc: "Print unused routes."
11
12
 
13
+ no_commands do
14
+ def invoke_command(*)
15
+ if options.key?("unused")
16
+ Rails::Command.invoke "unused_routes", ARGV
17
+ else
18
+ super
19
+ end
20
+ end
21
+ end
22
+
23
+ desc "routes", "List all the defined routes"
12
24
  def perform(*)
13
- require_application_and_environment!
25
+ boot_application!
14
26
  require "action_dispatch/routing/inspector"
15
27
 
16
28
  say inspector.format(formatter, routes_filter)