我正在尝试为dotnet核心编写一个自定义代码生成器,但迄今为止仅凭借其有限的文档获得了小小的成功。为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
这个问题是关于如何写代码生成erator使用aspnet中的现有对象,但我想编写一个在dotnet cli中运行的可执行文件:例如dotnet mygenerator arg0 arg1,我可以使用这个解决方案吗? –
@ H.Herzl该解决方案完全符合你的要求。您可以通过更新TestCodeGeneratorModel(可用)来控制参数,引入您的自定义属性,并使用[Option]属性对其进行装饰。另外,为什么你想引入一个新的cli命令,那么你会错过许多由aspnet-codegenerator提供的功能。 –
我有我的代码生成代码作为块包,我想允许从命令行执行一旦安装,是否有意义? –