bake icon indicating copy to clipboard operation
bake copied to clipboard

Enhance cake bake to recognize unchanged files

Open lab-at-nohl opened this issue 4 years ago • 12 comments

This is a (multiple allowed):

  • [ ] bug

  • [x] enhancement

  • [ ] feature-discussion (RFC)

  • CakePHP Version: 4.0.0

  • Platform and Target:

What you did

rerun bin/cake bake ... after changing the schema

What happened

cake asks for permission to overwrite existing files

What you expected to happen

cake will overwrite files which have been baked before without confirmation

Possible solution: cake bake will store hashes of automatically created files. If file haven't been touched when running bake again, just overwrite it. Background: At least beginners experiment a lot, confusion about changed/unchanged files happens. A lot of work can get lost.

lab-at-nohl avatar Sep 22 '20 21:09 lab-at-nohl

At least beginners experiment a lot, confusion about changed/unchanged files happens. A lot of work can get lost.

How will this help work being lost?

othercorey avatar Sep 22 '20 22:09 othercorey

I also disagree with this. It adds a complexity and other pitfalls.

Instead, one should have committed the files (via Git for example), and then can easily run -o (overwrite) to quickly regenerate, verify the diff (changes), apply certain undos (undo undesired hunks or reapply already done customizations from the history diff). Thats how everyone pretty much does it quite efficiently these days.

dereuromark avatar Sep 22 '20 22:09 dereuromark

You can overwrite files using the --force option as well.

markstory avatar Sep 23 '20 01:09 markstory

Hm, I still think bake should not ask to overwrite untouched files. At least I got used to overwrite all. I could go the other way around, how to see from outside if a file has been changed? One could go with commits, sure, but who does as many commits when starting something new?

I just returned to cake after some years (2.x or so). I still find the number of files confusing, you cannot look inside all before bake. Maybe I should quit using cake bake then...

lab-at-nohl avatar Sep 23 '20 23:09 lab-at-nohl

using the --force option

oh, yes, not -o, but -f (I confuse them sometimes when not checking the CLI help)

should not ask to overwrite untouched files

untouched is a very difficult word. What if the bake templates or helper code to generate those changed? Even a single white space change would automatically make your hash solution void. In the end there is no alternative to what was already said - and using the force option is 2 char extra to have already full overwrite possibility

I don't think there is more to be done here at this point.

dereuromark avatar Sep 23 '20 23:09 dereuromark

@lab-at-nohl You should probably only re-generate files if you never customize them. Only new files should be baked.

othercorey avatar Sep 24 '20 22:09 othercorey

Hm, I still think bake should not ask to overwrite untouched files. At least I got used to overwrite all. I could go the other way around, how to see from outside if a file has been changed? One could go with commits, sure, but who does as many commits when starting something new?

Ah ok, this ask wasn't clear to me before. Having bake check if the contents of the current file matches the new contents and not overwrite seems doable to me. It would have the limitations that @dereuromark mentioned around only being able to do hash/naive checks though.

markstory avatar Sep 25 '20 01:09 markstory

I still don't understand how that avoids losing work. If the file is unchanged, what work is lost? Isn't work lost when the file is changed?

othercorey avatar Sep 25 '20 05:09 othercorey

I still don't understand how that avoids losing work.

It doesn't.

The only thing that adding the check would do is avoid having to show message to user asking whether file should be overwritten. Instead bake could just say "The contents are same and recreating file has been skipped".

ADmad avatar Sep 25 '20 06:09 ADmad

The only thing that adding the check would do is avoid having to show message to user asking whether file should be overwritten. Instead bake could just say "The contents are same and recreating file has been skipped".

Sure. We might have an issue with newlines if the entire file is hashed.

othercorey avatar Sep 25 '20 06:09 othercorey

Ok, thanks for all the feedback, maybe I did not explain well. I try to make my scenario more clear:

I designed schema in db, used cake bake für models and controller, especially to get associations straight. Then I added new fields in db, nothing very relevant. I did this several times to put my needs together - each time baking the model by confirming y to overwrite at least 4 files (table, entity, test and ...). I do this as a basic test to see if all went well. This kind of work wasn't serious enough for an inital commit or backup. However, I got very used to overwrite all files (although I forbid myself to use a). Finally I changed an association (in a different model) and rebaked also the beforementioned model therefore. What I forgot was that I - as I read it somehwere for advice - had moved complex validation logic from controller to the model. After overwriting the model, this was gone. Actually I had a similar case with templates, too.

The feature I missed would look like this: Whenever something is baked, in the base folder a file like ".autobake" is created/modified with file name(s) and hashes of new files as key-value table. If the developer changes the file, it is most likely for a good reason (and worth to ask before bake overwrites this file). If the file is completly untouched, bake will overwrite it without any further notification (thus: bake updates it) - it was created automatically before, it can be generated automatically again.

You my call it a convenience enhancement. However, for users like me it would be of help.

lab-at-nohl avatar Sep 25 '20 12:09 lab-at-nohl

So I guess the issue is that devs get habituated to always select "overwrite" during initial development and then eventually mistakenly overwrite even if they have made customizations to the file.

Having bake not prompt for overwriting when file contents haven't actually been changed would hopefully avoid devs getting into the habit of blindly selecting to overwrite.

I don't mind if someone wants to put in the effort to implement this :slightly_smiling_face:.

ADmad avatar Sep 25 '20 14:09 ADmad

Support for updating table and entity classes will be in bake 2.8.

othercorey avatar Sep 09 '22 01:09 othercorey

Very nice. That is actually more than I expected. I will give it a try. Thanks

lab-at-nohl avatar Sep 09 '22 08:09 lab-at-nohl