One month ago, I described an extension to the DslDesigner to author modelbus. This is a real life example of what could be a MEF extension to our authoring tool. Writing such an extension is easy as shown in the “How to: extend the Domain-Specific Language Designer” page on the MSDN library and in the sample named SimpleDslDesignerExtension in the code gallery download tab for the VsVmSdk
I terminated my article by promising to explain how you could, yourself, enable MEF extensions to your DSL, so that your customers, partners, colleagues or even yourself can write extensions targeting your DSL. This is the object of my post today.
The first step is to go to the download tab of the Visual Studio Visualization and Modeling SDK on the code gallery, and download the following zip file: Enabling a DSL for MEF extensions
This file contains a few text templates that really only include text templates provided in the SDK. Those are in subfolders depending on whether you’ll need to add them to the Dsl or to the DslPackage project.
To MEF-extension enable you DSL, as a DSL author will need to:
1/ Add DesignerExtensionMetadataAttribute.tt, GestureExtensionControler.tt, GestureExtensionEnablement.tt (form the zip file) to the DSL project in the Dsl\GeneratedCode folder.
2/ Add CommandExtensionRegistrar.tt, CommandExtensionVSCT.tt, PackageExtensionEnablement.tt, ValidationExtensionEnablement.tt, ValidationExtensionRegistrar.tt (also in the zip file) to the DslPackage project, in the DslPackage\GeneratedCode folder
3/ Add one line to the DslPackage\Commands.vsct to include the newly generated CommandExtensionVSCT.vsct (from CommandExtensionVSCT.tt): <Include href="GeneratedCode\CommandExtensionVSCT.vsct"/>
<?xml version="1.0" encoding="utf-8"?> <CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <!-- --> <!-- This file contains custom command definitions. –> <!-- --> <!-- NOTE: Each time commands are added or changed, the "version" parameter to the –> <!-- ProvideMenuResource attribute in Shell\Package.tt should be incremented. –> <!-- This causes Visual Studio to re-merge the menu definitions for the package. –> <!-- Otherwise, changes won't take effect until the next time devenv /setup is run. –> <!-- --> <Extern href="stdidcmd.h"/> <Extern href="vsshlids.h"/> <Extern href="msobtnid.h"/> <Extern href="virtkeys.h"/> <Extern href="DSLToolsCmdID.h"/> <Include href="GeneratedCode\GeneratedVsct.vsct"/> <Include href="GeneratedCode\CommandExtensionVSCT.vsct"/> <Commands package="guidPkg"> </Commands>
Transform all templates, and rebuild your DSL. It is now enabled to discover MEF extensions. That’s that simple!
To write an extension, do something similar to “How to: extend the Domain-Specific Language Designer” , but of course referencing your Dsl and if necessary DslPackage DLLs, using your own namespaces, and your concepts.