From: Daniel C. \(kzu\) <dca...@us...> - 2005-10-31 09:31:36
|
Update of /cvsroot/mvp-xml/Design/v2/src/CustomTools In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29101/v2/src/CustomTools Added Files: CategorySupportAttribute.cs CustomTool.cs CustomToolAttribute.cs VersionSupportAttribute.cs Log Message: Finished port to v2 of XGen and XsdGen. Removed XmlValidate as the new XML editor has built-in support for validation. --- NEW FILE: CustomTool.cs --- 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; using System.Globalization; 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}"; /// <summary> /// {0}=Custom Tool Name /// {1}=Tool version /// {2}=.NET Runtime version /// </summary> const string TemplateAutogenerated = @"//------------------------------------------------------------------------------ // <autogenerated> // This code was generated by the {0} tool. // Tool Version: {1} // Runtime Version: {2} // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // </autogenerated> //------------------------------------------------------------------------------ "; #endregion Constants protected override sealed byte[] GenerateCode(string inputFileName, string inputFileContent) { try { string code = OnGenerateCode(inputFileContent, inputFileContent); return ConvertStringToBytes(code); } catch (Exception ex) { if (ex is TargetInvocationException) { ex = ex.InnerException; } return ConvertStringToBytes(String.Format( CultureInfo.CurrentCulture, Properties.Resources.CustomTool_GeneralError, ex)); } } private byte[] ConvertStringToBytes(string code) { return System.Text.Encoding.UTF8.GetBytes(code); } protected abstract string OnGenerateCode(string inputFileName, string inputFileContent); public static string GetToolGeneratedCodeWarning(Type customToolType) { CustomToolAttribute attribute = (CustomToolAttribute)Attribute.GetCustomAttribute( customToolType, typeof(CustomToolAttribute), true); if (attribute == null) { throw new ArgumentException(String.Format( CultureInfo.CurrentCulture, Properties.Resources.CustomTool_ToolRequiredAttributeMissing, customToolType, typeof(CustomToolAttribute))); } return String.Format(TemplateAutogenerated, attribute.Name, ThisAssembly.Version, Environment.Version); } private static object CustomToolAttribute(CustomToolAttribute customToolAttribute) { throw new Exception("The method or operation is not implemented."); } #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(String.Format( CultureInfo.CurrentCulture, Properties.Resources.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(String.Format( CultureInfo.CurrentCulture, Properties.Resources.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(String.Format( CultureInfo.CurrentCulture, Properties.Resources.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(String.Format( CultureInfo.CurrentCulture, Properties.Resources.Tool_AttributeMissing, type, typeof(CategorySupportAttribute))); categories = (CategorySupportAttribute[]) attrs; } #endregion Helper methods } } --- NEW FILE: VersionSupportAttribute.cs --- using System; namespace Mvp.Xml.Design.CustomTools { /// <summary> /// Determines which versions of VS.NET are supported by the custom tool. /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class VersionSupportAttribute : Attribute { Version _version; /// <summary> /// Initializes the attribute. /// </summary> /// <param name="version">Version supported by the tool.</param> public VersionSupportAttribute(string version) { _version = new Version(version); } /// <summary> /// Version supported by the tool. /// </summary> public Version Version { get { return _version; } } } } --- NEW FILE: CustomToolAttribute.cs --- using System; namespace Mvp.Xml.Design.CustomTools { /// <summary> /// Specifies custom tool registration information. /// </summary> [AttributeUsage(AttributeTargets.Class)] public class CustomToolAttribute : Attribute { string _name; string _description; bool _code; /// <summary> /// Assigns custom tool information to the class. /// </summary> /// <param name="name">Name of the custom tool.</param> /// <param name="description">A description of the tool.</param> /// <param name="generatesDesignTimeCode"> /// If <see langword="true" />, the IDE will try to compile on the fly the /// dependent the file associated with this tool, and make it available /// through intellisense to the rest of the project. /// </param> public CustomToolAttribute(string name, string description, bool generatesDesignTimeCode) { _name = name; _description = description; _code = generatesDesignTimeCode; } /// <summary> /// Name of the custom tool. /// </summary> public string Name { get { return _name; } } /// <summary> /// Friendly description of the tool. /// </summary> public string Description { get { return _description; } } /// <summary> /// Specifies whether the tool generates design time code to compile on the fly. /// </summary> public bool GeneratesDesignTimeCode { get { return _code; } } } } --- NEW FILE: CategorySupportAttribute.cs --- using System; namespace Mvp.Xml.Design.CustomTools { /// <summary> /// Determines which VS.NET generator categories are supported by the custom tool. /// This class also contains constants for C# and VB.NET category guids. /// </summary> [AttributeUsage(AttributeTargets.Class, AllowMultiple = true)] public class CategorySupportAttribute : Attribute { Guid _category; /// <summary> /// VS Generator Category for C# Language. /// </summary> public const string CSharpCategory = "{FAE04EC1-301F-11D3-BF4B-00C04F79EFBC}"; /// <summary> /// VS Generator Category for VB Language. /// </summary> public const string VBCategory = "{164B10B9-B200-11D0-8C61-00A0C91E29D5}"; /// <summary> /// Initializes the attribute. /// </summary> /// <param name="categoryGuid"> /// Either <see cref="CSharpCategory"/> or <see cref="VBCategory"/>. /// </param> public CategorySupportAttribute(string categoryGuid) { _category = new Guid(categoryGuid); } /// <summary> /// The identifier of the supported category. /// </summary> public Guid Guid { get { return _category; } } } } |