I am trying to write a custom code generator for dotnet core, but had little success so far with the limited documentation around it.
Poked around the CodeGeneration source code a bit, found out how to trigger the generators from command line and how it internally works.
Since the generators available within dotnet core wouldn't suffice my needs I tried writing my own CodeGenerator, but doesn't seem to be able to invoke it through "dotnet aspnet-codegenerator" command. Below is my custom code generator (currently has no implementation - my goal is to be able to trigger this from dotnet cli and end up with the exception),
namespace TestWebApp.CodeGenerator
{
[Alias("test")]
public class TestCodeGenerator : ICodeGenerator
{
public async Task GenerateCode(TestCodeGeneratorModel model)
{
await Task.CompletedTask;
throw new NotImplementedException();
}
}
public class TestCodeGeneratorModel
{
[Option(Name = "controllerName", ShortName = "name", Description = "Name of the controller")]
public string ControllerName { get; set; }
[Option(Name = "readWriteActions", ShortName = "actions", Description = "Specify this switch to generate Controller with read/write actions when a Model class is not used")]
public bool GenerateReadWriteActions { get; set; }
}
}
Below is how I'm trying to invoke the code generator,
dotnet aspnet-codegenerator -p . TestCodeGenerator TestController -m TestWebApp.Models.TestModel
or
dotnet aspnet-codegenerator -p . test TestController -m TestWebApp.Models.TestModel
This though, doesn't seem to work and complains about not being able to locate the custom code generator. See error message below,
Finding the generator 'TestCodeGenerator'...
No code generators found with the name 'TestCodeGenerator'
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGeneratorsLocator.GetCodeGenerator(String codeGeneratorName)
at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)
RunTime 00:00:06.23
What is it that I am missing Or what changes should I have to make for CogeGenerator to pickup my custom class?
Repro: Github
NET Code Generator is one of the most useful tools in your toolbelt. All other commercial code generators are mainly based on generating code from database schemas. The . NET Code Generator can not only generate from database schemas but also generate based on other source code classes, JSON, or CSV Files.
Create an ASP.NET Core applicationOpen that empty directory in VS Code by selecting File -> Open Folder. Open the terminal by using the shortcut Ctrl + Shift + ` or Terminal -> New Terminal. Then, it will show you the list of ASP.NET project templates. Select the web application option.
Ok. Found out what was missing in my code.
Almost everything was correct, except for the fact that Custom Code generators cannot be residing within the same assembly as web project, and the Custom Code generator should be referenced from the web project as a package reference (project reference won't work).
Below are the requirements for Custom Code generator to be visible to the dotnet cli code generator,
Microsoft.VisualStudio.Web.CodeGeneration
as a dependencydotnet pack -o ../custompackages
(make sure to add this location (../custompackages) to nuget.config)
Note: The code in my question has a model that doesn't accept Model parameter (-m switch) and expects a controllerName parameter, so, to invoke the code generator you would have to use,
dotnet aspnet-codegenerator -p . test --controllerName TestController
OR
dotnet aspnet-codegenerator -p . TestCodeGenerator --controllerName TestController
Refer related discussion here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With