bake
bake copied to clipboard
Enhance cake bake to recognize unchanged files
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.
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?
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.
You can overwrite files using the --force
option as well.
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...
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.
@lab-at-nohl You should probably only re-generate files if you never customize them. Only new files should be baked.
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.
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?
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".
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.
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.
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:.
Support for updating table and entity classes will be in bake 2.8.
Very nice. That is actually more than I expected. I will give it a try. Thanks