为Dotnet Core编写自定义代码生成器 [英] Writing custom Code generator for Dotnet Core

查看:835
本文介绍了为Dotnet Core编写自定义代码生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为dotnet核心编写一个自定义代码生成器,但是到目前为止,围绕它的文档有限,效果不佳.

仔细研究一下CodeGeneration源代码,了解如何从命令行触发生成器以及其内部工作方式.

由于dotnet核心中可用的生成器无法满足我的需求,因此我尝试编写自己的CodeGenerator,但似乎无法通过"dotnet aspnet-codegenerator"命令来调用它.下面是我的自定义代码生成器(当前没有实现-我的目标是能够从dotnet cli触发此代码并以异常结束),

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; }
    }
}

下面是我尝试调用代码生成器的方式,

dotnet aspnet-codegenerator -p . TestCodeGenerator TestController -m TestWebApp.Models.TestModel

dotnet aspnet-codegenerator -p . test TestController -m TestWebApp.Models.TestModel

但是,这似乎不起作用,并且抱怨找不到自定义代码生成器.请参阅下面的错误消息,

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

我想念的是什么?或者我需要对CogeGenerator进行哪些更改以提取自定义类?

复制: Github

解决方案

好.找出我的代码中缺少的内容.

几乎所有内容都是正确的,除了以下事实:自定义代码生成器不能与Web项目位于同一程序集中,并且应从Web项目中引用自定义代码生成器作为程序包引用(项目引用将不起作用) ).

以下是自定义代码生成器对dotnet cli代码生成器可见的要求,

  • 应该在网络项目之外
  • 应该具有Microsoft.VisualStudio.Web.CodeGeneration作为依赖项
  • 自定义代码生成器应打包并添加为将使用代码生成器的Web项目依赖项

dotnet pack -o ../custompackages

(确保将此位置(../custompackages)添加到nuget.config)

注意:我问题中的代码的模型不接受Model参数(-m开关),并且需要controllerName参数,因此,要调用代码生成器,您将必须使用,

dotnet aspnet-codegenerator -p . test --controllerName TestController

OR

dotnet aspnet-codegenerator -p . TestCodeGenerator --controllerName TestController

此处

请参考相关讨论

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

解决方案

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

这篇关于为Dotnet Core编写自定义代码生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