copier-org / copier

Library and command-line utility for rendering projects templates.
https://readthedocs.org/projects/copier/
MIT License
1.96k stars 176 forks source link

Conditional messages for before/after copy/update #1757

Open farhaan-shamsee opened 1 month ago

farhaan-shamsee commented 1 month ago

Actual Situation

Currently the message_before_update and similar messages gets printed on every copy or update.

There can be the case where I want some specific message to be printed when a condition is met. I think this is not possible currently.

Desired Situation

In my case, I have different versions of template. So when a user is updating from 1.0.0 to 2.0.0, I want a message to be printed, for example to take backup of a file, as it will be a breaking change.

But if the user is already on 2.0.0, and runs update to change some answers, then I do not want the same message to be displayed.

Or in short the update message should be displayed on the first update only of a particular template version.

Proposed solution

As a workaround for my use-case, I was trying to get the _copier_answers._commit value from the answers file and then apply some logic to check if the version of template was changing (as it will only change if I am updating to new template version). But it seems that the _commit value changes even before the update command gets executed. So I do not have any way of getting the previous value of _commit.

pawamoy commented 1 month ago

Sounds like migrations. You'd have a migration between v1 and v2 that does the file backup for you and prints a message.

yajo commented 4 weeks ago

It could be done with migrations, but in reality those run at the end of the update. However the feature request is to have messages at the beginning AFAICS.

farhaan-shamsee commented 3 weeks ago

Sounds like migrations. You'd have a migration between v1 and v2 that does the file backup for you and prints a message.

Yes, this can resolve/help in my use case but partially. As mentioned by @yajo , there can be some use cases where we want the messages to be printed before also.

yajo commented 1 week ago

Can't you just add a message like: "If you are doing a major version update, make sure to check the template change log."?

farhaan-shamsee commented 5 days ago

Yes, in fact this is what we are doing currently, redirecting them to our release notes. And also looking at the migrations as mentioned above. I believe this fulfils our requirement.