[WIP] Scriban-based code generation #80
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
A long time ago I floated the idea of using text templates to generate code instead of CodeDom.
I finally got to do a prototype. This is not ready to merge but here's what already committed:
ScribanGlobals. Those methods are used inside the template to "extract" from CodeDom information about the model being generated.Please bear in mind the template is a proof of concept but it's not final. It could be better split in smaller parts, the model could have better names, we can add some comments, etc.
It's already somewhat capable, though. I have experimented with schema
Microsoft.Search.Query.xsdwhich is reasonably large and makes use of:stringIf you run the code on this branch, the generated code is exactly the same as the current generator if you ignore whitespace differences. I was not masochist enough to match whitespace 1:1. Generating exactly the same code helps ensure there is no regression.
Tip
There is a VS Code extension to highlight the Scriban syntax
What would be the next steps?
I was surprised how quickly I was able to put this together, which speaks in favor of templates.
I am also extremely tempted to modify the templates to improve generated code, something I never felt empowered to do in CodeDom. But I won't do that before a 1:1 match of every generated file is possible and merged. Improvements are planned but will come afterwards.
The big amount of work right now is to (1) remove CodeDom and generate a data-only equivalent model; (2) support all features, this prototype is far from complete.
Things that could be possible in the future but I don't envision in this PR:
@mamift That's a drastic change but I believe it's for the better. What do you think? Should I continue working on this?