7

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

jjk_charles
  • 1,250
  • 11
  • 23

1 Answers1

5

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

jjk_charles
  • 1,250
  • 11
  • 23
  • This question it's about how write a code generator using existing objects in aspnet, but I want to write an executable which runs in dotnet cli: e.g. dotnet mygenerator arg0 arg1, can I use this solution ? – H. Herzl Jan 10 '17 at 06:48
  • @H.Herzl This solution does exactly what you want. You can control the arguments by updating the TestCodeGeneratorModel (available in question), introducing your custom properties, and decorating them with [Option] attribute. Also, why do you want to introduce a new cli command, you'll then miss out on many of the features offered by aspnet-codegenerator. – jjk_charles Jan 10 '17 at 11:05
  • I have my code generation code as nugget package and I want to allow execute from command line once it's installed, makes sense ? – H. Herzl Jan 11 '17 at 05:05
  • But my code generation isn't only for aspnet core, so I guess if I limit my code geneation for aspnet core it will be wrong, so my question is how can I add the command line interface for my existing nuget package: https://www.codeproject.com/Articles/1160615/Generating-Code-for-EF-Core-with-CatFactory – H. Herzl Jan 11 '17 at 07:19