T4模板无法与建设改造 [英] T4 template will not transform with build

查看:284
本文介绍了T4模板无法与建设改造的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows 7
使用VisualStudio的旗舰2012(更新一)我可以让我的T4模板来生成一个文件:
右键单击[TT文件] |运行自定义工具的。这一伟大工程! (耶对我来说)。如果我运行菜单命令
它还适用构建|变换所有T4模板的。但我想自动化!与每一个构建产生
我遇到一个问题,当我试图让生成的代码文件 - 这是我的目标结果
我看了看这个:
T4变革和建立在Visual Studio

I'm using VisualStudio Ultimate 2012 (Update 1) on Windows 7. I can get my T4 template to generate a file by: right click [tt file] | run Custom tool. That works great! (yay for me). It also works if I run the menu command: Build | Transform All T4 Templates. But I want automation! I run into a problem when I try to get the generated code file to be produced with every build -- which is my goal.
I looked at this: T4 transformation and build order in Visual Studio

这是不是我想要的。它讨论使用预生成生成事件。我想这是构建的一部分

this isn't what I want. It discusses using a pre-build build event. I wanted it to be part of the build.

和这样的:
是否有办法让Visual Studio中运行在成功转换所有模板建?

这是不是我想要的不是。它介绍了使用生成后事件

this isn't what I wanted either. It discusses using a post-build event.

后来我发现这一点:
了解T4:MSBuild的集成

从的奥列格Sych发表的博客

完美!这就是我想要的。虽然他的博客上讨论了VS2010,我把它采用VS2012适用。

Perfect! This is what I want. Although his blog discusses VS2010, I have adopted it to VS2012 where applicable.

我通过每一个他的脚步实现该走了。
我安装的Visual Studio 2012 SDK &放; Visual Studio中的可视化和建模SDK(2012)

I walked through each of his steps for implementing this. I installed Visual Studio SDK 2012 & Visual Studio Visualization and Modeling SDK (2012)

我开始使用的有形T4插上,但想有一个与这样一个问题,我用Oleg的T4工具箱Beta版。可悲的是,无论是与我似乎得到相同的结果:以构建一个错误

I started with using the Tangible T4 plug in, but thinking there was a problem with that, I used Oleg's T4 Toolbox Beta. Sadly, with either I seemed to get the same results: an error with a build.

我跟着他在他的现场指导和重读的所有步骤和部件。我一直在这里为研究天,现在我的第一篇文章。我卡住了。感谢您看...

I followed his instruction on his site and reread all the steps and parts. I have been researching for days, and now my first post here. I am stuck. Thanks for looking...

要回顾一下:
我得到一个错误,当我建立在Visual Studio 2012或当我运行命令行的MSBuild (但不再当我的运行自定义工具的或使用手册的变换所有T4模板的 - 这两方面的工作罚款)。

To recap: I get an error when I build in visual studio 2012 or when I run the msbuild from the command-line (but again not when I run Custom Tool or use the manual Transform All T4 Templates - Both of those work fine).

下面是我的t4template名为 s_code.tt

Here is my t4template called s_code.tt:

<#@ template  debug="true" hostSpecific="true" language="C#" #>
<#@ output extension=".js" #>
<#@ assembly name="System.Core" #>
<#@ assembly name="System.Xml" #>
<#@ assembly name="EnvDTE" #>
<#@ assembly name="EnvDTE80" #>
<#@ assembly name="Microsoft.VisualStudio.Shell.Interop.8.0" #>
<#@ import namespace="System.Diagnostics" #>
<#@ import namespace="System" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.IO" #>
<#@ import namespace="EnvDTE" #>
<#@ import namespace="EnvDTE80" #>
<#@ import namespace="Microsoft.VisualStudio.TextTemplating" #>
<#@ import namespace="Microsoft.VisualStudio.Shell.Interop" #>


<#
    IServiceProvider serviceProvider = (IServiceProvider)Host;
    EnvDTE.DTE dte = (EnvDTE.DTE)serviceProvider.GetService(typeof(EnvDTE.DTE));
    var configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name;
    string filename = this.Host.ResolvePath("s_code_source.txt");
    string[] lines = File.ReadAllLines(filename);
    bool isAccountFound = false;
    int linecount = lines.Length;
    int currentline = 0;
    for (int i=0;i<linecount;i++)
    {
        if (Contains_S_Account(lines[i]))
        {
            if (configName.ToUpper() == "DEBUG")
            {
                    WriteLine("var s_account = \"macudev2\"");
            }
            else if (configName.ToUpper() == "RELEASE")
             {
                   WriteLine("var s_account = \"macudev\"");
             }

            currentline = i;
            isAccountFound = true;
        }
        else
        {
            WriteLine(lines[i]);
        }
    }
    for (int i=currentline;i<linecount;i++)
    {
        WriteLine(lines[i]);
    }


