Closed christoneethling closed 1 year ago
I've thought about that and sounds like a great idea for reaching a broader audience. But I wouldn't even know where to start. Do you have experience? Would you like to collaborate with a PR?
Right now there's basically CodegenCS which is a class library with helpers, and there's dotnet-codegencs which is a .NET global tool which can be invoked using any bat/cmd/ps1. What I have been doing (check this is just using PS1 scripts to invoke the global tool. In the second script you'll see I basically run codegencs run SimplePOCOGenerator.cs
. Basically the run
action of the tool will create a single-csfile csproj, add some standard references (like Newtonsoft), and run the csproj. There are some obvious flaws in this approach (e.g. how to reference other Nuget dependencies). I also made some experiments using pure Roslyn (in this source generator) but that's not much different.
I suppose the most intuitive/friendly approach for running a template would be something like:
And for extracting a database schema it could also be based on Wizard-like VS extensions:
What are your thoughts? Do you have any opensource extensions that we could refer to?
The custom file extensions already sound like you str doing and also an easy way to learn the VS SDK. Enjoy it!
@christoneethling @Rickrat I've just published the first draft for the VS extension.
Can you please test it?
If it works well please rate the extension with 5 stars and share with your friends.
Did you have a chance to try the extension? I've just published v3.1 (for Visual Studio 2022 only).
One new cool stuff is dependency injection (that can load files - path is relative to the template:
public class MyTemplate
{
void Main(ICodegenContext context, IModelFactory factory)
{
var model = factory.LoadModelFromFile<OpenApiDocument>("petstore-openapi3.json");
foreach (var entity in model.Definitions)
context[entity.Key + ".cs"].WriteLine(GenerateEntity(entity.Key, entity.Value));
}
FormattableString GenerateEntity(string definitionName, NJsonSchema.JsonSchema schema)
{
return $$"""
namespace MyNamespace
{
public class {{definitionName}}
{
// my properties...
{{ schema.Properties.Select(prop => GenerateProperty(prop)) }}
}
}
""";
}
FormattableString GenerateProperty(KeyValuePair<string, NJsonSchema.JsonSchemaProperty> prop)
{
return $$"""
xx
""";
}
}
Hi there, I was just wondering if there was ever a consideration to make this tool a visual studio extension? What would you consider the pro's and cons of the alternative approaches? Thanks