Design-time T4 templates in ASP.NET 5 (VS 2015) - t4

I can't seem to find a way to make T4 templates in VS 2015 RTM, in an ASP.NET 5 (vNext) project.
I even installed the T4 toolbox for Visual Studio 2015 extension, but the tt templates are not transformed.
The property Custom Tool doesn't appear in the tt file properties, neither can't I find the 'Run Custom Tool' command.
Update
The reason I want the T4 templates, is the introduction of the config.json file, and the pluggable configuration system, which is an awesome thing, but with the price of not having the setting properties strongly-typed.
I've read this article that explains how to achive this, but there is still no generation. Since I have a pretty complex configuration structure, I thought about making a T4 template that will generate an AppSettings file. Any ideas on that are obviously welcome too.

The ASP.Net 5 (vnext) project is a completely new animal and technically still in beta, its not scheduled for RC til November 2015. Also it's attempting to be completely cross platform so initially the team favored using razor templates instead of T4 for scaffolding. They had no plans to support T4 (or any single file generators) at all until an out cry from the community made them change their mind. According to that thread they will support it but have given no dates. They do seem to have made progress, back in January when I was testing my T4 extension I had issues with the project file(now in json format) not supporting custom properties but as of the release on 7/20/2015 it seems to work now. The engine for running T4 inside of visual studio 2015 is still there so you can use it if you like from other project types. You can create a console app and have it store the T4 files but generate them in the vnext project. If you want a cleaner solution you can also try out my extension T4 Awesome, it gives you a way to organize and call your templates via right click menus.

I've found that I can still use the MSBuild targets that ship with the Modeling SDK for Microsoft Visual Studio 2015 when building an ASP.NET 5 project with ASP.NET 5 RC1.
I got there by modifying the directions from
MSDN - Code Generation in a Build Process.
Add the *.tt and the output files (*.cs in my case) to the project folder structure.
Unload and edit the project's *.xproj file.
Add the following Import element after the Microsoft.DNX.targets import:
<Import Project="$(MSBuildExtensionsPath)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TextTemplating\Microsoft.TextTemplating.targets" />
Add an ItemGroup element similar to the following (I added this immediately before the import statements):
<ItemGroup>
<Content Include="MyTemplate.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>MyTemplate.cs</LastGenOutput>
</Content>
</ItemGroup>
Optionally, you can add elements to the Globals PropertyGroup element to control the transformation task:
<TransformOnBuild>true</TransformOnBuild>
<TransformOutOfDateOnly>false</TransformOutOfDateOnly>
<OverwriteReadOnlyOutputFiles>true</OverwriteReadOnlyOutputFiles>
Reload the project's *.xproj and build normally.
The templates I'm using are pretty simple, so there might be limitations of this approach that I'm missing out on.