#>

<#+
private bool Contains_S_Account(string line)
{
    if (line.ToLower().Contains("var s_account"))
    {
        return true;
    }
    else
    {
        return false;
    }
}
#>

下面是我的 .csproj的文件:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
    <ProductVersion>
    </ProductVersion>
    <SchemaVersion>2.0</SchemaVersion>
    <ProjectGuid>{0A44E136-F4A4-4B31-95DD-2C8A79FDFAF4}</ProjectGuid>
    <ProjectTypeGuids>{349c5851-65df-11da-9384-00065b846f21};{fae04ec0-301f-11d3-bf4b-00c04f79efbc}</ProjectTypeGuids>
    <OutputType>Library</OutputType>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <RootNamespace>Macu.Content</RootNamespace>
    <AssemblyName>Macu.Content</AssemblyName>
    <TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
    <UseIISExpress>true</UseIISExpress>
    <IISExpressSSLPort />
    <IISExpressAnonymousAuthentication />
    <IISExpressWindowsAuthentication />
    <IISExpressUseClassicPipelineMode />
    <TargetFrameworkProfile />
    <TransformOnBuild>True</TransformOnBuild>
    <IncludeFolders>$(MSBuildProjectDirectory)\Include</IncludeFolders>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
    <DebugSymbols>true</DebugSymbols>
    <DebugType>full</DebugType>
    <Optimize>false</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>DEBUG;TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
    <DebugType>pdbonly</DebugType>
    <Optimize>true</Optimize>
    <OutputPath>bin\</OutputPath>
    <DefineConstants>TRACE</DefineConstants>
    <ErrorReport>prompt</ErrorReport>
    <WarningLevel>4</WarningLevel>
  </PropertyGroup>
  <ItemGroup>
    <Reference Include="EnvDTE, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="envdte80, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="Microsoft.CSharp" />
    <Reference Include="Microsoft.VisualStudio.Shell.Interop.10.0, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
      <EmbedInteropTypes>True</EmbedInteropTypes>
    </Reference>
    <Reference Include="Microsoft.VisualStudio.TextTemplating.11.0, Version=11.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
    <Reference Include="System" />
    <Reference Include="System.Xml" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="shared\script\s_code.js">
      <AutoGen>True</AutoGen>
      <DesignTime>True</DesignTime>
      <DependentUpon>s_code.tt</DependentUpon>
    </Content>
    <Content Include="shared\script\s_code_source.txt" />
    <Content Include="Web.config" />
  </ItemGroup>
  <ItemGroup>
    <Compile Include="Properties\AssemblyInfo.cs" />
  </ItemGroup>
  <ItemGroup>
    <Content Include="shared\script\s_code.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>s_code.js</LastGenOutput>
      <Parameters>
        &lt;%3fxml version="1.0" encoding="utf-16"%3f&gt;
        &lt;ArrayOfParameterStorage xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" /&gt;
      </Parameters>
    </Content>
    <None Include="Properties\PublishProfiles\Local.pubxml" />
    <None Include="Web.Debug.config">
      <DependentUpon>Web.config</DependentUpon>
    </None>
    <None Include="Web.Release.config">
      <DependentUpon>Web.config</DependentUpon>
    </None>
  </ItemGroup>
  <ItemGroup>
    <T4ReferencePath Include="$(VsInstallDir)PublicAssemblies\" />
  </ItemGroup>
  <ItemGroup>
    <Folder Include="shared\images\" />
  </ItemGroup>
  <ItemGroup />
  <ItemGroup>
    <Service Include="{508349B6-6B84-4DF5-91F0-309BEEBAD82D}" />
  </ItemGroup>
  <PropertyGroup>
    <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
    <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
  </PropertyGroup>
  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v11.0\TextTemplating\Microsoft.TextTemplating.targets" />
  <Import Project="$(VSToolsPath)\WebApplications\Microsoft.WebApplication.targets" Condition="'$(VSToolsPath)' != ''" />
  <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\WebApplications\Microsoft.WebApplication.targets" Condition="false" />
  <ProjectExtensions>
    <VisualStudio>
      <FlavorProperties GUID="{349c5851-65df-11da-9384-00065b846f21}">
        <WebProjectProperties>
          <UseIIS>True</UseIIS>
          <AutoAssignPort>True</AutoAssignPort>
          <DevelopmentServerPort>30698</DevelopmentServerPort>
          <DevelopmentServerVPath>/</DevelopmentServerVPath>
          <IISUrl>http://localhost:50012/</IISUrl>
          <NTLMAuthentication>False</NTLMAuthentication>
          <UseCustomServer>False</UseCustomServer>
          <CustomServerUrl>
          </CustomServerUrl>
          <SaveServerSettingsInUserFile>False</SaveServerSettingsInUserFile>
        </WebProjectProperties>
      </FlavorProperties>
    </VisualStudio>
  </ProjectExtensions>
  <PropertyGroup>
    <PreBuildEvent>
    </PreBuildEvent>
  </PropertyGroup>
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
       Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

下面是一台运行后,我看到的错误的MSBuild 从命令行:

Here is the error I see after running MSBuild from command line:

C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Conten
t>msbuild macu.content.csproj /t:TransformAll
Microsoft (R) Build Engine version 4.0.30319.17929
[Microsoft .NET Framework, version 4.0.30319.17929]
Copyright (C) Microsoft Corporation. All rights reserved.

Build started 1/17/2013 2:16:59 PM.
Project "C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\M
acu.Content\macu.content.csproj" on node 1 (TransformAll target(s)).
ExecuteTransformations:
  Performing incremental T4 transformation
  Calculating whether transformed output is out of date...
  Transforming template shared\script\s_code.tt...
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Micr
osoft.TextTemplating.targets(396,5): warning : Compiling transformation: The va
riable 'isAccountFound' is assigned but its value is never used. Line=24, Colum
n=7 [C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.
Content\macu.content.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Micr
osoft.TextTemplating.targets(396,5): error : Running transformation: System.Nul
lReferenceException: Object reference not set to an instance of an object.\r [C
:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Conten
t\macu.content.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Micr
osoft.TextTemplating.targets(396,5): error :    at Microsoft.VisualStudio.TextT
emplating7D294BC599798219F70D124BB1976BCDFB50B07280E2004F9365EC71A617D68D059E43
6CBD1AD344727611A619EE41F939B60372B3E16565CA2D4E4B40FBC5C7.GeneratedTextTransfo
rmation.TransformText() in c:\Users\[myUserName]\Documents\Visual Studio 2012\Proje
cts\Macu.Content\Macu.Content\shared\script\s_code.tt:line 21. Line=21, Column=
0 [C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Co
ntent\macu.content.csproj]
Done Building Project "C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\
Macu.Content\Macu.Content\macu.content.csproj" (TransformAll target(s)) -- FAIL
ED.


Build FAILED.

"C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Cont
ent\macu.content.csproj" (TransformAll target) (1) ->
(ExecuteTransformations target) ->
  C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Mi
crosoft.TextTemplating.targets(396,5): warning : Compiling transformation: The
variable 'isAccountFound' is assigned but its value is never used. Line=24, Col
umn=7 [C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Mac
u.Content\macu.content.csproj]


"C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Cont
ent\macu.content.csproj" (TransformAll target) (1) ->
(ExecuteTransformations target) ->
  C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Mi
crosoft.TextTemplating.targets(396,5): error : Running transformation: System.N
ullReferenceException: Object reference not set to an instance of an object.\r
[C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Cont
ent\macu.content.csproj]
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\TextTemplating\Micr
osoft.TextTemplating.targets(396,5): error :    at Microsoft.VisualStudio.TextT
emplating7D294BC599798219F70D124BB1976BCDFB50B07280E2004F9365EC71A617D68D059E43
6CBD1AD344727611A619EE41F939B60372B3E16565CA2D4E4B40FBC5C7.GeneratedTextTransfo
rmation.TransformText() in c:\Users\[myUserName]\Documents\Visual Studio 2012\Proje
cts\Macu.Content\Macu.Content\shared\script\s_code.tt:line 21. Line=21, Column=
0 [C:\Users\[myUserName]\Documents\Visual Studio 2012\Projects\Macu.Content\Macu.Co
ntent\macu.content.csproj]

    1 Warning(s)
    1 Error(s)

Time Elapsed 00:00:01.04

你能看见什么,我做的这是造成错误,并且你知道如何解决它,所以我可以使用的MSBuild并没有碰到这个问题?

Can you see anything that I am doing which is causing the error, and do you know how to fix it so I can use msbuild and not run into this error?

推荐答案

我怕你不能成功运行在构建时模板使使用主机变量来获得在像DTE IDE服务,为的MSBuild T4主机被设计为在命令行工作既以及建立在IDE中,因此,不公开的全球服务提供商或DTE到模板。同样是从texttransform.exe,简单的命令行的主机如此。

I'm afraid that you can't successfully run templates at build time that make use of the Host variable to get at IDE services like the DTE, as the msbuild T4 host is designed to work both from the command line as well as builds in the IDE, and as such, does not expose the global service provider or DTE to templates. The same is true from texttransform.exe, the simple command-line host.

这篇关于T4模板无法与建设改造的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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