From: Daniel C. \(kzu\) <dca...@us...> - 2004-10-10 15:29:51
|
Update of /cvsroot/mvp-xml/Design/v1/src/CustomTools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10616/v1/src/CustomTools Added Files: CustomTool.cs Log Message: Base class for custom tools, that provides helper methods and properties as well as registration methods. --- NEW FILE: CustomTool.cs --- #region using using System; using System.CodeDom; using System.CodeDom.Compiler; using System.Collections; using System.Diagnostics; using System.IO; using System.Reflection; using System.Runtime.InteropServices; using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using Microsoft.Win32; using EnvDTE; using VSLangProj; #endregion using namespace Mvp.Xml.Design.CustomTools { /// <summary> /// Base class for all custom tools. /// </summary> /// <remarks> /// Inheriting classes must provide a <see cref="GuidAttribute"/>, static /// methods with <see cref="ComRegisterFunctionAttribute"/> and <see cref="ComUnregisterFunctionAttribute"/>, /// which should call this class <see cref="Register"/> and <see cref="UnRegister"/> /// methods, passing the required parameters. /// </remarks> public abstract class CustomTool : VisualStudio.BaseCodeGeneratorWithSite { #region Constants /// <summary> /// {0}=VsVersion.Mayor, {1}=VsVersion.Minor, {2}=CategoryGuid, {3}=CustomTool /// </summary> const string RegistryKey = @"SOFTWARE\Microsoft\VisualStudio\{0}.{1}\Generators\{2}\{3}"; #endregion Constants #region Properties /// <summary> /// Provides access to the current project item selected. /// </summary> protected ProjectItem CurrentItem { get { return base.GetService(typeof(ProjectItem)) as ProjectItem; } } /// <summary> /// Provides access to the current project item selected. /// </summary> protected VSProject CurrentProject { get { if (CurrentItem != null) return CurrentItem.ContainingProject.Object as VSProject; return null; } } #endregion Properties #region Service access /// <summary> /// Provides access to services. /// </summary> /// <param name="serviceType">Service to retrieve.</param> /// <returns>The service object or null.</returns> protected override object GetService(Type serviceType) { object svc = base.GetService(serviceType); // Try the root environment. if (svc == null && CurrentItem == null) return null; VisualStudio.IOleServiceProvider ole = CurrentItem.DTE as VisualStudio.IOleServiceProvider; if (ole != null) return new VisualStudio.ServiceProvider(ole).GetService(serviceType); return null; } #endregion Service access #region Registration and Installation /// <summary> /// Registers the custom tool. /// </summary> public static void Register(Type type) { Guid generator; CustomToolAttribute tool; VersionSupportAttribute[] versions; CategorySupportAttribute[] categories; GetAttributes(type, out generator, out tool, out versions, out categories); foreach (VersionSupportAttribute version in versions) { foreach (CategorySupportAttribute category in categories) { RegisterCustomTool(generator, category.Guid, version.Version, tool.Description, tool.Name, tool.GeneratesDesignTimeCode); } } } /// <summary> /// Unregisters the custom tool. /// </summary> public static void UnRegister(Type type) { Guid generator; CustomToolAttribute tool; VersionSupportAttribute[] versions; CategorySupportAttribute[] categories; GetAttributes(type, out generator, out tool, out versions, out categories); foreach (VersionSupportAttribute version in versions) { foreach (CategorySupportAttribute category in categories) { UnRegisterCustomTool(category.Guid, version.Version, tool.Name); } } } #endregion Registration and Installation #region Helper methods /// <summary> /// Registers the custom tool. /// </summary> private static void RegisterCustomTool(Guid generator, Guid category, Version vsVersion, string description, string toolName, bool generatesDesignTimeCode) { /* * [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio\[vsVersion]\Generators\[category]\[toolName]] * @="[description]" * "CLSID"="[category]" * "GeneratesDesignTimeSource"=[generatesDesignTimeCode] */ string keypath = String.Format(RegistryKey, vsVersion.Major, vsVersion.Minor, category.ToString("B"), toolName); using(RegistryKey key = Registry.LocalMachine.CreateSubKey(keypath)) { key.SetValue("", description); key.SetValue("CLSID", generator.ToString("B")); key.SetValue("GeneratesDesignTimeSource", generatesDesignTimeCode ? 1 : 0); } } /// <summary> /// Unregisters the custom tool. /// </summary> private static void UnRegisterCustomTool(Guid category, Version vsVersion, string toolName) { string key = String.Format(RegistryKey, vsVersion.Major, vsVersion.Minor, category.ToString("B"), toolName); Registry.LocalMachine.DeleteSubKey(key, false); } private static void GetAttributes(Type type, out Guid generator, out CustomToolAttribute tool, out VersionSupportAttribute[] versions, out CategorySupportAttribute[] categories) { object[] attrs; // Retrieve the GUID associated with the generator class. attrs = type.GetCustomAttributes(typeof(GuidAttribute), false); if (attrs.Length == 0) throw new ArgumentException(SR.Tool_AttributeMissing( type, typeof(GuidAttribute))); generator = new Guid(((GuidAttribute)attrs[0]).Value); // Retrieve the custom tool information. attrs = type.GetCustomAttributes(typeof(CustomToolAttribute), false); if (attrs.Length == 0) throw new ArgumentException(SR.Tool_AttributeMissing( type, typeof(CustomToolAttribute))); tool = (CustomToolAttribute) attrs[0]; // Retrieve the VS.NET versions supported. Can be inherited. attrs = type.GetCustomAttributes(typeof(VersionSupportAttribute), true); if (attrs.Length == 0) throw new ArgumentException(SR.Tool_AttributeMissing( type, typeof(VersionSupportAttribute))); versions = (VersionSupportAttribute[]) attrs; // Retrieve the VS.NET generator categories supported. Can be inherited. attrs = type.GetCustomAttributes(typeof(CategorySupportAttribute), true); if (attrs.Length == 0) throw new ArgumentException(SR.Tool_AttributeMissing( type, typeof(CategorySupportAttribute))); categories = (CategorySupportAttribute[]) attrs; } #endregion Helper methods } } |