Skip to content

Conversation

ndbroadbent
Copy link
Contributor

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 from Rails::Generators::Base. The default editor is now GUI_EDITOR, and falls back to EDITOR.

It wraps Thor's template and copy_file actions, and opens generated/copied files in your text editor if options["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.)

@@ -251,6 +251,27 @@ def extify(name)
end
end

# Opens generated template in editor if --editor option is given.
#
Copy link
Contributor

Choose a reason for hiding this comment

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

✂️

@frodsan
Copy link
Contributor

frodsan commented Dec 19, 2012

👍 Like it.

@ndbroadbent
Copy link
Contributor Author

OK, have updated the commit. Sorry about that, I had started working from the 3-2-stable branch, and forgot to update the coding style :)

@tilsammans
Copy link
Contributor

Very cool.

def copy_file(source, *args, &block)
super
destination = args.first || source
open_file_in_editor(destination) if options["editor"].present?

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.

@carlosantoniodasilva
Copy link
Member

It'll also needs some tests, and a changelog entry.

@ndbroadbent
Copy link
Contributor Author

@carlosantoniodasilva, I have added tests and a changelog entry. Thanks

@ndbroadbent
Copy link
Contributor Author

P.S. Is there any chance this could also be accepted into 3-2-stable? I've prepared a open_generated_files_in_editor_32stable branch if so.

@carlosantoniodasilva
Copy link
Member

@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.

@carlosantoniodasilva
Copy link
Member

/cc @rafaelfranca

@steveklabnik
Copy link
Member

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.

@ndbroadbent
Copy link
Contributor Author

@steveklabnik - rebased, and thanks for the tip!

@kinopyo
Copy link
Contributor

kinopyo commented Mar 1, 2013

Like it!

@rafaelfranca
Copy link
Member

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.

@arunagw
Copy link
Member

arunagw commented May 3, 2013

@ndbroadbent Hey did you got time to work on this again?

@ndbroadbent
Copy link
Contributor Author

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)
Copy link
Contributor Author

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.

@mikegee
Copy link
Contributor

mikegee commented May 21, 2014

This is almost a year stale now. Do we want to try again to get it merged, or give up and just close it?

@matthewd
Copy link
Member

The current implementation seems pretty intrusive... maybe it would feel better if we had a primary_template (or similar name) method that would call template, plus consider any other special handling that might be due to the "main" output of the task (such as, say, opening it in an editor).

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.

@arthurnn
Copy link
Member

I like the idea here 👍 Lets merge this, and iterate over it.

@rafaelfranca
Copy link
Member

@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?

@matthewd
Copy link
Member

matthewd commented Jan 3, 2015

@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}\"")
Copy link
Member

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.

@sgrif
Copy link
Contributor

sgrif commented Oct 30, 2015

Forcing a rebuild.

@sgrif sgrif closed this Oct 30, 2015
@sgrif sgrif reopened this Oct 30, 2015
@sgrif
Copy link
Contributor

sgrif commented Oct 30, 2015

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?

@sgrif
Copy link
Contributor

sgrif commented Dec 8, 2016

Closing due to inactivity.

@sgrif sgrif closed this Dec 8, 2016
y-yagi added a commit to y-yagi/rails that referenced this pull request Jul 19, 2018
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.