DSL,在VS 2012中创建自定义工具代码生成器 [英] DSL, create a Custom Tool Code Generator in VS 2012

查看:57
本文介绍了DSL,在VS 2012中创建自定义工具代码生成器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好, 

Hi everybody, 

我正在尝试在VS2012中创建自定义工具。

I'm trying to create a Custom Tool in VS2012.

问题是:"找不到自定义工具ItmFileToolGenerator"在这个系统上。我将描述我的所作所为:

The problem is: "Cannot find custom tool ItmFileToolGenerator" on this system. I'm going to describe what I did:

我创建了一个DSL。在我的调试实例中,我有一个.tt文件(ModelReport.tt),

I create a DSL. In my debugging instance I have a .tt file (ModelReport.tt) that

生成一个cs文件。

这是包含的顶部tt文件的一部分:

This is the top of the include section of the tt file:

<#@ template inherits="Microsoft.VisualStudio.TextTemplating.VSHost.ModelingTextTransformation" #>
<#@ output extension=".cs" #>
<#@ template language="CS" debug="True"#>
<#@ MyDSLModel processor="MyDSLModelDirectiveProcessor" requires="fileName='Sample.itm'" #>

我按照可视化和建模SDK实验室 - (对于此步骤,第4/6部分)来自VS2010。

I followed the Visualization and Modeling SDK Lab - (for this step, part 4/6) which is for VS2010.

我要写下我做的步骤。也许(可能)我错过了一些东西。

I'm going to write the steps I did. Maybe(Probably) I missed something.

1-我在 DslPackage 中创建 CustomCode 目录和 CodeGeneration 子目录。在这个子目录中,我添加了我的tt文件

1- I create in DslPackage a CustomCode Directory and a CodeGeneration sub-directory. In this sub-dir I added my tt file

  &NBSP;来自调试实例的(ModelReport.tt)。我重命名了文件  ModelReport.tt - > Itm.t4(Itm是新的文件扩展名)

    (ModelReport.tt) from the debugging instance. I renamed the file  ModelReport.tt -> Itm.t4 ( Itm is the new file extension)

2- Itm.t4 属性:BuildAction =嵌入式资源,CustomTool =空白

2- Itm.t4 properties: BuildAction = Embedded Resource, CustomTool = blank

3-我在 CodeGeneration
目录中创建了 CodeGenerationResource.resx 文件。

3- I created a CodeGenerationResource.resx file in the CodeGeneration directory.

4-将Itm.t4拖到资源文件中。

4- Drag Itm.t4 into the resources file.

5-在 CodeGeneration 目录中添加 CodeGeneration.cs 文件,该文件如下:

5- Add a CodeGeneration.cs file in CodeGeneration directory which is the following:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TextTemplating.VSHost;
using System.IO;
using System.Runtime.InteropServices;


namespace MyDSLModel.CustomCode.CodeGeneration
{   
    [ComVisible(true)]
    [System.Runtime.InteropServices.Guid("EDC7CA77-1FD5-4306-BE22-D03A661F1E8F")]
    public class CodeGenerator: TemplatedCodeGenerator
    {
        public override string GetDefaultExtension()
        {
            return "itm.cs";
        }
        protected override byte[] GenerateCode(string inputFileName, string inputFileContent)
        {   
            // Replace the supplied file contents with the template we want to run
            inputFileContent = ASCIIEncoding.UTF8.GetString(CodeGenerationResource.Itm); 

            // Substitute the name of the current model file into the template
            FileInfo fi= new FileInfo(inputFileName);

            inputFileContent = inputFileContent.Replace("Sample.itm", fi.Name);
            inputFileContent = inputFileContent.Replace("namespace Debugging", "namespace " + FileNamespace);

            // Now just delegate the rest of the work to the base class
            byte[] data;
            data = base.GenerateCode(inputFileName, inputFileContent);
            byte[] ascii = new byte[data.Length - 3];
            Array.Copy(data, 3, ascii, 0, data.Length - 3);
            return ascii; 
        }
    }
}

**第一期 - 在我的ModelRepot.tt文件的include部分(现在Itm.t4文件)我有处理器:

**First issue - In the include section of my ModelRepot.tt file (now Itm.t4 file) I have the processor:

MyDSLModel processor="MyDSLModelDirectiveProcessor"

无法识别此处理器(当然我们在主实例中)。我添加了

this processor is not recognized (of course we are in the main instance) . I've added instead

Dsl processor="DslDirectiveProcessor"




我犯了错误吗?

最后我创建了CodeGenerator.pkgdef文件并设置了以下属性:

At the end I created the CodeGenerator.pkgdef file and I did set the following properties:

构建操作=内容,

复制到输出目录=复制如果更新,

Copy to Output directory = Copy if newer,

包含在VSIX中= true

Include in VSIX = true

这是CodeGenerator.pkgdef文件

This is the CodeGenerator.pkgdef file

Windows Registry Editor Version 5.00 ; TODO: Replace with your GUID [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\11.0_Config\CLSID\{EDC7CA77-1FD5-4306-BE22-D03A661F1E8F}] "InprocServer32"="C:\\Windows\\System32\\mscoree.dll" "ThreadingModel"="Both" ; TODO: Replace with your class "Class"="MyDSLModel.CodeGenerator" ; TODO: Replace with your assembly "Assembly"="MyDSLModel.DslPackage, Version=1.0.0.0, Culture=neutral" ; TODO: Replace with your dll file "CodeBase"="file:///C:\\work\\MyDSLModel\\DslPackage\\bin\\Debug\\MyDSLModel.DslPackage.dll" ; TODO: Replace with your tool name [HKEY_LOCAL_MACHINE\Software\Microsoft\VisualStudio\11.0_Config\Generators\{FAE04EC1-301F-11D3-BF4B-

00C04F79EFBC} \\ \\ immFileToolGenerator]
@ ="从itm文件生成c#文件的自定义工具"
" CLSID" =" {EDC7CA77-1FD5-4306-BE22-D03A661F1E8F}"
" GeneratesDesignTimeSource" = dword:00000001
; TODO:替换为您的描述
@ =" ItmFileToolGenerator"

00C04F79EFBC}\ItmFileToolGenerator] @="Custom tool that geneates c# files from itm files" "CLSID"="{EDC7CA77-1FD5-4306-BE22-D03A661F1E8F}" "GeneratesDesignTimeSource"=dword:00000001 ; TODO: Replace with your description @="ItmFileToolGenerator"

任何想法?

谢谢提前。

Nico

推荐答案

Hello Nico。

Hello Nico.

在VS 2012中,您不再需要再添加CodeGenerator.pkgdef文件。

In VS 2012, you don't need any longer to add a CodeGenerator.pkgdef file.

您需要做的就是在Code生成器类上添加一个属性(即继承自TemplatedCodeGenerator的类)

All you need to do is add an attribute on your Code generator class (that is the class inheriting from TemplatedCodeGenerator)

[ ProvideCodeGenerator typeof运算 CodeGenerator ),
" ItfFileToolCodeGenerator"
"基于.itm文件生成C#文件的自定义工具"
true ,RegisterCodeBase = false )]

[ProvideCodeGenerator(typeof(CodeGenerator), "ItfFileToolCodeGenerator", "Custom tool that generates C# files based on .itm files", true, RegisterCodeBase=false)]

.pkgdef将自动生成(您仍需要GUID)

The .pkgdef will be generated automatically (you'll still need the GUID)

问候

Regards


这篇关于DSL,在VS 2012中创建自定义工具代码生成器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