I ve put off using generated code as part of the build process for fear of the complexity it introduces into the build process.
Is there a simple way to integrate build-time generated code into an app?
The kind of code I m thinking of is similar to the resource and settings file code generation that Visual studio performs:
- Having intellisense here is valuable
- There are a lot of properties and links between properties that are trivial to describe, but impossible to implement tersely in C#.
- The underlying resource is be modifiable and the code is automatically regenerated without needing any user interaction and without any need to understand the internals of the generator.
For (a non-real-world) example consider a precompiler that generated accessor to the named capture groups of a Regex via similarly named C# properties (or methods). This is typical of the kinds of things I d like to generate: long snippets of boilerplate wrappers whose primary function is to enable compile time checking for errors (in the above; accessing non-existant capturing groups or writing and invalid regex) and no less importantly, intellisense for these properties. Finally, this setup should be trivially usable by others on the team with only the bare minimum of learning curve. I.e., it s absolutely not acceptable to require manual intervention to regenerate the code, nor acceptable to commit the generated code into source control. At worst, everyone should just need to install some extension; ideally the extension should be installable into the source-tree so that anyone that checks out the tree can build the project without any introduction.
For that to work well, it s critical that the IDE integration be excellent: Updating the underlying "resource" definition file should trigger a regeneration of the code without any user interaction, and ideally the generator itself would be easy to maintain for other developers later on (i.e. some amount of generator debug-ability is a plus).
Finally, an XSLT-like approach where the same template can be applied to various input resources is ideal; both because this means that you don t even need to look at the actual generator code if all you want to do is is update the resource, and because it makes template reuse trivial.
I ve looked at T4, but from what I ve seen this has a less handy ASP-like approach where template and resource aren t cleanly split (i.e, the generator is responsible for finding the resource - which makes template reuse less easy).
Is there a better (cleaner) solution or some way of running T4 such that the same template is can be trivially reused and (much like .NET settings files) that any update of the resource automatically triggers a regeneration of the implemented code?
Summary: I m looking for a code-gen approach that can
- Regenerate code automatically without dev intervention when the underlying resource (not the template!) changes.
- Be somewhat simple to maintain
- Be able to share the same generator template between several resources (which, with point #1 probably implies the resource should refer to the generator and not vice-versa).