如何在项目中使用 T4 自定义脚手架?

发布于 2025-01-08 02:00:59 字数 461 浏览 0 评论 0原文

我正在使用 T4Scaffolding做一些 C# 代码生成。考虑以下项目结构:

项目结构
我在模板项目中创建了一个自定义脚手架。现在我想为 Dummy 项目运行它,但出现此异常:

enter image description这里

我如何在项目中使用自定义脚手架?

问题是:我在 TFS 中有 20 个项目,我想在 1 个中心位置管理 t4 模板的代码。

I'm using T4Scaffolding to do some do some C# code generation. Consider the following project structure:

Project structure
I've created a custrom scaffolder in the Templates project. Now I would like to run it for the Dummy project, but I get this exception:

enter image description here

How can I use Custom Scaffolding over projects?

The thing is: I've got 20 projects in TFS and I would like to manage the code of the t4 templates in 1 central location.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2025-01-15 02:00:59

我认为这只是将 CodeTemplates/Scaffolders/BaseSuperModule 中的文件和目录复制到虚拟项目中,然后调用 Scaffold 命令的问题。

编辑:这很有道理。我正在学习如何将自定义 t4Scaffolding 代码移动到 NuGet 包中。我认为这将是最适合您的路线。一旦我弄清楚,我会立即更新我的答案。

编辑2:我找到了这篇文章,我浏览了它并有一个有效的示例。尝试一下,如果我可以提供进一步的帮助,请告诉我。

http://geekswithblogs.net/michelotti/archive/2011/07/14/leverage-t4scaffolding-for-wcf-web-api.aspx

I think it is just be a matter of copying the files and Directories located in CodeTemplates/Scaffolders/BaseSuperModule into the Dummy Project and then calling the Scaffold command.

Edit: That makes perfect sense. I am in the middle of learning how to move custom t4Scaffolding code into a NuGet package. I think that would be the route that would work best for you. I will update my answer as soon as I figure it out.

Edit 2: I found this article and I went through it and have a working example. Give it a try and let me know if I might be able to help further.

http://geekswithblogs.net/michelotti/archive/2011/07/14/leverage-t4scaffolding-for-wcf-web-api.aspx

つ可否回来 2025-01-15 02:00:59

我认为诀窍是从模板项目中进行脚手架,并将要生成的项目名称传递给例如,

T4Scaffolding.Scaffolder(Description = "Creates a model entity")][CmdletBinding()]
param(
    [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$EntityName,
    [string]$ProjectName,
    [string]$CodeLanguage,
    [string[]]$TemplateFolders,
    [switch]$Force = $false
)

# Find the code project, or abort
$project = Get-Project ( $ProjectName) -ErrorAction SilentlyContinue
if (!$project) { throw "Cannot find code project corresponding to solution '$ProjectName'" }

$entityFile = $EntityName
Add-ProjectItemViaTemplate $entityFile -Template EntityTemplate `
    -Model @{ Namespace = $namespace; EntityName = $EntityName; MappingName = $MappingName } `
    -SuccessMessage "Added Model non-gen output at {0}" `
    -TemplateFolders $TemplateFolders -Project $ProjectName -CodeLanguage $CodeLanguage -Force:$false

您必须将字符串传递给 Add-ProjectItemViaTemplates 中的 -Project 参数,而不是项目对象

如果您已经知道各种脚手架需要应用到的项目,那么您可以在脚手架中为ProjectName设置默认值。

MvcScaffolding 包中有一些这样的示例。

I think the trick is to do the scaffolding from the Templates project and pass in the name of the project you want to generate into e.g.

T4Scaffolding.Scaffolder(Description = "Creates a model entity")][CmdletBinding()]
param(
    [parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)][string]$EntityName,
    [string]$ProjectName,
    [string]$CodeLanguage,
    [string[]]$TemplateFolders,
    [switch]$Force = $false
)

# Find the code project, or abort
$project = Get-Project ( $ProjectName) -ErrorAction SilentlyContinue
if (!$project) { throw "Cannot find code project corresponding to solution '$ProjectName'" }

$entityFile = $EntityName
Add-ProjectItemViaTemplate $entityFile -Template EntityTemplate `
    -Model @{ Namespace = $namespace; EntityName = $EntityName; MappingName = $MappingName } `
    -SuccessMessage "Added Model non-gen output at {0}" `
    -TemplateFolders $TemplateFolders -Project $ProjectName -CodeLanguage $CodeLanguage -Force:$false

You must pass a string to the -Project argument in Add-ProjectItemViaTemplates not a project object

If you already know the projects that the various scaffolders need to apply to, then you can set a default value for ProjectName in the scaffolder.

There's some examples of this in the MvcScaffolding package.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文