Skip to content

Conversation

y-yagi
Copy link
Member

@y-yagi y-yagi commented May 27, 2018

This is a retry of #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.

@y-yagi y-yagi force-pushed the add_editor_option_to_generator branch 2 times, most recently from d7139f1 to 1294857 Compare June 9, 2018 00:17
@y-yagi y-yagi force-pushed the add_editor_option_to_generator branch 2 times, most recently from 20dd022 to 8900c4e Compare June 17, 2018 11:55
Copy link
Contributor

@kaspth kaspth left a comment

Choose a reason for hiding this comment

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

Nice idea!


module Rails
module Generators
module PrimaryFileHelpers # :nodoc:
Copy link
Contributor

Choose a reason for hiding this comment

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

extend ActiveSupport::Concern?

template "controller.rb", File.join("app/controllers", class_path, "#{file_name}_controller.rb")
destination = File.join("app/controllers", class_path, "#{file_name}_controller.rb")
template "controller.rb", destination
primary_file(destination)
Copy link
Contributor

Choose a reason for hiding this comment

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

This obstructs the flow a bit too much for me.

How about adding a primary_template method? Or overriding template so we can pass a primary_file: true option?

Or perhaps primary_file can just return the destination. So we can do template "controller.rb", primary_file(File.join(…))

Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to open multiple files in the editor too? It might be nice to open the whole shebang after a scaffold generator has run for instance.

Then it could make sense for template to collect files to open after the generator has finished.

Copy link
Member Author

Choose a reason for hiding this comment

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

That it depends on the editor whether or not can open multiple files (I'm not sure if it is possible with an editor without tab functions)
It is difficult to check the operation with various editors, so I would like to avoid supporting it.

template "controller.rb", primary_file(File.join(…)) seems nice. I will update.

Copy link
Member Author

Choose a reason for hiding this comment

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

My bad. In order to generate a file, need to run template first. So template "controller.rb", primary_file(File.join(…)) does not work.
Instead, I added the primary_template method.


def primary_file?
shell.base.class.to_s == "Rails::Generators::MigrationGenerator"
end
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand how these primary_file? checks work?

Copy link
Member Author

Choose a reason for hiding this comment

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

This generator will be triggered from Rails::Generators::MigrationGenerator, so this check works(Does this make the answer as expected...?).

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what you mean. Can you give an example of the complete code flow?

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it's just because I don't understand what shell or shell.base is here.

def self.included(base)
base.class_option :editor, type: :string, aliases: "-e", lazy_default: ENV["EDITOR"],
required: false, banner: "editor",
desc: "Open generated primary file in the specified editor (Default: $EDITOR)"
Copy link
Contributor

Choose a reason for hiding this comment

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

What type of generators can't support the --editor option?

Copy link
Member Author

Choose a reason for hiding this comment

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

For example, it can not support scaffold generator which can not decide primary file.

@y-yagi y-yagi force-pushed the add_editor_option_to_generator branch 2 times, most recently from f125234 to c422c20 Compare July 19, 2018 09:44
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.
@y-yagi y-yagi force-pushed the add_editor_option_to_generator branch from c422c20 to d9118db Compare July 19, 2018 09:58
included do
class_option :editor, type: :string, aliases: "-e", lazy_default: ENV["EDITOR"],
required: false, banner: "editor",
desc: "Open generated primary file in the specified editor (Default: $EDITOR)"
Copy link
Contributor

Choose a reason for hiding this comment

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

Should this really be an --editor option or a --edit option? Because we don't allow editor customization for the credentials:edit or secrets:edit commands, there $EDITOR is assumed.

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we indent arguments like this. I'd just do:

class_option :editor, type: :string, 
  required: false, 

end

def primary_file?
self.instance_of?(shell.base.class)
Copy link
Contributor

Choose a reason for hiding this comment

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

Could remove the self. here.

included do
class_option :editor, type: :string, aliases: "-e", lazy_default: ENV["EDITOR"],
required: false, banner: "editor",
desc: "Open generated primary file in the specified editor (Default: $EDITOR)"
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we indent arguments like this. I'd just do:

class_option :editor, type: :string, 
  required: false, 

def primary_template(source, destination)
template(source, destination)
primary_file(destination)
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Think this makes more sense in the PrimaryFileHelpers module.


module Rails
module Generators
module PrimaryFileHelpers # :nodoc:
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm curious about the naming choice here by the way. Why the word primary? Since there's only ever one file that's editable — and thus no secondary files — what makes it primary? Especially when this adds an editor option and not a primary_file option?

How about Rails::Generators::TemplateEditing which overrides template to support an edit: true option?

end
end
end
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Dig all these tests 😍


def primary_file?
shell.base.class.to_s == "Rails::Generators::MigrationGenerator"
end
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps it's just because I don't understand what shell or shell.base is here.

@kaspth
Copy link
Contributor

kaspth commented Sep 13, 2019

@y-yagi I just had a use for this, are you interested in picking it up again or do you want me or someone else to take over? It'd be really cool to have in 6.1 ✌️

@y-yagi
Copy link
Member Author

y-yagi commented Sep 14, 2019

Ya, I'm still interested in this. But I have another idea related to this. I'd like to think about what to do with this, so please wait ;)

@kaspth
Copy link
Contributor

kaspth commented Sep 14, 2019

Sounds great! 😄

@rails-bot
Copy link

rails-bot bot commented Dec 17, 2019

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Dec 17, 2019
@kaspth
Copy link
Contributor

kaspth commented Dec 22, 2019

@y-yagi did you get to attempt the other idea?

@rails-bot rails-bot bot removed the stale label Dec 22, 2019
@y-yagi
Copy link
Member Author

y-yagi commented Dec 22, 2019

Sorry for the late action. I plan to provide a callback to perform processing on the generated file after the file is generated(The callback will support file operations in the editor and execution of rubocop, etc).

But still working on it. y-yagi@0470943 I am going to finish this in the new year holidays.

@kaspth
Copy link
Contributor

kaspth commented Dec 22, 2019

Cool, interesting 😊

@rails-bot
Copy link

rails-bot bot commented Mar 21, 2020

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.
Thank you for your contributions.

@rails-bot rails-bot bot added the stale label Mar 21, 2020
@rails-bot rails-bot bot closed this Mar 29, 2020
@simi
Copy link
Contributor

simi commented Mar 29, 2020

@y-yagi this would be really helpful for me (and I was thinking about similar idea few times already) for generated migrations. I was looking at master...y-yagi:add_after_generate. Is there still any plan from your side for this? Is there anything I can help you with?

@kaspth
Copy link
Contributor

kaspth commented Mar 29, 2020

@simi I think this has utility enough that 2 people can work on it — as long as you don’t erase credit. Base your work on the commit in that branch, get it rebased, flesh it out more and check allow edits from maintainers on the PR. Then @y-yagi will have an easier time wrapping it up if he has time to jump in or we can wrap it up if you make progress on it 👌

@y-yagi
Copy link
Member Author

y-yagi commented Apr 3, 2020

@simi Thanks for pulling up this and sorry for the late response.
I had finished that remaining fixed and created a PR #38870. I hope you can check if that helps your issue. Thanks!

@simi
Copy link
Contributor

simi commented Apr 3, 2020

@y-yagi looking in there. Thanks for info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants