Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consistently format commands in help messages #45716

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions railties/lib/rails/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require "thor"
require "erb"

require "active_support/core_ext/class/attribute"
require "active_support/core_ext/module/delegation"
require "active_support/core_ext/string/filters"
require "active_support/core_ext/string/inflections"

Expand Down Expand Up @@ -34,6 +36,8 @@ def corrections

include Actions

class_attribute :bin, instance_accessor: false, default: "bin/rails"

class << self
def exit_on_failure? # :nodoc:
false
Expand Down Expand Up @@ -91,8 +95,8 @@ def printing_commands
namespaced_commands
end

def executable
"rails #{command_name}"
def executable(subcommand = nil)
"#{bin} #{command_name}#{":" if subcommand}#{subcommand}"
end

# Use Rails' default banner.
Expand Down Expand Up @@ -172,6 +176,10 @@ def namespaced_commands
end
end

no_commands do
delegate :executable, to: :class
end

def help
if command_name = self.class.command_name
self.class.command_help(shell, command_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ module Command
class ApplicationCommand < Base # :nodoc:
hide_command!

self.bin = "rails"

def help
perform # Punt help output to the generator.
end
Expand Down
12 changes: 6 additions & 6 deletions railties/lib/rails/commands/credentials/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ that just contains the secret_key_base used by MessageVerifiers/MessageEncryptor
signing and encrypting cookies.

For applications created prior to Rails 5.2, we'll automatically generate a new
credentials file in `config/credentials.yml.enc` the first time you run `bin/rails credentials:edit`.
credentials file in `config/credentials.yml.enc` the first time you run `<%= executable(:edit) %>`.
If you didn't have a master key saved in `config/master.key`, that'll be created too.

Don't lose this master key! Put it in a password manager your team can access.
Expand All @@ -32,20 +32,20 @@ You could prepend that to your server's start command like this:

=== Set up Git to Diff Credentials

Rails provides `bin/rails credentials:diff --enroll` to instruct Git to call
`bin/rails credentials:diff` when `git diff` is run on a credentials file.
Rails provides `<%= executable(:diff) %> --enroll` to instruct Git to call
`<%= executable(:diff) %>` when `git diff` is run on a credentials file.

Running the command enrolls the project such that all credentials files use the
"rails_credentials" diff driver in .gitattributes.

Additionally since Git requires the driver itself to be set up in a config file
that isn't tracked Rails automatically ensures it's configured when running
`credentials:edit`.
`<%= executable(:edit) %>`.

Otherwise each co-worker would have to run enable manually, including on each new
repo clone.

To disenroll from this feature, run `bin/rails credentials:diff --disenroll`.
To disenroll from this feature, run `<%= executable(:diff) %> --disenroll`.

=== Editing Credentials

Expand All @@ -62,7 +62,7 @@ The `credentials` command supports passing an `--environment` option to create a
environment specific override. That override will take precedence over the
global `config/credentials.yml.enc` file when running in that environment. So:

bin/rails credentials:edit --environment development
<%= executable(:edit) %> --environment development

will create `config/credentials/development.yml.enc` with the corresponding
encryption key in `config/credentials/development.key` if the credentials file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def edit
require_application!
load_generators

ensure_editor_available(command: "bin/rails credentials:edit") || (return)
ensure_editor_available(command: executable(:edit)) || (return)

ensure_encryption_key_has_been_added
ensure_credentials_have_been_added
Expand Down Expand Up @@ -114,9 +114,9 @@ def warn_if_credentials_are_invalid

def missing_credentials_message
if !credentials.key?
"Missing '#{key_path}' to decrypt credentials. See `bin/rails credentials:help`"
"Missing '#{key_path}' to decrypt credentials. See `#{executable(:help)}`"
else
"File '#{content_path}' does not exist. Use `bin/rails credentials:edit` to change that."
"File '#{content_path}' does not exist. Use `#{executable(:edit)}` to change that."
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def enroll_project_in_credentials_diffing
gitattributes.write(GITATTRIBUTES_ENTRY, mode: "a")

say "Enrolled project in credentials file diffing!"
say "Rails ensures the rails_credentials diff driver is set when running `credentials:edit`. See `credentials:help` for more."
say "Rails ensures the rails_credentials diff driver is set when running `#{executable(:edit)}`. See `#{executable(:help)}` for more."
end
end

Expand Down Expand Up @@ -42,7 +42,7 @@ def diffing_driver_configured?
end

def configure_diffing_driver
system "git config diff.rails_credentials.textconv 'bin/rails credentials:diff'"
system "git config diff.rails_credentials.textconv '#{executable(:diff)}'"
end

def gitattributes
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/dev/dev_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Command
class DevCommand < Base # :nodoc:
no_commands do
def help
say "rails dev:cache # Toggle development mode caching on/off."
say "#{executable(:cache)} # Toggle development mode caching on/off."
end
end

Expand Down
6 changes: 3 additions & 3 deletions railties/lib/rails/commands/encrypted/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ See the `Rails.application.encrypted` documentation for using them in your app.
By default, Rails looks for the encryption key in `config/master.key` or
`ENV["RAILS_MASTER_KEY"]`, but that lookup can be overridden with `--key`:

rails encrypted:edit config/encrypted_file.yml.enc --key config/encrypted_file.key
<%= executable(:edit) %> config/encrypted_file.yml.enc --key config/encrypted_file.key

Don't commit the key! Add it to your source control's ignore file. If you use
Git, Rails handles this for you.
Expand All @@ -17,12 +17,12 @@ Git, Rails handles this for you.

To edit or create an encrypted file use:

rails encrypted:edit config/encrypted_file.yml.enc
<%= executable(:edit) %> config/encrypted_file.yml.enc

This opens a temporary file in `$EDITOR` with the decrypted contents for editing.

=== Viewing Files

To print the decrypted contents of an encrypted file use:

rails encrypted:show config/encrypted_file.yml.enc
<%= executable(:show) %> config/encrypted_file.yml.enc
6 changes: 3 additions & 3 deletions railties/lib/rails/commands/encrypted/encrypted_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def help
def edit(*)
require_application!

ensure_editor_available(command: "bin/rails encrypted:edit") || (return)
ensure_editor_available(command: executable(:edit)) || (return)
ensure_encryption_key_has_been_added
ensure_encrypted_configuration_has_been_added

Expand Down Expand Up @@ -96,9 +96,9 @@ def encrypted_file_generator

def missing_encrypted_configuration_message
if !encrypted_configuration.key?
"Missing '#{key_path}' to decrypt data. See `bin/rails encrypted:help`"
"Missing '#{key_path}' to decrypt data. See `#{executable(:help)}`"
else
"File '#{content_path}' does not exist. Use `bin/rails encrypted:edit #{content_path}` to change that."
"File '#{content_path}' does not exist. Use `#{executable(:edit)} #{content_path}` to change that."
end
end
end
Expand Down
2 changes: 2 additions & 0 deletions railties/lib/rails/commands/new/new_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Rails
module Command
class NewCommand < Base # :nodoc:
self.bin = "rails"

no_commands do
def help
Rails::Command.invoke :application, [ "--help" ]
Expand Down
2 changes: 2 additions & 0 deletions railties/lib/rails/commands/plugin/plugin_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Command
class PluginCommand < Base # :nodoc:
hide_command!

self.bin = "rails"

def help
run_plugin_generator %w( --help )
end
Expand Down
2 changes: 1 addition & 1 deletion railties/lib/rails/commands/runner/runner_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def perform(code_or_file = nil, *command_argv)
end
rescue SyntaxError, NameError => e
error "Please specify a valid ruby command or the path of a script to run."
error "Run '#{self.class.executable} -h' for help."
error "Run '#{executable} -h' for help."
error ""
error e
exit 1
Expand Down
8 changes: 4 additions & 4 deletions railties/lib/rails/commands/secrets/USAGE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

Rails 5.2 has introduced a new `credentials` API that replaces Rails secrets.
Please use the Rails `credentials` commands instead.
Run `rails credentials:help` for more information.
Run `bin/rails credentials:help` for more information.

=== Storing Encrypted Secrets in Source Control

Expand All @@ -13,7 +13,7 @@ with the code.

=== Setup

Run `bin/rails secrets:setup` to opt in and generate the `config/secrets.yml.key`
Run `<%= executable(:setup) %>` to opt in and generate the `config/secrets.yml.key`
and `config/secrets.yml.enc` files.

The latter contains all the keys to be encrypted while the former holds the
Expand Down Expand Up @@ -51,12 +51,12 @@ the key. Add this:

config.read_encrypted_secrets = true

to the environment you'd like to read encrypted secrets. `bin/rails secrets:setup`
to the environment you'd like to read encrypted secrets. `<%= executable(:setup) %>`
inserts this into the production environment by default.

=== Editing Secrets

After `bin/rails secrets:setup`, run `bin/rails secrets:edit`.
After `<%= executable(:setup) %>`, run `<%= executable(:edit) %>`.

That command opens a temporary file in `$EDITOR` with the decrypted contents of
`config/secrets.yml.enc` to edit the encrypted secrets.
Expand Down
4 changes: 2 additions & 2 deletions railties/lib/rails/commands/secrets/secrets_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def edit
if ENV["EDITOR"].to_s.empty?
say "No $EDITOR to open decrypted secrets in. Assign one like this:"
say ""
say %(EDITOR="mate --wait" rails secrets:edit)
say %(EDITOR="mate --wait" #{executable(:edit)})
say ""
say "For editors that fork and exit immediately, it's important to pass a wait flag,"
say "otherwise the secrets will be saved immediately with no chance to edit."
Expand Down Expand Up @@ -56,7 +56,7 @@ def show
private
def deprecate_in_favor_of_credentials_and_exit
say "Encrypted secrets is deprecated in favor of credentials. Run:"
say "rails credentials:help"
say "bin/rails credentials:help"

exit 1
end
Expand Down
12 changes: 6 additions & 6 deletions railties/lib/rails/commands/server/server_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def environment
end

def restart_command
"bin/rails server #{@original_options.join(" ")} --restart"
"#{executable} #{@original_options.join(" ")} --restart"
end

def early_hints
Expand All @@ -246,7 +246,7 @@ def pid
end

def self.banner(*)
"rails server -u [thin/puma/webrick] [options]"
"#{executable} -u [thin/puma/webrick] [options]"
end

def prepare_restart
Expand All @@ -260,15 +260,15 @@ def rack_server_suggestion(server)

gem "#{RECOMMENDED_SERVER}"

Run `bin/rails server --help` for more options.
Run `#{executable} --help` for more options.
MSG
elsif server.in?(RACK_SERVERS)
<<~MSG
Could not load server "#{server}". Maybe you need to the add it to the Gemfile?

gem "#{server}"

Run `bin/rails server --help` for more options.
Run `#{executable} --help` for more options.
MSG
else
error = CorrectableError.new("Could not find server '#{server}'.", server, RACK_SERVERS)
Expand All @@ -279,7 +279,7 @@ def rack_server_suggestion(server)
end
<<~MSG
#{formatted_message}
Run `bin/rails server --help` for more options.
Run `#{executable} --help` for more options.
MSG
end
end
Expand All @@ -288,7 +288,7 @@ def print_boot_information(server, url)
say <<~MSG
=> Booting #{ActiveSupport::Inflector.demodulize(server)}
=> Rails #{Rails.version} application starting in #{Rails.env} #{url}
=> Run `bin/rails server --help` for more startup options
=> Run `#{executable} --help` for more startup options
MSG
end
end
Expand Down
16 changes: 16 additions & 0 deletions railties/test/command/base_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def command
assert_equal [], Rails::Command::HiddenCommand.printing_commands
end

test "::executable returns bin and command name" do
assert_equal "bin/rails generate", Rails::Command::GenerateCommand.executable
end

test "::executable integrates subcommand when given" do
assert_equal "bin/rails generate:help", Rails::Command::GenerateCommand.executable(:help)
end

test "::executable integrates ::bin" do
class Rails::Command::CustomBinCommand < Rails::Command::Base
self.bin = "FOO"
end

assert_equal "FOO custom_bin", Rails::Command::CustomBinCommand.executable
end

test "ARGV is populated" do
class Rails::Command::ArgvCommand < Rails::Command::Base
def check_populated(*args)
Expand Down