Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

An Introduction to the New Visual Studio Snippet Format  : Page 3

Visual Studio 2005 ships with a pre-built—and extensible—personal code library that it makes available as "snippets" in the IDE so you can drop pre-tested chunks of code into your projects easily.


advertisement
Packaging Your Snippets For Distribution
With the Visual Studio Community Content Installer (VSI), it can't get much simpler to create a redistributable version of your code snippets. This is a three-step process that you can also use to package Visual Studio project templates, starter kits, and controls, and even expand upon by registering other content handlers. But that is beyond the scope of this article. There are in-depth instructions on MSDN for those looking to take full advantage of the VSI. The primary difference between the various types of distributions lies in the contents of the XML manifest, or Meta file. Visual Studio Community Content Installer files have a .vsi extension and are handled by a standalone EXE on the user's machine. The VSI came to fruition from a need to create a way to easily and consistently exchange Visual Studio content to be shared within the developer community. Through a simple wizard, the VSI helps the user install snippets in the specific registered location required by Visual Studio .NET.

For distributing snippets, the VSI package is made up of your .snippet files and a .vscontent file, which is an XML manifest. The XML manifest for snippet files has a root element named VSContent, which in turn must contain a Content element for every snippet you want to include. The content element has the following child nodes:

  • FileName—The name of the snippet file.
  • DisplayName—The friendly name displayed in the VSI interface.
  • Description—The Tooltip text for the item.
  • FileContent—The valid value for code snippet distributions is Code Snippet.
  • ContentVersion—For now, this should always be 1.0 (at this point, all content types are 1.0).
  • Attributes—The parent node for a set of Attribute child nodes.
  • Attribute—A valid attribute name/value pair for this element is lang with a value of either csharp, jsharp, vb or XML.
Author's Note: Code snippets require the lang attribute. Other attributes include TemplateType, ProjectType, and ProjectSubType. Other valid FileContent values are Addin, Macro Project, VSTemplate, and Toolbox Control.

A completed XML manifest to install snippets should look similar to Listing 1.

Now that you have your snippet files and your manifest, you are ready to package it. Add those files to a ZIP archive. Once zipped, change the file extension to .vsi, and you're done. Your snippets are ready for distribution. When you are ready to test your package, or install a package you received, simply double-click on the .vsi file, or if downloading from the Web, choose Open from your download dialog and the installer will display all the snippets that were included in the VSI package, including any information about the publisher if it was signed.



If you are not familiar with the publisher, if the .vsi file isn't signed, or you're downloading from a third-party site, I recommend clicking the Review button. This will open Windows Explorer and display the files included in the package via a temp file, giving you the chance to scan it for viruses. Reviewing the contents will help protect you from blindly installing malicious or unknown file types. Furthermore, any files included in a VSI package should be viewable with Notepad, so you can review the code. After you have reviewed the code and deemed it safe, click the Next button. This will display the Installing Content dialog with links in the Details pane for each item you want to install. After checking all the desired items, click Next and then choose the location where you want to install them, and—if applicable—configure how they should be installed. Click Next to complete the installation. There's always the possibility for naming conflicts; if they occur you'll be given the opportunity to overwrite, cancel the installation, or rename the snippets.

When the installation is complete (or failed to complete), links appear next to each item. Clicking these links displays the install status. If any failed to install, you can use the status report to fix the issue and attempt to install again. When you're done, click Finish to wrap up the install. Visual Studio records every VSI package you install in a history file named ContentInstallerHistory.xml located in the C:\Documents and Settings\\My Documents\Visual Studio 2005 folder. Note that this is a hidden file, so you may have to change the view properties of that folder to display all the files.

In my opinion, snippets are an excellent addition to the features in Visual Studio .NET. You obtain better organization of your code, scalability through replaceable parameters, and gain another step in efficiency; "Work smarter, not harder"...right?



Michael Sanchez is a Web-based software developer doing business as Libretto Software and Runtime Web Development. He currently develops applications for Sharp Healthcare.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap