Search code examples
visual-studiomsbuildcode-generationt4csproj

I want to setting T4 Template with wildcard in csproj


What I do

I have multiple tt files in my project file. Therefore, many entries are created for each file as follows.

<ItemGroup>
  <Compile Update="Sample.cs">
    <DesignTime>True</DesignTime>
    <AutoGen>True</AutoGen>
    <DependentUpon>Sample.tt</DependentUpon>
  </Compile>
</ItemGroup>

<ItemGroup>
  <None Update="Sample.tt">
    <Generator>TextTemplatingFileGenerator</Generator>
    <LastGenOutput>Sample.cs</LastGenOutput>
  </None>
</ItemGroup>

I want to prevent making this entry for every tt file.

What I have tried

The following statements were made using wildcards.

<ItemGroup>
  <Compile Update="**/*.tt.cs">
    <DesignTime>True</DesignTime>
    <AutoGen>True</AutoGen>
    <DependentUpon>%(Filename)</DependentUpon>
  </Compile>
  <None Update="**/*.tt">
    <Generator>TextTemplatingFileGenerator</Generator>
    <LastGenOutput>%(Filename).tt.cs</LastGenOutput>
  </None>
</ItemGroup>

The following settings are used in the tt file.

<#@ output extension="tt.cs" #>

Problem

T4 works, but when I save the tt file, the following entry is added to csproj.

<ItemGroup>
  <Compile Update="Sample.tt.cs">
    <DesignTime>True</DesignTime>
  </Compile>
</ItemGroup>

Question

How can I prevent entries from being added?


Solution

  • We can see the description of Update from this page

    Enables you to modify metadata of an item; typically used to override the default metadata of specific items after a group of items is intially specified (such as with a wildcard).

    Update overrides Compile when save the tt file, so this code will be added to scproj. This doesn't seem to prevent.

    <ItemGroup>
      <Compile Update="Sample.tt.cs">
        <DesignTime>True</DesignTime>
      </Compile>
    </ItemGroup>