mardi 27 septembre 2016

How to compute a hash of assembly, so that this hash could be used to tell if the code in the assembly has changed?

The problem is that rebuilding exactly the same code generates a different assembly when compared with the result of the previous build.

Why do I need this? I have a T4 template that generates certain source code from the given contract assembly. That source code is checked in into VCS, even though it is generated. This is because the contract assembly changes relatively infrequently. However, when it does change, I would like to fail the build as long as the aforementioned T4 template is not reevaluated.

My plan is to plant the hash code of the contract assembly in the generated source file, for example:

// 1B-D0-06-48-02-C2-C5-C5-48-37-AA-61-66-6B-6D-01

There would be an msbuild task that runs when the project containing the template is built. That task is going to compute the contract assembly hash and compare it against the one baked into the generated source code. Inequality means the contract assembly has changed and we need to rerun the template.

Storing the assembly version does not help - it is the same during the development.

Another solution would be to evaluate the template at run-time into a temporary file and copy it over the generated source file if it is different - no hash required.

However, I want to attract dev's attention to the fact that the contracts changed. At this point I want the developer to reevaluate the template manually. But only when it is needed.

I think I could write a complex code that reflects on the contract assembly to create some kind of a canonical representation of its types. Taking the hash of this canonical representation should be it. However, this is not trivial and I would like to see if it can be avoided.





Aucun commentaire:

Enregistrer un commentaire