【问题标题】:Writing custom Code generator for Dotnet Core为 Dotnet Core 编写自定义代码生成器
【发布时间】:2017-05-12 04:49:15
【问题描述】:

我正在尝试为 dotnet 核心编写一个自定义代码生成器,但到目前为止,由于文档有限,因此收效甚微。

浏览了一下 CodeGeneration 源代码,了解了如何从命令行触发生成器以及它在内部是如何工作的。

由于 dotnet core 中可用的生成器无法满足我的需求,我尝试编写自己的 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

【问题讨论】:

    标签: c# asp.net-core code-generation dotnet-cli


    【解决方案1】:

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

    几乎一切都是正确的,除了自定义代码生成器不能与 Web 项目驻留在同一个程序集中,并且自定义代码生成器应该作为包引用从 Web 项目中引用(项目引用不起作用)。

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

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

    dotnet 包 -o ../custompackages

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

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

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

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

    参考相关讨论here

    【讨论】:

    • 这个问题是关于如何使用 aspnet 中的现有对象编写代码生成器,但我想编写一个在 dotnet cli 中运行的可执行文件:例如dotnet mygenerator arg0 arg1,我可以使用这个解决方案吗?
    • @H.Herzl 这个解决方案完全符合您的要求。您可以通过更新 TestCodeGeneratorModel(有问题)、引入您的自定义属性并使用 [Option] 属性装饰它们来控制参数。另外,你为什么要引入一个新的 cli 命令,你会错过许多 aspnet-codegenerator 提供的功能。
    • 我有我的代码生成代码作为 nugget 包,我想在安装后允许从命令行执行,有意义吗?
    • 但是我的代码生成不仅仅针对aspnet核心,所以我想如果我限制我的aspnet核心代码生成会出错,所以我的问题是如何添加命令行界面我现有的 nuget 包:codeproject.com/Articles/1160615/…
    猜你喜欢
    • 2021-01-21
    • 2019-03-16
    • 2010-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 1970-01-01
    相关资源
    最近更新 更多