Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Writing custom Code generator for Dotnet Core

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

like image 785
jjk_charles Avatar asked Dec 27 '16 22:12

jjk_charles


People also ask

What is .NET code generator?

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.

How do I create a .NET Core application code in Visual Studio?

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.


1 Answers

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

like image 128
jjk_charles Avatar answered Oct 02 '22 16:10

jjk_charles