If you watch Julie Lerman's pluralsight video: http://www.pluralsight.com/courses/entity-framework-7-looking-ahead she addresses this. There are no plans as of now to remove the T4 templates in upcoming versions of Visual Studio but they didn't make it in for the release. You code always run the reverse engineer tool on your database and go with a code first approach and switch back later (though I don't know why you would in my opinion) but that would be a work around until there is more information on the T4 templates in current VS versions.

Related

How to get NuGet options in Visual Studio 2017 Community?

I am struggling with creating NuGet packages. I am using Visual Studio 2017 Community edition.
I have seen a couple of videos that show a "Pack" option on the menu when right-clicking the project in Solution Explorer. However, I do not have that option. Is this one of the features in the other (non-Community) versions of Visual Studio? I believe I have also seen a "create NuGet package on build" option mentioned somewhere. I cannot find that either.
I have tried various ways of using nuget, dotnet, and msbuild from the command line(s), but haven't had much success. Very frustrating.
Any help is appreciated.
If you really want to use Visual Studio, I would recommend installing an extension that helps you with that problem. For example, this one. The options people have in videos depend on the extensions they have installed. For you, it is the same.
Alternatively, just use the command-line tooling for this as explained here or for .NET Core here or here.
dotnet/msbuild pack is only available for SDK-style projects, but I believe works for all versions of Visual Studio, as well as on the command line. .NET Core introduced these SDK-style projects, which can be identified by <Project Sdk="Microsoft.NET.Sdk">. If your project (.csproj if it's a C# project) doesn't have the Sdk property or import Microsoft.NET.Sdk in either of the two other ways, then it's not an SDK style project and doesn't support packing in this way. Another obvious difference between the two styles of projects is that SDK projects are only a few lines long from the new project template and don't list files in the project, whereas old style projects are typically a full screen long, even from a new project template with only a single class file, and it does list individual files in the project. If you want to continue with this project type, you'll need to use nuget.exe pack and you'll probably want to create a .nuspec file to define some of the package metadata.
However, using SDK style projects is the future, it just takes time for all of Microsoft's existing project types to migrate. It's much simpler to use, so personally I would avoid old style projects unless you're using a project type (like ASP.NET, not ASP.NET Core) that doesn't support it.
All of this is confusing for anyone new to the .NET ecosystem. My recommendation is 1. when you install Visual Studio, when making your workload selections, make sure in the component list that .NET Core is selected, whatever the newest version of .NET Core that is available at the time of installation. When creating a new project in Visual Studio, always select the .NET Core version, or .NET Standard version of any new project template, even if you want to target the (Windows) .NET Framework, in which case you edit the .csproj and change <TargetFramework>netstandard2.0</TargetFramework> to <TargetFramework>net45</TargetFramework>, although I would recommend multi-targeting possible by adding a s to the element name and using a semi-colon separated list: <TargetFrameworks>net45;netstandard2.0</TargetFrameworks>. So, avoid the "Class Library (.NET Framework)" template, instead use "Class Library (.NET Standard)" and then change the target if you have to.
#zivkan led me down the right path. Changing my project types to .Net Core from .Net Framework made all the options I mentioned in my original post available. No extensions were needed.
My .Net Core class library project now has the Pack and Publish options available on the project's context menu. In addition, there is a another tab (Package) on the project properties page. On that page there is a "Generate NuGet package on build" option along with version, name, tags and other properties.
I have done much .Net framework development, but have been ignoring .Net Core and the newer options. I guess I need to dig in and learn about them.

Cross-targeting frameworks with NuGet 4.0 and Visual Studio 2017

I am having a rough time figuring out how to setup cross-targeting inside a Visual Studio 2017 project and I have not been able to find any examples.
I started out with a .NET Standard 1.5 project and to keep it simple I am just trying to add .NET Standard 1.6. If I understand the documentation correctly, I should now be able to do all of this inside the csproj file without having to mess with a project.json or nuspec file.
I've tried all of these values but none seem to work:
<TargetFrameworks>netstandard15;netstandard16</TargetFrameworks>
<TargetFrameworks>netstandard1.5;netstandard1.6</TargetFrameworks>
<TargetFrameworks>.NETStandard,Version=v1.5;.NETStandard,Version=v1.6</TargetFrameworks>
This is the only source of documentation I can find on the feature and it doesn't contain a full example:
https://docs.nuget.org/ndocs/schema/msbuild-targets
https://docs.nuget.org/ndocs/create-packages/supporting-multiple-target-frameworks
I've gotten this to work on latest Visual Studio 2017. As described in this post https://blogs.msdn.microsoft.com/dotnet/2016/10/19/net-core-tooling-in-visual-studio-15/ it is the correct way to do it. My csproj file looks like this:
<PropertyGroup>
<TargetFrameworks>netstandard1.6;net452</TargetFrameworks>
</PropertyGroup>
Visual Studio 2017 RC release notes also has this listed as a feature (under .NET Core and Docker):
Cross-target multiple target frameworks in one project.
My mistake at the start was that when I first created the project the property was called TargetFramework, I tried to add multiple targets and VS did not like that at all. It just crashes then... So make sure to rename it to TargetFrameworks and it should work.

Visual Studio: Setup Project changes based on build configuration?

I have a project that was built for company X. Then they decided to allow company Y to use the product - and they wanted to make some minor branding-type changes. I come from a C programming background, so I added another build configuration that specifies a conditional compilation symbol depending on which brand the solution is being built for. Then the source code has a few:
#if COMPANY_X
// do stuff
#elif COMPANY_Y
// do different stuff
#endif
Now here's my question: Can I use the solution's build configuration to manipulate a single (Visual Studio Installer) setup project? Or do I have to maintain multiple setup projects to manage the differences between them (app name, install folder, manufacturer, etc.)? (Or perhaps more fundamentally, am I going about this all wrong?)
Background info: Visual Studio 2010, Visual Studio Installer, C#
You can use single vs 2010 solution and create multiple solution configurations (ideally for different company or different environments).
VS 2010 Menu --> Build--> Configuration Manager--> Select new from Active solution configuration. Using this approach you dont have to write conditional builds.

T4 code generation without Visual Studio 2010?

Is it possible to run T4 code generation without needing Visual Studio 2010? Basically I have to build an in house ORM (don't ask..if I had a choice I wouldn't). I was planning to use subsonic as a base but change some things and how they work. However my main question is can I run T4 from an external application that I write, so I can use the features of T4? Or am I better off doing it myself (which I doubt)?
TextTransform.exe will do what you want for simple scenarios:
http://msdn.microsoft.com/en-us/library/bb126245.aspx
Here is how to run a T4 template from your own code for templates created in VS 2010:
http://msdn.microsoft.com/en-us/library/ee844259(VS.100).aspx
And here is how to run a T4 template from your own code for templates created in VS 2008:
http://www.capprime.com/software_development_weblog/PermaLink,guid,104d9faf-5780-42ca-88e5-c04cb88f61b3.aspx
There will be some issues running Subsonic T4 templates outside Visual Studio:
How can I automate the t4 code generation for SubSonic
I would stick to T4 rather than roll your own template engine.
T4 is a part of Visual Studio. If your ORM tool can assume that Visual Studio is available, T4 is a good choice. You have an option of redistributing the Visual Studio shell, which also includes T4, with your application. Alternatively, you can use preprocessed templates to compile the templates into executable code generators. In compiled form, these templates don't require Visual Studio, but also cannot be modified.
Oleg
AFAIK T4 templates are invoked from within Visual Studio IDE.
Building an ORM needs more than text templates. I suggest you look into AtomWeaver (at http://www.atomweaver.com) which is a code generator that lets you build models from individual building blocks (called "Atoms"). These Atoms are smart templates that act both as text templates but also as mini-programs, allowing you to do much more that simple string substitution.
You can develop your own "Atoms" that transform a database structure into source code. Then, for each new database, you combine these Atoms to build your schema, and fire up the generator to obtain the source code. Because what you've built was actually a model of your DB, you can later make any changes and regenerate your code.
AtomWeaver implements ABSE, a kind of model-driven software development (has nothing to do with UML or MDA). Learn the mechanics of ABSE at http://www.abse.info
AtomWeaver is presently in public beta. There isn't much documentation at this point, so you may have a hard time getting up to speed with it.
There is a command line utility called TextTransform.exe you can use to generate code for a T4 template. I can't comment on if it is the right tool for building a ORM, but I like it well enough for generating state machines from an XML file.
http://msdn.microsoft.com/en-us/library/bb126245.aspx

Visual Studio class/file templates: Is there a way to change their content automatically per project/solution?

I've updated my default templates in Visual Studio for classes, interfaces, code files, etc. I removed the default namespaces and added a copyright header blurb.
Is there a way to use a variable or something in the template so I don't have to zip/unzip and re-run the vs installer to change the copyright header? (I'm a consultant, the code-owner isn't always me or my company).
Yes, you can. The documentation for this sort of thing is part of the Visual Studio SDK. There are already many variables you can use.
If you find you want to get fancy, look into the Guidance Automation Toolkit. A template using GAT can accept user input as well as information from the project and environment, can unfold one or more templates, filling in placeholders with the data gathered, and then can execute various actions against the unfolded templates, the project, or whatever.
You can get the complete example implemented here: Multi-Project Templates with Wizard: Visual Studio 2010 Sample

Resources