index > Visual Studio Team System - Domain-Specific Language Tools > How-to: create a custom tool that runs directly against a model f...

How-to: create a custom tool that runs directly against a model f...


The T4 engine that ships with the DSL tools gives you a flexible way to generate text output based on the contents of a model by running a custom tool against a template.

 

However, in a production environment, you may not want a user of your designer to have access to the template. You might prefer the user experience to be more like that provided by the Microsoft dataset generator, where the custom tool runs directly against the model file, rather than against a template.

 

This is very easy to do using the May release of the DSL tools.

 

1) Create and test your template as normal, but then put in a placeholder in the directive where the name of the model would usually go e.g. ??MODELFILENAME%??

 

2) Create a new class that inherits from TemplatedCodeGenerator. You can make life easier for yourself by including the following import statements:

 

using Microsoft.VisualStudio.TextTemplating.VSHost;

using System.Runtime.InteropServices;

using Microsoft.VisualStudio.EnterpriseTools.Shell;

using System.IO;

 

3) Add a new Guid custom attribute to the class e.g.

           

DuncanP
Duncan

This is very interesting - thanks for sharing it.

We are looking at how to provide better support for the scenario you describe, by making it easier to call T4 from within code. The solution we have in mind is where you add code to your generated designer to perform code generation. This might be hooked into a menu item or save or some other event, can crawl across the model using the same APIs you use from within a T4 template, and then fire one or more templates from model elements passed to it/them.

This is also more flexible in another way: it lets you apply a template to an elements within a model, so, for example, you could cycle through a set of pages in a UIP Chart, generating a separate code file from each page using the same T4 temnplate.


Stuart
Stuart Kent MSFT

Thanks Stuart.

The ability to run templates from code, to run templates against specified elements, and to generate multiple output files would all be very useful.

It would also be useful if the "designer code generation" functionality could be tied into the existing "Transform All Templates" control on the Solution Explorer to give the user a simple way to regenerate all code related to a model, whether it is in a template file in the project or controlled by the designer.

The ability to re-generate all output relating to a model automatically is an important part of moving to automated builds, and is one of the advantages of T4 over the previous document generation technology in the DSL tools. The usefulness of code-generation from within a designer would be reduced unless it could also be accessed as part of an automated build.

Duncan

DuncanP

Hi to all.

1) In this scenario often I work creating a new designer file by the "Add new item" option obtained by right click on solution window.

But for each new file, I have to set the "custom Tool Property" by hand with the name of my own custom tool. Is there the possibility to do this automatically?

2)As wrote in this thread, in my designer I can generate several file from the same source model; I overwrote the GenerateCode Method.

Under the model file, in solution explorer appears the generated file. But it is the file created thanks to this instruction in GeneratedCode method

data = base.GenerateCode(inputFileName, inputFileContent);
return data;

How can I add the others generated files under the model file?
Is this possible?

Thanx for your help

Paolo




Paolo Arrigoni - Pisa University - Computer Science Engineering Department
Paolo Arrigoni
An update on this for the June CTP:

For this to work with the June release you need to move the ProvideCodeGenerator attribute from the custom tool class to the package class.
In the DslPackage project (not the Dsl project) create a new file that includes a partial definition of the Package class. Apply the ProvideCodeGenerator attribute to that class eg:

namespace CompanyName.MyDsl
{
[ProvideCodeGenerator(typeof(MyCustomTool), "MyCodeGenerator", "Generates code from .mydsl models", true)]
internal sealed partial class MyDslPackage
{
}
}
Brian OByrne
Just a note. For this to work with VB project system (in June CTP, at least), ProjectSystem property should also be supplied.

[ProvideCodeGenerator(typeof(YourGenerator), "Name", "Desciription", true, ProjectSystem = ProvideCodeGeneratorAttribute.VisualBasicProjectGuid)]
.

Default is CSharpProjectGuid. You may decorate with this attribute twice, one for c# and one for VB.
gokhan.altinoren
reply 6

You can use google to search for other answers

 

More Articles

How to control order of transformation steps?
How to Export diagram as image
Experimental UI - show and hide shapes and connectors
Are these known problems
Reading and writing models in June release?
how can i get a Service into T4 textemplate.
Nested Shapes
Feedback
How to change the Name property when a Shape is droped into the d...
Documentation for Microsoft.VisualStudio.Modeling
Welcome to Bokebb   New Update   Joins the collection  
 

New Articles

A better name for a BlankLanguage
Checkbox list as property type which is …
circular dependency and custom properties
Designing ClassDesigner in DSL Tools?
How access to a connector property
Popup dialog in DSL
DSL and XMI interchange format
Create a diagram by using custom code
Is there any DSL Specifications?
Reordering items in compartments?
Replacing a null value....
How can i force the placement of a port …
Problem loading different models in Dire…
Retrieving embedded elements
Why the f$%^&* does the DSL tools as…

Hot Articles

Xsd To DslDefinition.dsl transformation
Troubleshooting mysterious crashes
DSL Tools V1 Released
Migrating .diagram files to V1 (Septembe…
Making a reference from a connector class
Resources: Domain Specific Language (DSL…
Language name has to start with capital …
Including code files in a DSL instance
Properties Mapping/Text Templates
Serialize reference type class member
Modal EditStyle and enumerations
Package Load Failure - ...TextTemplating…
constrain the number of a specific shape
GAC support in Templating Engine (Featur…
How access to a connector property

Recommend Articles

Self-contained Embedding
Stability of Modeling API
How to make the connector text parallel …
How to determine if a model passes valid…
How to set the tooltip for the shapes an…
Licensing & pricing
How do I get a new ModelElement's corres…
Compartment Shape for Model
Problem starting new DSL-project
Default values not stored
How to migrate / implement CustomStorage…
Use of the TextTransform tool
Announcing DSL Tools May 2005 CTP for Vi…
Error Creating DSL Designer Project
unable to find common parent for view fi…