T4 code generation without Visual Studio 2010? - 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

Related

T4 templates vs TT Templates vs Includes in visual studio

In visual studio there are 2 types of text transformation templates file extensions .T4 vs .TT
I would like to know their differences and which one I should use when I want extend to build Views Controllers and Models, when I read a schema off a DB
I also want to know if .includes can be reused in both.
There is no difference. Back around 2008 this feature was treated as if it were an add-in (even though it ended up being built into VS directly). Microsoft called it the "Text Template Transformation Toolkit," hence the .T4 extension. Common usage shortened that to "text templates" yielding the .TT extension and this appears to have become the standard extension.
Transformation files are just code, they can use any feature of the language you choose. For example, in C# templates I regularly reference .NET assemblies as follows:
<## import namespace="System.Text" #>

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

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.

Visual Studio project generation

Is there any way to auto generate visual studio projects with a, lets say, some kind of UML tool, so I can design project dependencies upfront and it will output the solution and its projects.
Bonus: I can do classical UML design (classes, interfaces, etc.) aswell
I'd appreciate any hint for any tool.
You could try GenMyModel. It lets you doing your model, writing and executing your own template generators directly from your browser.

How to integrate a custom antlr4-based language into Visual Studio

I'd like to integrate my own custom language in Visual Studio.
I'd like to have:
Syntax highlighting
invoke a custom compiler, producing source files in another compilable language. These shall be compiled in a second phase.
some simple tools like rename (not text based, so that a local variable is only renamed within scope), find code lines that reference this function/variable etc.
Intellisense (code completion, suggest members, types etc.)
Is there a way to achieve this without too much effort?
I already discovered Xtext (limited to a subset of antlr3) for eclipse, but need a similar tool based on antlr4 for visual studio?
Check out the Extending Visual Studio chapters in MSDN, especially
Language Services
Projects and Solutions
Editors
There are a few other blogs and tutorials on these topics as well:
CodeProject
ANTLR and MyC
And a few language services are open source and provide a great example:
IronPython
IronRuby
ASP.NET Web Stack

What type of extension for VS (and how) to make, to generate C# or C++ code from some text [more so a model]?

I am new to Visual Studio Extensibility and want to make an addin/extension which shall do the following:
It should read all the files with a specific file extension (assume "*.ump").
It should process the text/code/whatever in the files.
It should create new Class/Code file with some code in it. [The code will be produced in step 2, just need to know how to do it?]
Yet, I have been racking my brains through extensibility, saw the single file generators .... and addins which go through ProjectItems and can detect the file extension,
BUT I HAVE NOT BEEN ABLE TO FIND a complete tutorial, guide or explanation as to how or what to do!!
Please help...
You don't want to read all files with a specific file extension in Visual C++ project nor standard Visual C# project. You may do that with hand-made MSBuild project (included in the solution).
In Visual C++ projects, there is a way to define custom tools. They are run as separate processes, so you can implement them in anything you want. Studio will ask you whether you want to define a tool (they are defined in special xml files; studio has dialog for editing them) when you add a file with extension unknown to it. In Visual C# projects, just manually write a MSBuild tasks and insert them into the project.
Do whatever you want. IIRC the generated files will have to be included in the project though. Well, for MSBuild, just tweak the project to your heart's desire, but in Visual C++ they have to.
You can combine MSBuild (csproj,vbproj) and VisualC++ projects in a single solution, so I recommend using separate.
If you ever find out you need to compile for different target where you can't use Visual Studio, you'll be glad that you have stand-alone tool you were just calling from Studio and not something that embeds in it.

Resources