C# – Writing custom Code generator for Dotnet Core

asp.net-coreccode generationdotnet-cli

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

Best Answer

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,

  • Should be outside of the web project
  • Should have Microsoft.VisualStudio.Web.CodeGeneration as a dependency
  • Custom Code generator should be packaged and added as a dependency to the web project which will make use of the code generator

dotnet 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

Related Topic