T4 template for Ruby or Java - t4

I am using T4 for generating code. With Visual Studio I could generate code in C# or VB. What is required for generating code in Ruby or Java ?
I have a some utility classes that is required in multiple language (C#, Ruby and Java). I am looking for defining T4 templates...
Thanks

With a standard T4 template, you can generate any code, but what comes out is part of your project. While you can generate any kind of text, this is really most useful if you're compiling the code that comes out.
With VS2010, you can now use a T4 PreProcessed Template. Instead of generating a text file, you generate the generator. Making the generator instead of the resulting code should give you lots of flexibility with integrating your Java/Ruby output.
Anything can come out of the generator, but I think you still have to write the actual T4 code in VB or C#.

You can generate code in any language using T4 or indeed any other textual artifact.
You just need to start with an example of what you want to generate and begin to parameterize it.
Only the code generation control code inside the template needs to be in C# or VB.

Related

how do I create readable C++/CX document from XML generated by Visual Studio 2015

My project is written in C++/CX on VS2015 and I am seeking a way to generate API documentation.
After googling and stackoverflow, I have tried doxygen, VSDocman,NDoc, Atomineer Pro Documentation and SandCastle, I found these tools do not support C++/CX syntax, therefore, they cannot generate correct document.
I also tried to generate XML file which VS supports. But it's hard to read XML file.
How do I generate API documentation from C++/CX? Thanks for any suggestion
There are a lot of misconceptions about the C++/CX language extension. You tried too many products that have no hope of getting you anywhere. First and foremost is that it is a native C++ extension and does not generate a .NET compatible assembly. So knock out any that try to use Reflection to parse metadata. Out goes NDoc, VSDocman, Sandcastle. Atomineer is out, it is just an editor add-in.
So all you got left is doxygen.
Sure, it doesn't know about C++/CX out of the box. The FAQ points out that in order to make it compatible with a language, you need to modify src/scanner.l
Having a look at it, I see it already supports the C++/CLI extension. That's another C++ language extension that supports generating MSIL. C++/CX syntax is very close to C++/CLI. Just some minor differences, like the gcnew keyword is too misleading and replaced by ref new. But that's not the kind of syntax that doxygen cares about, it just wants to know about declarations. Those keywords are the same.
So the only obstacle I can guess at is that you just forgot to tell it about the language. It can't guess at it correctly from just the filename extension, .cpp and .h will get it to parse plain C++. Modify or add this line in the config file:
CPP_CLI_SUPPORT = YES
And tweak scanner.l if necessary.

Can ANTLR lexers/parsers be used for VS syntax highlighting and Error List?

I'm happily developing a language in ANTLR 4.4 / ANTLR4CS, and I'd like to start integrating my language into an IDE. The natural place for me feels like integrating it into Visual Studio. It occurs to me that someone might have figured out a general way to plug an ANTLR lexer into the Visual Studio syntax highlighting system, or ANTLR parsers into an MSBuild task such that errors appear in the Visual Studio error list.
Is there any kind of starting point, base package, plugin system, or similar which lets you integrate an ANTLR4CS language with Visual Studio's language support mechanisms?
Alternatively, has anyone had any success building, say, a Sublime Text build system / language def automatically from their grammar?
#Lucas Trzesniewski wrote:
I did this by reparsing the whole file every time, doing it on a line-by line basis was too much of a PITA for my needs (and our DSL files are small so it works fine).
I don't know if it'll be suitable for your needs, but you can implement a single file generator from a VS extension. The Generate method takes a IVsGeneratorProgress parameter which lets you report errors easily.

Is it possible to generate from code using T4

I have used T4 to generate partial classes from some input file (XML, etc) and then hand code additional partial bits onto those generated classes.
Is it possible to go the other way? To hand craft partial classes, and use T4 to template boiler plate bits to them?
Obviously I can't use reflection to look for the classes since it's not compiled yet, but I see Visual Studio inspect uncompiled code for different utilities. Perhaps Visual Studio offers some feature to support this I don't know about. Long shot, I guess.
Thanks
Also, you can use T4 with VS's CodeModel to read the code in your project without compiling and then generate from that metadata.
There's some pointers to examples here: http://blogs.msdn.com/b/garethj/archive/2009/09/25/dte-and-t4-better-together.aspx
Actually, T4 is used this way frequently. Yes, it requires reflection, but partial classes compile even if bits of them aren't generated yet. I would look at examples for generating strongly typed views as described here for examples of using reflection to generate new files.

For what purposes have YOU used T4?

T4 has existed for several years in Visual Studio, but doesn't get a lot of attention. However, for those that know it, there seems to be some very creative and useful purposes.
I am researching some different ways that T4 is used, and I would appreciate to hear how YOU may have used it for real life scenarios. I am primarily interested in non-standard and creative uses.
Some interesting examples:
Phil Haack uses T4 to create static CSS files from .less
To Generate WPF and Silverlight Dependency Properties using T4 Templates
Note: I realize this is a discussion-oriented question, but the answers could be helpful to others. I have tagged it as subjective and also marked as "community wiki", so please allow the question to remain open. Thanks!
I am not a big fan of the stringy-ness of app.config/web.config, so I use T4 to read those files and make an AppSettings/WebSettings class that wraps the connection strings and key/values in a real class. This means that, as long as I always use AppSettings.SomeValue to reference my app.config, I get compile time checking, which is really nice.
I've used the T4 Templates within the sharp-architecture to generate everything from models to controllers to basic views.
Definitely worth checking out, even if you just want to see some advanced examples of T4 templates in action
I use T4 to:
Generate CRUD SQL Server and Oracle scripts.
Generate Data Access Layer, based on a database schema
Layer Generate Business Logic Layer, based on a database schema
Generate ASP.Net webforms, both HTML and codebehind, based on a database schema (scafolding).
It gives me a good, quick, simple, basic starting point for my projects.
And the best is I'm in control.
Here you can download an example of my templates
SubSonic 3.0 makes heavy use of T4 templates for generating your entity code.
Essentially it calls GetSchema() on your database connection and runs each table it finds through the T4 entity template. The great thing about using T4 here is that if you don't like the way it's handling your database schema, just edit the template.
I've tweaked the T4's to handle MySQL databases better for my situation, as I make use of many tinyint columns which the default T4 maps to byte types. A quick edit to the T4 gave me the type I wanted instead for my application entities.
LINQ to SQL templates for T4
http://l2st4.codeplex.com/
Templates replicating the functionality of the SQLMetal and the LINQ to SQL classes designer code-generators for both C# and VB.Net requiring just Visual Studio 2008.
Check out this podcast on T4 by Scott Hanselman talking with Kathleen Dollard.
http://www.hanselminutes.com/default.aspx?showID=170
I've used T4 to generate:
proxies (design time, for injecting/wrapping monitoring
code/logging/... in a very specific exposed api).
interface generation for a one-on-one interface/class mapping
replace reflection
code by "directly/real" calling code (maintenance advantage of
reflection code, but performance of the actual code) for instance
when allowing access to properties through an indexer, or something
in that direction.
xml generation for a java project (couldn't find a
T4-like solution for java, that is easily shared within a company, T4
is easy because it's build in and you can run it from command line)
generate enums from a master database (we generated them both for a delphi-code base and .Net code base)
T4 Templates are used heavily in the Web Service Software Factory (Service Factory).
See here for a list of more than 30 T4 Generators from the community in several areas including ASP.NET,WCF, UML, ADO.NET, .NET
http://t4-editor.tangible-engineering.com/How-Do-I-With-T4-Editor-Text-Templates.htm

Visual Studio Language Service with C# intellisense

Last year I wrote a Language Service for Visual Studio which added syntax highlighting for NHaml files: http://github.com/snappycode/hamleditor.
To clarify, NHaml is a html template language that can mix in code elements like an aspx file can. This plugin adds support to the IDE for editing NHaml files, but basically only adds syntax highlighting.
I was wondering if anyone knows how to add inline c# intellisense to the service like you get now in an aspx file. I'm hoping that would be possible without doing the whole c# grammar myself specific for the plugin.
Has anyone written a language service that mixes languages?
UPDATE:
It looks like the spark view engine guys have made some inroads here, I am investigating their implementation
I checked the Spark View Engine, and they seem to have made a generic ATL stuff (called SparkLanguagePackageLib), that in fact seems to be not containiag anything Spark specific. It seems to be just a generic C# intellisense library that needs the following:
The original code
The C# source that gets generated from the original code
The position mappings between the two (for example the code on line 2 pos 5 gets mapped in the output to line 4 pos 10, etc.)
Some other things, like Paintings(?)
And after that you can call:
events.OnGenerated(
primaryText, // original source code
entry.SourceCode, // generated sourcecode
cMappings, // mappings between the two
ref mappings[0], // ?
cPaints, // ?
ref paints[0]); // ?
I've tried to find Spark-specific stuff in that C++ library, but I couldn't find anything: everythig spark-related is split to a separate C# code file. I think this is good, because:
You don't need to edit the C++ files
If the spark view engine's intellisense support is installed it can be used by other view engines too
You only need to create a class, that maps between the original nhaml file and it's generated C# counterpart.
Btw. Are you still working on this NHaml Intellisense library? If not I'll try to patch their implementation in hope it can be converted to NHaml easily.
this looks like it might help
http://www.codeproject.com/KB/recipes/VSLanguageService.aspx
I finally managed to modify the code to support NHaml. It wasn't that hard at all. Unfortunately the original NHaml library doesn't support everything that was needed, so I had to create a new parser for NHaml. It doesn't support all of the constructs, but it supports most of them (enough to make NHaml programming easier)
Download: http://github.com/sztupy/nhamlsense
Screencast: http://www.youtube.com/watch?v=8jTZ2zC9eYc
You can easily add keywords by creating or modifying a usertype.dat file. Check here for some directions on attaching to specific file extentions. That might get you at least part of the way, without redoing the complete c# syntax.
(In fact, I'm not sure what you mean exactly by 'syntax highlighting' in this context. I'm sure, for instance, you get brace-match highlighting for free in the editor).

Resources