T4 Template in VS 2015 - t4

How do I get T4 Template support in Visual Studio 2015?
In VS 2015 the file property does not allow to specify the *tt file to be marked and allow to run from context menu.
I am using VS 2015 CTP6.

Tried VS2015 Ultimate CTP6 and T4 support is in (as well as debugging T4 which is nice). One specify TextTemplatingFileGenerator as Custom tool as before and when saving the .TT file it is executed.
There's a context menu called "Run Custom Tool" which seems to execute the .TT file as well.

As far as I can tell, the properties in the csproj have to be correct for the t4 template debug options to show up. This works for me in vs 2015 update 1
There's two sections, the template and the output file.
<Content Include="Entities\DataGenerator.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>DataGenerator.cs</LastGenOutput>
</Content>
<Compile Include="Entities\DataGenerator.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>DataGenerator.tt</DependentUpon>
</Compile>
*Note that TextTemplatingFileGenerator is case sensitive.

Related

Where is Visual Studio Build Action stored?

I'm writing a parser to pull data from various programs.
Some of the programs have "Build Action" set to "None" and I want to skip them.
I was guessing that this property would be in the .csproj, .btproj file, but I don't see it there. So my question is where is it stored?
I'm specifically dealing with BizTalk orchestrations, but I think the same concept would apply to C# and C# project files as well. I'm using Visual Studio 2015 to be compatible with BizTalk 2016.
It seems to create a "None" XML element in the .btproj file as shown below.
I was expecting to be an attribute or element value, not an xml element itself.
<ItemGroup>
<None Include="Orchestrations\PublishPIHistoricalData_v2.odx">
<SubType>Task</SubType>
<TypeName>PublishPIHistoricalData_v2</TypeName>
<Namespace>ABC.Integration.BizTalk.ProcessDataHistorian.Orchestrations</Namespace>
</None>
whereas a compiled orchestration will look like this:
<ItemGroup>
<XLang Include="Orchestrations\PublishPIDataODS.odx">
<SubType>Task</SubType>
<TypeName>PublishPIData_ODS</TypeName>
<Namespace>ABC.Integration.BizTalk.ProcessDataHistorian.Orchestrations</Namespace>
</XLang>

How to apply nested files in Visual Studio 2019 for Class Library project

Microsoft Visual Studio Community 2019.
Version 16.9.5.
I want *.Generated.cs files to be nested under corresponding *.cs file in a Class Library project exactly like in does in a RestApi Console Application project.
File nesting is enabled for both projects, Active Settings is set to 'Web' for both.
Using Visual Studio 2019 (16.10.3) here. Based on the recommendation of another answer I got it to work by putting the following in the .csproj file, then reopening the solution. I don't know if it matters or not, but I put this as the last elements group the closing </Project> tag in the file.
<ItemGroup>
<ProjectCapability Include="DynamicDependentFile" />
<ProjectCapability Include="DynamicFileNesting" />
</ItemGroup>

T4 templates not generating output in new VS2017 csproj projects

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?
.tt files are only auto-run by VS on save. You can install AutoT4 to have them run before/after build. (Be aware that at the moment there is a limitation with the new .csproj files - the options don't show up for them in the properties window.)
If you've converted from the old project.json/.xproj format, you may need to add the template to the project explicitly:
<ItemGroup>
<None Update="Foo.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>Foo.cs</LastGenOutput>
</None>
<Compile Update="Foo.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>Foo.tt</DependentUpon>
</Compile>
</ItemGroup>
Related GitHub issue
Edit
As mentioned in the comments below, you can do this quickly & easily by excluding, then including the template in your project.
I realise this is 2+ years old but for those bumping into this issue years on like me, the method listed below works for me without installing anything. I had the exact same issue, after upgrading a project from Visual Studio 2010 to Visual Studio 2017. YMMV. Make a backup copy of your .csproj file before you start.
Forcing rebuild of all .tt files when you build your project can be achieved without installing anything, by editing the .csproj project file. Editing the .csproj file seems clunky, but is is the approved way https://learn.microsoft.com/en-gb/visualstudio/modeling/code-generation-in-a-build-process?view=vs-2015
Within your .csproj file, you will find lots of PropertyGroup nodes. At the end of the list of PropertyGroup nodes (position not critical), add another PropertyGroup node with this content:
<PropertyGroup>
<TransformOnBuild>true</TransformOnBuild>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
</PropertyGroup>
Now look near the end of the .proj file, and you will see a line like this:
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
(For interest, on my computer with VS2017 on it that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\Microsoft.CSharp.targets)
Beneath that line, add a line like this:
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets" />
(For interest, on my computer that resolves to C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\Microsoft\VisualStudio\v15.0\TextTemplating\Microsoft.TextTemplating.targets)
YMMV. If yours is a web project, there is probably a line nearby that is similar but to do with Microsoft.WebApplication.targets, from which you can draw inspiration.
That, possibly with a restart of Visual Studio, should do it. If you delete the transformed file that your .tt file emits, and then do a rebuild of your project, you should see that the emitted file reappears.

Hot to get Visual Studio 2010 to ignore files with a .java extension

I have a web site that includes some Java sample code. Visual Studio insists that it is J# code and complains that it can't compile it because J# is not available.
If I exclude the files from the project then Visual Studio renames the files, breaking them.
What do I do?
You can mark them as 'content' in the csproj. Either click on the item in solution explorer -> properties and set BuildAction to Content, or edit your csproj by hand and change
<Compile Include="Whatever.java" />
to
<Content Include="Whatever.java" />

How to enable 'Add Config Transforms' for old Visual Studio 2008 project?

I am working in the new Visual Studio 2010 RTM and I would like to use web.config transforms.
My site is configured to use .NET 4.0 but it was formerly a Visual Studio 2008 web application project.
When I right-click on my web.config file I do not see the 'Add Config Transforms' option as I should. I also tried adding creating a new web.config but I still do not see the transform option.
Does anyone know how to enable web.config transforms for projects in Visual Studio 2010 that were originally created in Visual Studio 2008?
I was able to get this to work with my existing project.
I did it by opening my csproj file in notepad and comparing the child elements to those of a brand new ASP.NET MVC project for VS2010.
I then removed several elements that I didn't need and saved and reloaded my project. Then I was able to select 'Add Config Transforms.'
I do not know exactly which element was the culprit but I would guess it was either <ProductVersion>9.0.30729</ProductVersion> or <OldToolsVersion>3.5</OldToolsVersion</>.
I was able to get this to work in my converted project by opening up the .proj file and adding the following:
<ItemGroup>
<Content Include="Web.Staging.config">
<DependentUpon>Web.config</DependentUpon>
<SubType>Designer</SubType>
</Content>
<Content Include="Web.Release.config">
<DependentUpon>Web.config</DependentUpon>
<SubType>Designer</SubType>
</Content>
</ItemGroup>
Then I copied my existing web.config twice into the web root and renamed them Web.Release.config and Web.Staging.config
In VS I right mouse clicked and included them in the project
I then opened them up and added
xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform"
to the configuration node so it looked like:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
You have to have matching Release and Staging configuration names (using configuration manager).
After this VS recognised them as web configuration transformation files

Resources