Updated #5829: Added --editor (-e) option to open all generated & copied files in the user's editor #8553

Closed
wants to merge 1 commit into
from

Projects

None yet
@ndbroadbent
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.)

@frodsan frodsan commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/actions.rb
@@ -251,6 +251,27 @@ def extify(name)
end
end
+ # Opens generated template in editor if --editor option is given.
+ #
@frodsan
frodsan Dec 19, 2012 Contributor

✂️

@frodsan frodsan commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/actions.rb
@@ -251,6 +251,27 @@ def extify(name)
end
end
+ # Opens generated template in editor if --editor option is given.
+ #
+ def template(source, *args, &block)
+ super
+ destination = args.first || source.sub(/\.tt$/, '')
+ open_file_in_editor(destination) if options["editor"].present?
+ end
+
+ # Opens copied files in editor if --editor option is given.
+ #
@frodsan
frodsan Dec 19, 2012 Contributor

✂️

@frodsan frodsan commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/actions.rb
@@ -251,6 +251,27 @@ def extify(name)
end
end
+ # Opens generated template in editor if --editor option is given.
+ #
+ def template(source, *args, &block)
+ super
+ destination = args.first || source.sub(/\.tt$/, '')
+ open_file_in_editor(destination) if options["editor"].present?
+ end
+
+ # Opens copied files in editor if --editor option is given.
@frodsan
frodsan Dec 19, 2012 Contributor

change --editor to <tt>--editor</tt>.

@frodsan frodsan commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/actions.rb
@@ -251,6 +251,27 @@ def extify(name)
end
end
+ # Opens generated template in editor if --editor option is given.
@frodsan
frodsan Dec 19, 2012 Contributor

change --editor to <tt>--editor</tt>

@frodsan frodsan and 1 other commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/actions.rb
+ #
+ def template(source, *args, &block)
+ super
+ destination = args.first || source.sub(/\.tt$/, '')
+ open_file_in_editor(destination) if options["editor"].present?
+ end
+
+ # Opens copied files in editor if --editor option is given.
+ #
+ def copy_file(source, *args, &block)
+ super
+ destination = args.first || source
+ open_file_in_editor(destination) if options["editor"].present?
+ end
+
+ private
@frodsan
frodsan Dec 19, 2012 Contributor

Please, follow the Coding Conventions guide.

  def public_method
  end

  private

    def private_method
    end
@ndbroadbent
ndbroadbent Dec 19, 2012 Contributor

These previous methods are protected already, so the private needs to be un-indented?

@frodsan
frodsan Dec 19, 2012 Contributor

Oops, sorry I didn't note that.

@frodsan frodsan commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/base.rb
@@ -21,6 +21,10 @@ class Base < Thor::Group
add_runtime_options!
strict_args_position!
+ class_option :editor, :type => :string, :aliases => "-e", :lazy_default => ENV['GUI_EDITOR'] || ENV['EDITOR'],
@frodsan
frodsan Dec 19, 2012 Contributor

Use 1.9 hash syntax for new features, Thanks 😄

@frodsan
Contributor
frodsan commented Dec 19, 2012

👍 Like it.

@ndbroadbent
Contributor

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
Contributor

Very cool.

@carlosantoniodasilva carlosantoniodasilva commented on an outdated diff Dec 19, 2012
railties/lib/rails/generators/actions.rb
@@ -251,6 +251,25 @@ def extify(name)
end
end
+ # Opens generated template in editor if <tt>--editor</tt> option is given.
+ def template(source, *args, &block)
+ super
+ destination = args.first || source.sub(/\.tt$/, '')
+ open_file_in_editor(destination) if options["editor"].present?
+ end
+
+ # Opens copied files in editor if <tt>--editor</tt> option is given.
+ def copy_file(source, *args, &block)
+ super
+ destination = args.first || source
+ open_file_in_editor(destination) if options["editor"].present?
@carlosantoniodasilva
carlosantoniodasilva Dec 19, 2012 Member

You can wrap the two lines in the if condition, in both methods.

@carlosantoniodasilva

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

@ndbroadbent
Contributor

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

@ndbroadbent
Contributor

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

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

@steveklabnik
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
Contributor

@steveklabnik - rebased, and thanks for the tip!

@kinopyo
Contributor
kinopyo commented Mar 1, 2013

Like it!

@rafaelfranca
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
Member
arunagw commented May 3, 2013

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

@ndbroadbent ndbroadbent Added --editor (-e) option to open the primary generated files in the…
… user's editor
09e19ec
@ndbroadbent
Contributor

Hey @arunagw, thanks for the reminder! I've rebased and rewritten this so that it only opens the main file for each generator.

@ndbroadbent ndbroadbent commented on the diff Jul 3, 2013
railties/lib/rails/generators/actions.rb
@@ -232,6 +232,16 @@ def readme(path)
log File.read(find_in_source_paths(path))
end
+ # Opens the given file in the user's editor, if generator is run with the <tt>--editor</tt> option.
+ #
+ # 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)
@ndbroadbent
ndbroadbent Jul 3, 2013 Contributor

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
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
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
Member

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

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

@repinel repinel commented on the diff Jun 14, 2015
railties/lib/rails/generators/actions.rb
@@ -232,6 +232,16 @@ def readme(path)
log File.read(find_in_source_paths(path))
end
+ # Opens the given file in the user's editor, if generator is run with the <tt>--editor</tt> option.
+ #
+ # 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)
+ run("#{options["editor"]} \"#{path}\"")
@repinel
repinel Jun 14, 2015 Contributor

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 sgrif was assigned by rails-bot Oct 20, 2015
@sgrif
Member
sgrif commented Oct 30, 2015

Forcing a rebuild.

@sgrif sgrif closed this Oct 30, 2015
@sgrif sgrif reopened this Oct 30, 2015
@sgrif
Member
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
Member
sgrif commented Dec 8, 2016

Closing due to inactivity.

@sgrif sgrif closed this Dec 8, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment