bennor / AutoT4

A zero-configuration extension for Visual Studio 2012+ which automatically runs your T4 templates at build time.
MIT License
11 stars 14 forks source link

Support for .tt files in new .csproj projects #19

Closed drewnoakes closed 7 years ago

drewnoakes commented 7 years ago

As described here on Stack Overflow:

I migrated a project.json/.xproj project to the newer CS2017 .csproj format.

The project contains a T4 (.tt) template file.

It doesn't regenerate its output on save or build. The output .cs file isn't nested below the .tt file either.

Is there something I have to do to get this working?

I installed AutoT4 1.2.1 and restarted VS, but it doesn't seem to update the output .cs file. Does this extension work with the newer .csproj project format?

drewnoakes commented 7 years ago

The readme suggests there should be a "Run on build" option for the .tt file. This is what I see:

image

bennor commented 7 years ago

I did test the extension with a new .NET Framework 4.6.2 project in VS 2017 and it all seemed okay, but it seems as though it'll have to work differently for the new project type. (I actually do very little .NET dev anymore so I haven't looked into the new format -- I'm assuming it's just for .NET Core?)

I just created a .NET Core project and nothing shows up. I'm assuming this is because none of the old VS interfaces are used for .NET Core. I'll have a look into it over the next few days and hopefully have something working by next week (provided it's possible). I did have trouble adding a text template to my project, as it didn't come up in any of the listed templates. I actually had to search for "template" to get it to come up.

drewnoakes commented 7 years ago

Thanks for the rapid response.

The new project format was born with .NET Core in mind, but it allows you to target multiple frameworks from a single project, which is really nice. You don't have to target .NET Core at all, actually.

Beyond that I know very little about how it differs within the IDE from an extension author's perspective, sorry.

bennor commented 7 years ago

Seems to be a massively different format. I'm assuming that all the same hooks will be available, just on different objects.

In my testing I was seeing it auto-generate on build (perhaps you have "Disabled" set globally in the options and that's why you don't -- I confirmed that this setting is still influencing it), but I didn't see the template specific options show up in the properties (as you've pointed out in your screenshot above).

drewnoakes commented 7 years ago

Interesting. I hadn't touched any settings:

image

I also don't have the option of creating a new T4 file from Add new item..., even searching for 'template' as you mentioned. I ran the VS2017 installer again to verify I had the T4 stuff installed, and it's there.

My .csproj file ```xml A fast, lightweight, cross-platform serialisation tool with support for safe contract evolution. Copyright Drew Noakes 2015-2017 Dasher 0.9.0 Drew Noakes netstandard1.3;net45 $(DefineConstants);UNSAFE true portable true Dasher Dasher Serialisation;Serialization;MsgPack;Communication;Formatting;DTO https://raw.githubusercontent.com/drewnoakes/dasher/master/Resources/logo-128x128.png https://github.com/drewnoakes/dasher https://www.apache.org/licenses/LICENSE-2.0.html git https://github.com/drewnoakes/dasher.git true true All ```
bennor commented 7 years ago

Hmm. I got it generating using a brand new NET Standard library. All I did was create the project, search for "template" in the Add Item dialog and add a new template. It regenerates on save (this is build into VS), and on build (AutoT4). I tested flipping the options to disabled and it stopped generating so it seems to be doing what it should.

Here's the .csproj file (this was all generated -- I haven't hand edited anything):

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard1.4</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <None Update="TextTemplate1.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>TextTemplate1.txt</LastGenOutput>
    </None>
    <None Update="TextTemplate1.txt">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>TextTemplate1.tt</DependentUpon>
    </None>
  </ItemGroup>

  <ItemGroup>
    <Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
  </ItemGroup>

</Project>

Would you mind following the above steps and seeing if that works. I'd just like to narrow it down to a project specific vs environmental issue.

drewnoakes commented 7 years ago

Nice. It's working.

For future, here's what I think happened. I converted from VS2015 .csproj to project.json/.xproj. That format probably never supported T4, and I wasn't updating the .tt because it was stable. So the relationship between the generator and the two files was lost in the JSON model. Converting to newer .csproj couldn't know to re-inject that relationship. I did it manually based upon your .csproj file.

So I think this issue is invalid. If you want to post a summary of this on Stack Overflow I'll accept your answer.

Thanks very much for your help!

drewnoakes commented 7 years ago

One small difference that might help others...

If you're generating a .cs file (or other type that needs to be compiled) be sure to use a Compile element, not a None.

Eg:

  <ItemGroup>
    <None Update="UnionTypes.tt">
      <Generator>TextTemplatingFileGenerator</Generator>
      <LastGenOutput>UnionTypes.cs</LastGenOutput>
    </None>
    <Compile Update="UnionTypes.cs">
      <DesignTime>True</DesignTime>
      <AutoGen>True</AutoGen>
      <DependentUpon>UnionTypes.tt</DependentUpon>
    </Compile>
  </ItemGroup>

  <ItemGroup>
    <Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
  </ItemGroup>
bennor commented 7 years ago

Glad I could help!

(Reopening to track the fact that the options dialog isn't visible for these projects.)

bennor commented 7 years ago

At this stage I can't seem to figure out how to attach properties to items in the new project types. I asked in the Extend VS Gitter but got no response, so I'm going to park this for now. I'm not sure how many people will be using T4 with NET Core anyway.