-
Notifications
You must be signed in to change notification settings - Fork 22k
Updated #5829: Added --editor (-e) option to open all generated & copied files in the user's editor #8553
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
Updated #5829: Added --editor (-e) option to open all generated & copied files in the user's editor #8553
Conversation
@@ -251,6 +251,27 @@ def extify(name) | |||
end | |||
end | |||
|
|||
# Opens generated template in editor if --editor option is given. | |||
# |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✂️
👍 Like it. |
OK, have updated the commit. Sorry about that, I had started working from the |
Very cool. |
def copy_file(source, *args, &block) | ||
super | ||
destination = args.first || source | ||
open_file_in_editor(destination) if options["editor"].present? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can wrap the two lines in the if condition, in both methods.
It'll also needs some tests, and a changelog entry. |
@carlosantoniodasilva, I have added tests and a changelog entry. Thanks |
P.S. Is there any chance this could also be accepted into |
@ndbroadbent sorry, 3-2 doesn't get any new feature, only bug fixes now. But don't worry, 4.0 beta is coming ;). I'll take a look later. Thanks. |
/cc @rafaelfranca |
This is already out of date. @ndbroadbent if you want this in 3-2-stable, you can run rails with your own custom patches pretty easily. |
@steveklabnik - rebased, and thanks for the tip! |
Like it! |
My idea is not to open all the files, only the main one for each generator. For example in the model generator we can open only the model file. We don't need to open the fixture file. |
@ndbroadbent Hey did you got time to work on this again? |
Hey @arunagw, thanks for the reminder! I've rebased and rewritten this so that it only opens the main file for each generator. |
# open_file_in_editor "app/models/account.rb" | ||
def open_file_in_editor(path) | ||
# Attempt to only open the main file generated by the base generator. | ||
if shell.base.class === self || shell.base.class.invocations.key?(:orm) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure about this line, but it does the job. If you run the controller generator, it will only open the controller file in the editor, and ignore the helper file. But if you run the helper generator, it opens the helper file in your editor. The :orm
condition is so that the active_record migration and model generators are allowed to open their files.
This is almost a year stale now. Do we want to try again to get it merged, or give up and just close it? |
The current implementation seems pretty intrusive... maybe it would feel better if we had a And the parameter description needs an update ("all"). Other than that, I'm 👍 to merge this. Trying to tab-complete the filename of a just-created migration is not fun. |
I like the idea here 👍 Lets merge this, and iterate over it. |
@matthewd not sure if I got you suggestion. Are you saying to extract the open in editor code to a new method or are you thinking in a more generic handler for the main file? |
@rafaelfranca the latter. Nothing complex... but just some agnostic way of knowing which file is the interesting one. Maybe as simple as def create_mailer_file
destination = File.join('app/mailers', class_path, "#{file_name}.rb")
template "mailer.rb", destination
primary_file destination
end
[..]
def primary_file(filename)
# I'm not sure exactly what, but I can see other things wanting to hook in here
open_file_in_editor(filename) if options["editor"].present?
end Largely, it seems neater to keep the raw "open in editor" concept out of the individual generators. I'm not sure how, but the logic in https://github.com/rails/rails/pull/8553/files#r5010513 could hopefully live there too. Maybe? |
def open_file_in_editor(path) | ||
# Attempt to only open the main file generated by the base generator. | ||
if shell.base.class === self || shell.base.class.invocations.key?(:orm) | ||
run("#{options["editor"]} \"#{path}\"") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have you tested with command-line editor like vim
? Although it opens template, the generator is most likely still running and also outputting to the terminal.
Forcing a rebuild. |
Ok well I can't get travis to run it, but I pulled this branch down locally and it has failing tests. @ndbroadbent are you still interested in working on this? |
Closing due to inactivity. |
This is a retry of rails#8553. Added support for generators added later from the original PR, and extract the function on the primary file into method. Also avoied adding the `editor` option to `Generators::Base`. If add it to `Generators::Base`, `editor` option will be output to the help of generator which does not support it. This confuses the user.
This is an updated version of #5829, with @rafaelfranca's proposal to make it higher level.
The
--editor
option is now aliased as-e
, and is available for all generators that inherit fromRails::Generators::Base
. The default editor is nowGUI_EDITOR
, and falls back toEDITOR
.It wraps Thor's
template
andcopy_file
actions, and opens generated/copied files in your text editor ifoptions["editor"]
is present.P.S. I initially tried to just open the main generated files, but I think it's better to just open them all. If you run the model generator, you'll probably want to edit the migration, model, and tests. (Instead of just the model.)