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

Use the IRB extension API to add commands to the console #117

Merged
merged 3 commits into from
May 22, 2024

Conversation

st0012
Copy link
Contributor

@st0012 st0012 commented May 5, 2024

After rails/rails#51705, the current way to add commands to the console will no longer work as Rails::ConsoleMethods will not be directly included to the IRB internal anymore.

So this commit uses the new IRB extension API to add commands instead, which would also bring a few benefits:

  • Commands can be displayed in IRB's help message to make them easier to discover.

    Screenshot 2024-05-05 at 14 55 37
  • The parameter of connect_to doesn't need to be a string anymore as registered commands aren't treated as Ruby methods. So this now works too:

    irb(main):002> connect_to hey:resque
    Connected to hey:resque
    => nil
    

@@ -67,10 +67,9 @@ class Engine < ::Rails::Engine

console do
require "irb"
require "irb/context"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newer versions of IRB require irb/context by default, so this won't be necessary.

module MissionControl::Jobs
class ConnectTo < IRB::Command::Base
category "Mission control jobs"
description "Connect to a job server"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can also add help_message to commands to display more detailed messages when help <cmd> is called.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!!

@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "importmap-rails"
spec.add_dependency "turbo-rails"
spec.add_dependency "stimulus-rails"
spec.add_dependency "irb", "~> 1.13"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new API is added in v1.13, so it's necessary to require it.

@rosa
Copy link
Member

rosa commented May 7, 2024

I'm so sorry for the delay, @st0012. I love this!! 🤩 Amazing work on this new IRB extension API; it's super cool 🙇‍♀️

After rails/rails#51705, the current way to add
commands to the console will no longer work as `Rails::ConsoleMethods`
will not be directly included in the IRB internal anymore.

So this commit uses the new IRB extension API to add commands instead,
which would also bring a few benefits:

- Commands can be displayed in IRB's help message to make them easier to
  discover.
- The parameter of `connect_to` doesn't need to be a string anymore as
  registered commands aren't treated as Ruby methods.
@rosa rosa merged commit 2d6d7ea into rails:main May 22, 2024
10 checks passed
@st0012 st0012 deleted the use-irb-extension-api branch May 23, 2024 00:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants