/// <summary> /// Creates a new instance of the <see cref="BusinessLogicViewModel"/> class. /// </summary> /// <param name="projectDirectory">The full path of the project directory. It cannot be null or empty. This is used as the "appVirtualDir" /// when creating a <see cref="ClientBuildManager"/>.</param> /// <param name="className">The name of the class to generate. It cannot be null.</param> /// <param name="language">The language. Currently must be "C#" or "VB".</param> /// <param name="rootNamespace">The root namespace of the project. This should be null for C# and non-null for VB. Codegen will suppress this namespace.</param> /// <param name="assemblyName">The name of the assembly containing the code we will generate.</param> /// <param name="contextTypes">The list of types to consider.</param> /// <param name="help">The help object used to display help on pressing F1.</param> public BusinessLogicViewModel(string projectDirectory, string className, string language, string rootNamespace, string assemblyName, IEnumerable <Type> contextTypes, IVsHelp help) { if (string.IsNullOrEmpty(projectDirectory)) { throw new ArgumentNullException("projectDirectory"); } if (string.IsNullOrEmpty(className)) { throw new ArgumentNullException("className"); } if (string.IsNullOrEmpty(language)) { throw new ArgumentNullException("language"); } if (contextTypes == null) { throw new ArgumentNullException("contextTypes"); } if (string.IsNullOrEmpty(assemblyName)) { throw new ArgumentNullException("assemblyName"); } this._projectDirectory = projectDirectory; this._className = className; this._language = language; this._rootNamespace = rootNamespace; this._assemblyName = assemblyName; this._contextTypes = new List <Type>(contextTypes); this._help = help; }
/// <summary> /// Creates a new instance of the <see cref="BusinessLogicViewModel"/> class. /// </summary> /// <param name="projectDirectory">The full path of the project directory. It cannot be null or empty. This is used as the "appVirtualDir" /// when creating a <see cref="ClientBuildManager"/>.</param> /// <param name="className">The name of the class to generate. It cannot be null.</param> /// <param name="language">The language. Currently must be "C#" or "VB".</param> /// <param name="rootNamespace">The root namespace of the project. This should be null for C# and non-null for VB. Codegen will suppress this namespace.</param> /// <param name="assemblyName">The name of the assembly containing the code we will generate.</param> /// <param name="contextTypes">The list of types to consider.</param> /// <param name="help">The help object used to display help on pressing F1.</param> public BusinessLogicViewModel(string projectDirectory, string className, string language, string rootNamespace, string assemblyName, IEnumerable<Type> contextTypes, IVsHelp help) { if (string.IsNullOrEmpty(projectDirectory)) { throw new ArgumentNullException("projectDirectory"); } if (string.IsNullOrEmpty(className)) { throw new ArgumentNullException("className"); } if (string.IsNullOrEmpty(language)) { throw new ArgumentNullException("language"); } if (contextTypes == null) { throw new ArgumentNullException("contextTypes"); } if (string.IsNullOrEmpty(assemblyName)) { throw new ArgumentNullException("assemblyName"); } this._projectDirectory = projectDirectory; this._className = className; this._language = language; this._rootNamespace = rootNamespace; this._assemblyName = assemblyName; this._contextTypes = new List<Type>(contextTypes); this._help = help; }
/// <include file='doc\VsTaskItem.uex' path='docs/doc[@for="VsTaskItem.NavigateToHelp"]/*' /> /// <devdoc> /// <para>[To be supplied.]</para> /// </devdoc> public int NavigateToHelp() { if (helpKeyword.Length != 0 && serviceProvider != null) { IVsHelp help = (IVsHelp)serviceProvider.GetService(typeof(IVsHelp)); if (help != null) { help.DisplayTopicFromF1Keyword(helpKeyword); return(NativeMethods.S_OK); } } return(NativeMethods.E_NOTIMPL); }
/// <include file='doc\HelpService.uex' path='docs/doc[@for="HelpService.ShowHelpFromUrl"]/*' /> /// <devdoc> /// Shows the given help topic. This should contain a Url to the help /// topic. The topic will be displayed in /// the environment's integrated help system. /// </devdoc> public virtual void ShowHelpFromUrl(string helpUrl) { IVsHelp help = (IVsHelp)provider.GetService(typeof(IVsHelp)); if (help != null) { try { help.DisplayTopicFromURL(helpUrl); } catch (Exception) { // IVsHelp causes a ComException to be thrown if the help // topic isn't found. } } }
/// <include file='doc\StyleBuilderSite.uex' path='docs/doc[@for="StyleBuilderSite.ShowHelp"]/*' /> /// <devdoc> /// </devdoc> public void ShowHelp(string helpKeyword) { Debug.Assert(helpKeyword != null, "null helpKeyword!"); if (site != null) { try { IVsHelp help = (IVsHelp)QueryService(typeof(IVsHelp)); if (help != null) { help.DisplayTopicFromF1Keyword(helpKeyword); } } catch (Exception e) { Debug.Fail(e.ToString()); } } }
public void RunStarted(object automationObject, Dictionary <string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams) { // This instance may be reused -- reinitialize each time this._project = null; this._dte2 = automationObject as DTE2; if (this._dte2 == null) { this.TerminateWizard(Resources.WizardError_No_DTE); } // Get active project. Throws if null. Project project = this.ActiveProject; ITypeDiscoveryService typeDiscoveryService = this.GetTypeDiscoveryService(project); if (typeDiscoveryService == null) { this.TerminateWizard(Resources.BusinessLogicClass_Error_No_TypeDiscoveryService); } // Check if the project has a reference to the assembly that has DbDomainService, supporting DbContext. VSProject vsproj = project.Object as VSProject; bool allowDbContext = false; if (vsproj != null) { allowDbContext = vsproj.References.Cast <Reference>().Any(r => String.Equals(r.Name, BusinessLogicClassConstants.DbDomainServiceAssemblyShortName)); } // Get the list of available ObjectContexts, DataContexts, and optionally DbContexts. bool foundDbContext = false; IEnumerable <Type> candidates = this.GetCandidateTypes(typeDiscoveryService, allowDbContext, out foundDbContext); // Ensure the user entered a non-null file name string fileName = replacementsDictionary["$rootname$"]; fileName = fileName.Trim(); if (string.IsNullOrEmpty(fileName)) { this.TerminateWizard(Resources.WizardError_Empty_Filename); } // Class name is file name minus extension. Validate not empty. string className = Path.GetFileNameWithoutExtension(fileName); if (string.IsNullOrEmpty(className)) { this.TerminateWizard(Resources.WizardError_Empty_Filename); } // We infer language from extension string extension = Path.GetExtension(fileName); bool isVb = extension.EndsWith("vb", StringComparison.OrdinalIgnoreCase); string language = isVb ? "VB" : "C#"; Property rootNamespaceProperty = project.Properties.Item("RootNamespace"); string namespaceName = rootNamespaceProperty == null ? null : (string)(rootNamespaceProperty.Value); if (string.IsNullOrEmpty(namespaceName)) { this.TerminateWizard(Resources.BusinessLogicClass_Error_No_RootNamespace); } // Extract VB root namespace for code gen. // If non-empty, it means we want to avoid generating namespaces that begin with it. string rootNamespace = isVb ? namespaceName : string.Empty; // Get the name of the assembly produced by the current project Property assemblyNameProperty = project.Properties.Item("AssemblyName"); string assemblyName = assemblyNameProperty == null ? null : (string)(assemblyNameProperty.Value); if (string.IsNullOrEmpty(assemblyName)) { this.TerminateWizard(Resources.BusinessLogicClass_Error_No_AssemblyName); } // We extract all the context types from the TypeDiscovery service and will pass // those to another AppDomain for analysis IEnumerable <Type> candidateTypes = candidates.Where(t => CodeGenUtilities.IsValidGenericTypeParam(t)); // We need the project path for the ClientBuildManager source folder string projectPath = project.FullName; if (string.IsNullOrEmpty(projectPath)) { this.TerminateWizard(Resources.BusinessLogicClass_No_Project_Path); } string projectDirectory = Path.GetDirectoryName(projectPath); try { IVsHelp help = this.GetService(typeof(IVsHelp)) as IVsHelp; // Strategy: we instantiate the contexts in another AppDomain so they can load the assembly outside of the current // Visual Studio root AppDomain. The main reason for this is the ability to reopen the DSWizard onto modified // client assemblies and see modified types. If they were loaded into the root AppDomain, we would not be able to // reload. The BusinessLogicViewModel is IDisposable and controls the other AppDomain's lifetime. using (BusinessLogicViewModel businessLogicViewModel = new BusinessLogicViewModel(projectDirectory, className, language, rootNamespace, assemblyName, candidateTypes, help)) { businessLogicViewModel.ShowDbContextWarning = false; // foundDbContext; //Removed by CDB //TODO: remove commented out section // Intercept exceptions to report to VS UI. businessLogicViewModel.ExceptionHandler = delegate(Exception ex) { this.ShowError(ex.Message); throw ex; }; // Invoke the wizard UI now this._dialog = new BusinessLogicClassDialog(); this._dialog.Model = businessLogicViewModel; IVsUIShell uiShell = this.GetService(typeof(IVsUIShell)) as IVsUIShell; IntPtr dialogOwnerHwnd = default(IntPtr); int result = uiShell.GetDialogOwnerHwnd(out dialogOwnerHwnd); if (result == 0 && dialogOwnerHwnd != default(IntPtr)) { WindowInteropHelper windowHelper = new WindowInteropHelper(this._dialog); windowHelper.Owner = dialogOwnerHwnd; } this._dialog.ShowInTaskbar = false; this._dialog.ShowDialog(); bool success = this._dialog.DialogResult.HasValue && this._dialog.DialogResult.Value; this._dialog.Model = null; // If user cancels dialog, cancel wizard if (!success) { throw new WizardCancelledException(); } // Capture some model state to we can dispose the contexts and models ContextViewModel currentContext = businessLogicViewModel.CurrentContextViewModel; this._isClientAccessEnabled = currentContext != null && currentContext.IsClientAccessEnabled; this._isODataEndpointEnabled = currentContext != null && currentContext.IsODataEndpointEnabled; // Compute a namespace that includes folder names namespaceName = this.ComputeNamespace(); // User said OK -- so let's generate the code GeneratedCode generatedCode = businessLogicViewModel.GenerateBusinessLogicClass(namespaceName); replacementsDictionary.Add("$generatedcode$", generatedCode.SourceCode); this.AddReferences(generatedCode.References); this._businessLogicCode = generatedCode; // If user elected metadata classes, do that into a separate file if (businessLogicViewModel.IsMetadataClassGenerationRequested) { // The null namespace asks to generate into the entity types own namespaces GeneratedCode generatedMetadataCode = businessLogicViewModel.GenerateMetadataClasses(null /* optionalSuffix */); replacementsDictionary.Add("$generatedmetadatacode$", generatedMetadataCode.SourceCode); this.AddReferences(generatedMetadataCode.References); this._generateMetadataFile = generatedMetadataCode.SourceCode.Length > 0; this._metadataCode = generatedMetadataCode; } else { this._generateMetadataFile = false; } } } finally { this._dialog = null; } }