Redwood: Generators: add ability to destroy generated files

Created on 24 Jan 2020  路  10Comments  路  Source: redwoodjs/redwood

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.

good first issue kinimprovement generators

All 10 comments

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?

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

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?

@cannikin looping you back in on this one.

@desa are you still interested in helping with this?

@thedavidprice yup, super interested in helping.

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

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.

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.

@antonmoiseev That would be amazing

This is done! 馃帀

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CR1AT0RS picture CR1AT0RS  路  4Comments

freddydumont picture freddydumont  路  3Comments

balaji-balu picture balaji-balu  路  3Comments

aldonline picture aldonline  路  3Comments

josteph picture josteph  路  3Comments