2(3) 关于 T4 模板、获取正在处理的主文件的名称以及能够结束处理的问题吗?

发布于 2024-10-01 05:18:33 字数 833 浏览 5 评论 0原文

我正在尝试为一个项目创建自己的 DSL,并且我想创建一个包含该主引擎的 T4 文件,然后创建包含该引擎并调用它的小 T4 文件。

我正在考虑对将调用引擎的 T4 文件尝试这种格式:

<#@ include file="EngineLib\EngineLib.tt" #>
<# RunEngine(); #>
dsl goes here

我有以下三个问题:

1. 我可以获得主脚本的名称吗?

基本上,在 EngineLib.tt 中,我可以检索包含它的脚本的完整路径和文件名(在本例中是上面显示的脚本)吗?或者,如果没有,它的内容是什么?

2. 我可以结束脚本处理吗?

例如,在 RunEngine() 方法内,如果第一个问题产生答案,我将通读原始脚本,删除包含 T4 指令的第一行,然后根据遵守我的 DSL 规则。

然后,当 RunEngine 从该 DSL 生成代码结果时,我将终止 T4 代码的处理,以便 DSL 本身不会作为生成文件的一部分输出。

这有道理吗?

假设 RunEngine 只是输出一个类,如果我无法终止处理,生成的文件将如下所示:

public class SomeClass
{
}
dsl goes here

显然,我不希望 DSL 代码作为我生成的输出的一部分。

3.我是不是找错了树?

我知道我不能在 DSL 中包含类似 T4 的代码,因为这最终会被编译成作为 T4 处理的一部分运行的代码,但在这种情况下,这不会成为问题。

还有什么我没有想到的可能会出错的地方吗?

I am trying to create my own DSL for a project, and I'd like to create one T4 file that contains the main engine for this, and then to create small T4 files that includes this engine and invokes it.

I was thinking of attempting this format for the T4 files that will invoke the engine:

<#@ include file="EngineLib\EngineLib.tt" #>
<# RunEngine(); #>
dsl goes here

The three questions I have are as follows:

1. Can I get the name of the primary script?

Basically, inside EngineLib.tt, can I retrieve the full path to and filename of the script that included it, in this case the script being shown above? Or, if not, the contents of it?

2. Can I end script processing?

For instance, inside the RunEngine() method, if the first question produces an answer, I would read through the original script, remove the first lines containing T4 directives, and then process the rest of the file according to my DSL rules.

Then, when RunEngine has produced the code result from that DSL, I would terminate processing of T4 code, so that the DSL itself isn't outputted as part of the generated file.

Did that make sense?

Assume RunEngine simply outputs a class, if I can't terminate processing, here's what the generated file would look like:

public class SomeClass
{
}
dsl goes here

Obviously I don't want the DSL code as part of my generated output.

3. Am I barking up the wrong tree?

I know that I can't include T4-like code in the DSL, since that would end up being compiled into code that would run as part of the T4 processing, but in this case it won't be a problem.

Anything else I haven't thought of that can go wrong?

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

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

发布评论

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

评论(1

甜点 2024-10-08 05:18:34
  1. 要获取脚本的名称,请使用Host.TemplateFile。要使主机可用,请将 hostspecic 属性添加到标头:

    <#@ template language="C#v3.5" hostspecific="true" debug="true" #>
    
  2. 要停止处理脚本,可以使用 return 语句。显然,无法在另一个函数中执行此操作,您必须在 <# #> 括号内执行此操作。

    <# return GenerationEnvironment.ToString(); #>
    
  3. 您尝试实现的目标通常是使用两个单独的文件来完成:T4 脚本和它用作源的数据文件。要使脚本在数据文件更改时更新生成的文件,以及从一个 T4 脚本生成多个文件,您可以使用 T4Toolbox。

  1. To get name of the script, use Host.TemplateFile. To make the host available, add hostspecific property to the header:

    <#@ template language="C#v3.5" hostspecific="true" debug="true" #>
    
  2. To stop processing the script, you can use return statement. Obviously, there is no way to do this inside another function, you must do it inside <# #> brackets.

    <# return GenerationEnvironment.ToString(); #>
    
  3. What you try to achieve is usually done using two separate files: T4 script and data file it uses as source. To make the script update generated file when data file is changed, and to generate multiple files from one T4 script, you can use T4Toolbox.

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