在VS扩展中调用抽象和派生的tt模板

问题描述 投票:0回答:1

我正致力于VS的扩展。它应该通过TT模板映射数据库对象。模板可以由扩展用户制作。唯一的事情是,必须继承抽象模板,whitch是扩展的一部分。我在Microsoft文档中找到了一个重新编写,以这种方式调用VS扩展中的转换:Invoking Text Transformation in a VS Extension它适用于虚拟模板,如下所示:

<#@ template language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>

<#
Generate(new List<string> {"AAA","BBB","CCC"},  "666");
#>
<#+ 
public void Generate(List<string> inputList, string tableName)
{
#>
using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;

namespace BBB 

public class <#= tableName #>
{
    <#+
    foreach(var t in inputList)
    {
    #>
        /// <summary>
        /// <#= t #>
        /// </summary>
    <#+
    }
    #>
}
<#+
}
#>

和代码:

Generate("..\\..\\Templates\\TextTemplate.tt");

public void Generate(string filePath)
    {
        IServiceProvider serviceProvider = ServiceProvider;
        ITextTemplating t4 = serviceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;
        T4Callback cb = new T4Callback();
        string result = t4.ProcessTemplate(filePath, File.ReadAllText(filePath), cb);
        string resultFileName = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)) + "_gen" + cb.fileExtension;
        File.WriteAllText(resultFileName, result, cb.outputEncoding);
    }


public class T4Callback : ITextTemplatingCallback
{
    public List<string> errorMessages = new List<string>();
    public string fileExtension = ".cs";
    public Encoding outputEncoding = Encoding.UTF8;

    public void ErrorCallback(bool warning, string message, int line, int column)
    { errorMessages.Add(message); }

    public void SetFileExtension(string extension)
    { fileExtension = extension; }

    public void SetOutputEncoding(Encoding encoding, bool fromOutputDirective)
    { outputEncoding = encoding; }
}

但它不适用于抽象模板。如果我有抽象模板:

<#@ template language="C#" #>

<#@ import namespace="System.Collections.Generic" #>
<#@ import namespace="System.IO" #>

<#@ parameter type="System.String" name="FakeIn" #> 

<#   
this.PushIndent("  ");  

//Generate method
Generate(InputData); 

//Save method
SaveOutput(OutputFileName);

this.PopIndent();  
#>  

<#+ 
#region Properties
public string OutputFileName { get; set; }
public InPutClass InputData { get; set; }
#endregion

#region Override method
protected virtual void Generate(InPutClass input) { }
#endregion

#region Non-Override method
protected void SaveOutput(string outputFileName) 
{
    string outputFilePath = Path.Combine(outputFileName);
    File.WriteAllText(outputFilePath, this.GenerationEnvironment.ToString()); 
    this.GenerationEnvironment.Remove(0, this.GenerationEnvironment.Length);
}
#endregion

#region Data classes
public class InPutClass
{
    public List<string> InputList { get; set; }
    public string InputTableName { get; set; }
}
#endregion
#>

并派生出一个这样的:

<#@ template language="C#" inherits="AbstractTemplate" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>

<#
InputData = new InPutClass{ InputList = new List<string>{"AAA","BBB","CCC"}, InputTableName = "abc" };
OutputFileName = "..\\..\\Templates\\file2.cs";
base.TransformText();  
#>

<#+ 
protected override void Generate(InPutClass input)
{
#>
using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.Serialization;
using ABEL.CORE.TypeComponents;
using ABEL.DAL.Attributes;
using Protos.Data;

namespace BBB 

public class <#= input.InputTableName #>
{
    <#+
    foreach(var t in input.InputList)
    {
    #>
        /// <summary>
        /// <#= t #>
        /// </summary>
    <#+
    }
    #>
}
<#+
}
#>

如果我调用方法生成结果是:

