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

Generators: add ability to destroy generated files #80

Closed
cannikin opened this issue Jan 24, 2020 · 10 comments
Closed

Generators: add ability to destroy generated files #80

cannikin opened this issue Jan 24, 2020 · 10 comments

Comments

@cannikin
Copy link
Member

In Rails there is a destroy command that acts as the evil twin of generate:

rails generate scaffold Post
rails destroy scaffold Post

Let's do the same thing in Redwood. The generators themselves just return a list of files/routes so we should be able to use those lists in a destroy process to remove the files created and match and remove the routes.

@cannikin cannikin changed the title Add ability to destroy generated files Generators: add ability to destroy generated files Jan 24, 2020
@desa
Copy link

desa commented Mar 13, 2020

I'd be interested in trying to implement this. At a high level would the work be

  • Add a destroy subcommand for redwood
  • For each subcommand of generate, create a corresponding subcommand under destroy that imports the files method exported in the corresponding generate subcommand
  • Delete each of the files returned by the files method and remove any associated routes

Does that seem more or less correct?

@cannikin
Copy link
Member Author

@desa thanks for joining in! Yes that sounds right to me!

@desa
Copy link

desa commented Mar 14, 2020

Got a question about the anticipated behavior.

If I run yarn rw generate page Foo followed by yarn rw destroy scaffold Foo, what should happen? Should I delete any files that exist that would have been generated by yarn rw generate scaffold? Or error out?

@thedavidprice
Copy link
Contributor

@cannikin looping you back in on this one.

@desa are you still interested in helping with this?

@desa
Copy link

desa commented Mar 31, 2020

@thedavidprice yup, super interested in helping.

@cannikin
Copy link
Member Author

cannikin commented Mar 31, 2020

@desa I always base my generator behavior decisions on what Rails does. I believe the destroyers just use the list of files that the generator creates and attempts to remove each one. So if you created something with the page generator, then ran the scaffold destroyer, it would remove any files that had the same file name as what the scaffold generator would have created, even if they weren't created with the scaffold generator. Also we have no way of knowing if the scaffold generator created them or the user just happened to create them with the same name, so it would be futile to try and stop them from getting deleted (unless we did a diff between the file's current contents and the content the generator would have created... 🤯)

I'm all about providing sharp knives rather than creating additional code to try and anticipate everything a user could possibly do wrong and prevent it from happening.

The common use case in the Rails world for using a destroyer is that you just created something and then realize you had a typo in the name, or your model wasn't quite defined right in the schema and you want to undo and redo the generated code so it picks up the change.

@thedavidprice
Copy link
Contributor

One request:
How about "[list of files] Are you sure you want to delete these files? y/n" confirmation message?

But to @cannikin's point and use case above, if this is primarily to be used as an immediate cleanup for a mistake, then that message feels a bit redundant. Probably not annoyingly so, though.

@antonmoiseev
Copy link
Contributor

Can I help with finishing this up? I've quickly prototyped destroy command for a couple of components, it looks like something I could do.

@peterp
Copy link
Contributor

peterp commented Apr 28, 2020

@antonmoiseev That would be amazing

antonmoiseev added a commit to antonmoiseev/redwood that referenced this issue Apr 30, 2020
antonmoiseev added a commit to antonmoiseev/redwood that referenced this issue May 13, 2020
jmreidy pushed a commit to jmreidy/redwood that referenced this issue May 30, 2020
@peterp
Copy link
Contributor

peterp commented Jun 14, 2020

This is done! 🎉

@peterp peterp closed this as completed Jun 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants