Поделиться через


DLR Hosting Sample : QikScript - Handle Visual Studio 2008 extensibility events in IronPython

The DLR Hosting API offers a well defined interface for managed applications to host and utilize the power of the DLR (Dynamic Language Runtime) to enable end user scripting. The objective of this sample (and the forthcoming series of posts based on this sample) is to demonstrate and explain the usage of the API in doing those things.

QikScript is a Visual Studio addin that hosts the DLR enabling end users to write python scripts to handle events fired by Visual Studio.

As part of its extensibility model, Visual Studio defines events that can be subscribed to through an add-in. Add-ins can also access ‘live’ objects like the project system, editor, menus, output window etc in the hosting instance of visual studio. By Hosting the DLR and routing the events appropriately, QikScript lets IronPython scripts to handle these events and access VS objects at runtime.

How to Install

1) Extract the contents of the attached zip file to a folder on disk( say C:\QikScript)

2) Copy the ‘QikScript.AddIn’ file to the VS 2008 AddIns folder. is the Addin folder is present in C:\Documents and Settings\<user name>\My Documents\Visual Studio 2008\Addins or C:\users\<user name>\My Documents\Visual Studio 2008\Addins

3) Open the AddIn file in a text or XML editor. Edit the value of the <Assembly> tag to indicate the full path to the ‘QikScript.dll’ file you copied in step 1.

4) If you extracted the files to a disk folder other than ‘C:\QikScript’ do the following. Go to the folder where you extracted the binary files. Open up the ‘Settings.XML’ file. Edit the ‘FilePath’ node in each of the ‘handler’ nodes to refer to the full path of the ‘handler.py’ file. (You can do a search and replace over the entire file).

5) The ‘handler.py’ contains the event handlers. You can write your own code in these methods and it will be invoked the next time VS fires that event.

Once you install the add-in, the next instance of VS that you start will invoke functions defined in the handler file. The simplest way to check if the addin is loaded succesfully is to take a look at the 'outpu' window of the VS. The dropdown box would contain an entry with the name 'QikScript' if loading is succesful.

Depending on your settings, it is possible for the addin not to load on startup. In such cases, you can go to the 'Tools->Addin Manager' menu item and manually select the addin, make it autoload and restart VS. Please do not hesitate to let me know via email or comments if you are stuck with the installation steps.

How to disable or remove the addin

The easiest way to disable this addin is to move or delete the QikScript.addin file from the AddIns folder. You could also change the extension of this file if you want to temporarily stop the VS from loading this. If you want to load VS without *any* addin you can use the ‘devenv /safemode’ from the Run prompt to start VS.

Package details

The deployment zip file contains the binaries that implement the functionality, an event handler definition XML file (settings.xml) and a python file (handler.py) that implements the event handlers. The DLR and ironPython binaries are from the IronPython 2.0 Beta 3 release

Settings.xml

· Settings.xml contains the details about the event handlers. You can edit the individual events to run a specific function in any given python file. 

· The settings.xml must reside in the same folder as the QikScript.dll file.

Handler.py

· The Handler.py file contains the event handlers.

      

 

· Each of the handler defined in this file uses the correct signature needed to work with VS. Some of the arguments are primitive data types – string, bool , int and some are VS specific types.  

· Note the commented out line just above the event handler. That line indicates the types of the arguments passed to the event handler.

 

The event handlers can also access the host VS object through the ‘hostApp’ variable in the script. The type of the object is DTE2. All output (like print calls) from the script is redirected to a new output window (with the name ‘QikScript’) that the add-in creates.

Notes

· This is strictly intended as a demo tool and comes with zero warranty or support.

· This addin doesn’t make VS an IronPython editor. You would use an external editor to edit the IronPython code. See this post if you are interested in enabling VS to edit IronPython sources

· I have used this addin only as part of Visual Studio 2008. It may or may not work in earlier versions.

· Theoretically, all operations supported by the VS through the extensibility model( the DTE2 interface) can be performed from any of the handlers. However, I did not attempt any testing to see which operations work and which don’t. So you are on your own when trying out adventurous thingsJ.

· Currently only ‘IronPython’ is available as the scripting language

This post is very light on implementation details deliberately. I am planning to put up a separate post with more details about the parts of the Hosting API this component uses.

Please let me know of any comments, questions, feedback or suggestions you have regarding this sample, DLR and/or the Hosting API.

QikScript.zip

Comments