ErrorGeneratingOutput

  • 编译转换:找不到类型或命名空间名称'AbstractTemplate'(您是否缺少using指令或程序集引用?)
  • 编译转换:找不到类型或命名空间名称'InPutClass'(您是否缺少using指令或程序集引用?)
  • 编译转换:'GeneratedTextTransformation.TransformText()':找不到合适的方法来覆盖
  • 编译转换:'GeneratedTextTransformation.Generate(InPutClass)':找不到合适的方法来覆盖
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:当前上下文中不存在名称“InputData”
  • 编译转换:找不到类型或命名空间名称'InPutClass'(您是否缺少using指令或程序集引用?)
  • 编译转换:当前上下文中不存在名称“OutputFileName”
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'GenerationEnvironment'的定义,并且没有扩展方法'GenerationEnvironment'可以找到接受类型'GeneratedTextTransformation'的第一个参数(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'GenerationEnvironment'的定义,并且没有扩展方法'GenerationEnvironment'可以找到接受类型'GeneratedTextTransformation'的第一个参数(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)
  • 编译转换:'GeneratedTextTransformation'不包含'Write'的定义,也没有扩展方法'Write'接受类型为'GeneratedTextTransformation'的第一个参数'(你是否缺少using指令或汇编引用?)

所以对我而言,模板对你自己一无所知。因为当我将它们作为解决方案的一部分并且只调用经典模板模板时.TransformText();一切都好。

我也尝试了PreprocessingTemplate的一些组合,如下所示:

   GenerateAbstract("\\..\\..\\Templates2\\DerivedTemplate2.tt");
   public void GenerateAbstract(string filePath)
    {
        IServiceProvider serviceProvider = ServiceProvider;
        ITextTemplating t4 = serviceProvider.GetService(typeof(STextTemplating)) as ITextTemplating;

        T4Callback cb = new T4Callback();
        string[] reference;
        string abstractTemplatePath = "..\\..\\Templates2\\AbstractTemplate.tt";

        string abstractTemplatePreprocessing = t4.PreprocessTemplate(abstractTemplatePath, File.ReadAllText(abstractTemplatePath), cb, "AbstractTemplate", "AbstractTemplating.Templates2", out reference);
        string derived2TemplatePreprocessing = t4.PreprocessTemplate(filePath, File.ReadAllText(filePath), cb, "DerivedTemplate2", "AbstractTemplating.Templates2", out reference);

        File.WriteAllText(Path.Combine("..\\..\\Templates2", "AbstractTemplate.cs"), abstractTemplatePreprocessing, cb.outputEncoding);
        File.WriteAllText(Path.Combine("..\\..\\Templates2", "DerivedTemplate2.cs"), derived2TemplatePreprocessing, cb.outputEncoding);

        string result = t4.ProcessTemplate("..\\..\\Templates2\\AbstractTemplate.cs", derived2TemplatePreprocessing, cb);

        string resultFileName = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath)) + "_gen" + cb.fileExtension;
        // Write the processed output to file:
        File.WriteAllText(resultFileName, result, cb.outputEncoding);
        // Append any error messages:
        if (cb.errorMessages.Count > 0)
        {
            File.AppendAllLines(resultFileName, cb.errorMessages);
        }
    }

但在这种情况下,DerivedTemplate2:AbstractTemplate的结果是C#代码,类似于模板是解决方案的一部分并且构建了解决方案。

所以最后我的问题是可以用这种方式调用抽象和派生模板吗?如果是的话,你能举个例子来说明如何实现这个目标。谢谢

c# t4 visual-studio-extensions
1个回答
0
投票

这只是一个意见,但在您描述的主题上:

1)不要隐藏用户的模板(将其与生成的代码一起放到解决方案中) - VS MVC-Entity-Extension执行它的方法 - 相反 - 但MS在这种情况下是错误的(并且MS中代码生成的低流行度)世界是这方面的证据)。如果必须生成100个文件,还要生成100个模板。但是更好的一个模板在一个文件中生成100个类。

2)不要混合代码生成和实现继承 - 它们彼此是正交的。首先是以表格形式向用户发布“一切”可以改变“一切”,其次是隐藏细节(​​为什么你应该隐藏细节,如果你想提供用户'改变一切'的功能)?

© www.soinside.com 2019 - 2024. All rights reserved.