/// <summary> /// Performs the validation of the item passed as target /// Returns true if the reference is allowed to be executed in the target /// that is if the target is a web project and C# project /// </summary> /// <param name="target">The target.</param> /// <returns> /// <c>true</c> if [is web C sharp project] [the specified target]; otherwise, <c>false</c>. /// </returns> public static bool IsWebCSharpProject(object target) { Project project = null; if (target is Project) { project = (Project)target; } else if (target is ProjectItem) { project = ((ProjectItem)target).ContainingProject; } if (project != null && DteHelper.IsWebProject(project) && project.Properties != null) { try { Property property = project.Properties.Item(CurrentWebsiteLanguagePropertyItem); return(property.Value != null && property.Value.ToString().Equals(CurrentWebsiteLanguagePropertyValue, StringComparison.InvariantCultureIgnoreCase)); } catch (Exception exception) { Trace.TraceError(exception.ToString()); // Some Project implementations throws this excpetion (i.e.: Analysis services project) // or the CurrentWebsiteLanguagePropertyItem property does not exists. return(false); } } return(false); }
/// <summary> /// Adds the reference to the project /// </summary> public override void Execute() { if (DteHelper.IsWebProject(referringProject)) { // This is a web project VSWebSite webProject = referringProject.Object as VSWebSite; if (webProject != null) { if (File.Exists(this.assemblyFilePath)) { webProject.References.AddFromFile(this.assemblyFilePath); } else { webProject.References.AddFromGAC(this.assemblyFilePath); } } } else { // This is a standard project VSProject vsProject = referringProject.Object as VSProject; if (vsProject != null) { vsProject.References.Add(this.assemblyFilePath); } } }
/// <summary> /// Retrieves the code file extension for the project. /// </summary> /// <param name="project">The project.</param> /// <returns></returns> public static string GetDefaultExtension(Project project) { if (DteHelper.IsWebProject(project)) { return(IsWebCSharpProject(project) ? ".cs" : ".vb"); } return(DteHelper.GetDefaultExtension(project)); }
/// <summary> /// Returns the enumerator /// </summary> /// <returns></returns> public IEnumerator <Project> GetEnumerator() { if (DteHelper.IsWebProject(project)) { return(IterateWebProjectReferences((VSWebSite)project.Object)); } else { return(IterateProjectReferences((VSProject)project.Object)); } }
/// <summary> /// Performs the validation of the item passed as target /// Returns true if the reference is allowed to be executed in the target /// that is if the target is a <see cref="VSWebProjectItem"/> file. /// </summary> /// <param name="target"></param> /// <returns></returns> public override bool OnIsEnabledFor(object target) { ProjectItem item = target as ProjectItem; if (item == null || (item.ContainingProject != null && !DteHelper.IsWebProject(item.ContainingProject))) { return(false); } return(base.OnIsEnabledFor(target)); }
/// <summary> /// Retrieves the connection strings section from the given project. /// </summary> public static ConnectionStringsSection GetSection(EnvDTE.Project project) { if (project == null) { return(null); } EnvDTE.ProjectItem configItem = null; //string configName; //if (DteHelper.IsWebProject(project)) //{ // configName = "Web.config"; //} //else //{ // configName = "App.config"; //} //configItem = DteHelper.FindItemByName(project.ProjectItems, configName, false); //if (configItem == null) //{ // configItem = DteHelper.FindItemByName(project.ProjectItems, configName.ToLowerInvariant(), false); //} if (DteHelper.IsWebProject(project)) { try { configItem = FindItemByName(project.ProjectItems, "web.config", true); } catch { //With Web projects without any subfolder this method throws an exception configItem = FindItemByName(project.ProjectItems, "web.config", false); } } else { configItem = FindItemByName(project.ProjectItems, "app.config", true); } if (configItem == null) { return(null); } string configFile = configItem.get_FileNames(1); return(GetSection(configFile)); }
/// <summary> /// Finds the type of the code element from the specified project. /// </summary> /// <param name="project">The project.</param> /// <param name="typeName">Name of the type.</param> /// <param name="elementKind">Kind of the element.</param> /// <returns> /// The <see cref="CodeElement"/> found or null if no matches. /// </returns> public static CodeElement FindCodeElementFromType(Project project, string typeName, vsCMElement elementKind) { if (project == null || string.IsNullOrEmpty(typeName)) { return(null); } // try to find it in the selected project CodeElement result = FindCodeElementByFullName(project, typeName, elementKind); if (result == null) { // navigate through the project references and look into each project if (!DteHelper.IsWebProject(project)) { VSProject vsProject = project.Object as VSProject; foreach (Reference reference in vsProject.References) { result = FindCodeElementFromType(reference.SourceProject, typeName, elementKind); if (result != null && result.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject) { break; } } } else { VSWebSite webProject = project.Object as VSWebSite; if (webProject != null) { foreach (AssemblyReference reference in webProject.References) { Project sourceProject = GetSourceProject(reference); result = FindCodeElementFromType(sourceProject, typeName, elementKind); if (result != null && result.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject) { break; } } } } } return(result); }
private void AddTypes(ITypeDiscoveryService discovery, Project dteProject) { bool isWebProject = DteHelper.IsWebProject(dteProject); ICollection types = null; try { types = discovery.GetTypes(typeof(object), false); } catch { types = new ArrayList(); } foreach (Type type in types) { if (!availableTypes.ContainsKey(type.FullName)) { // Web projects do not have if (!isWebProject && type.Assembly.GetName().Name != (string)dteProject.Properties.Item("AssemblyName").Value) { availableTypes.Add(type.FullName, type); } else { availableTypes.Add(type.FullName, new ProjectType(type)); } } } if (types.Count > 0 && useProjectItemWrapper) { foreach (ProjectItem projectItem in dteProject.ProjectItems) { Type type = BuilderProjectItemWrapper.BuildWrapper(projectItem); if (type != null && !availableTypes.ContainsKey(type.FullName)) { availableTypes.Add(type.FullName, type); } } } }
/// <summary> /// Executes the action /// </summary> public override void Execute() { bool gaced = (_assembliesPath == null); List <string> assemblies = BuildAssembliesPathList(gaced); if (DteHelper.IsWebProject(_referringProject)) { // This is a web project VSWebSite webProject = _referringProject.Object as VSWebSite; if (webProject != null) { foreach (string assemblyFilePath in assemblies) { if (gaced) { webProject.References.AddFromGAC(assemblyFilePath); } else { webProject.References.AddFromFile(assemblyFilePath); } } } } else { // standard project VSProject vsProject = _referringProject.Object as VSProject; if (vsProject != null) { foreach (string assemblyFilePath in assemblies) { vsProject.References.Add(assemblyFilePath); } } } }
private static bool IsWebSiteOrWebAppProject(Project project) { return(DteHelper.IsWebProject(project) || IsWebApplicationProject(project)); }
/// <summary> /// Edits the specified object's value using the editor style indicated by the <see cref="M:System.Drawing.Design.UITypeEditor.GetEditStyle"></see> method. /// </summary> /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"></see> that can be used to gain additional context information.</param> /// <param name="provider">An <see cref="T:System.IServiceProvider"></see> that this editor can use to obtain services.</param> /// <param name="value">The object to edit.</param> /// <returns> /// The new value of the object. If the value of the object has not changed, this should return the same object it was passed. /// </returns> public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value) { DynamicTypeService typeService = (DynamicTypeService)provider.GetService(typeof(DynamicTypeService)); IVsHierarchy hier = DteHelper.GetCurrentSelection(provider); ITypeDiscoveryService typeDiscovery = typeService.GetTypeDiscoveryService(hier); Project project = ToDteProject(hier); if (DteHelper.IsWebProject(project)) { VSWebSite vsProject = (VSWebSite)project.Object; List <string> assemblies = new List <string>(); foreach (AssemblyReference reference in vsProject.References) { if (!string.IsNullOrEmpty(reference.FullPath)) { assemblies.Add(reference.FullPath); } } MethodInfo setAsssembliesMethod = typeDiscovery.GetType().GetMethod(SetAssembliesMethodName, BindingFlags.NonPublic | BindingFlags.Instance); setAsssembliesMethod.Invoke(typeDiscovery, new object[] { assemblies.ToArray() }); } if (typeDiscovery != null) { List <string> assembliesAdded = new List <string>(); List <Assembly> assemblies = new List <Assembly>(); List <Type> types = new List <Type>(); foreach (Type type in typeDiscovery.GetTypes(typeof(object), false)) { if (ShouldInclude(type)) { if (!assembliesAdded.Contains(type.Assembly.FullName)) { assembliesAdded.Add(type.Assembly.FullName); assemblies.Add(type.Assembly); } types.Add(type); } } IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); ClassBrowserEditorForm form = new ClassBrowserEditorForm(assemblies, types); DialogResult result; if (svc != null) { result = svc.ShowDialog(form); } else { result = form.ShowDialog(); } if (result == DialogResult.OK) { return(form.TypeFullName); } else { return(value); } } else { return(null); } }
/// <summary> /// Adds the project reference to the <see cref="ReferencedProject"/> /// </summary> public override void Execute() { if (this.referencedProject != null) { //Add project reference if (this.ReferencedProject.UniqueName.Equals(this.ReferringProject.UniqueName, StringComparison.InvariantCultureIgnoreCase)) { // Do nothing. return; } // If reference already exists, nothing happens. // If referringProject is a VSProject VSProject vsProject = referringProject.Object as VSProject; if (vsProject != null) { if (DteHelper.IsWebProject(referencedProject)) { VsWebSite.VSWebSite vsWebSite = (VsWebSite.VSWebSite)(referencedProject.Object); foreach (VsWebSite.WebService webService in vsWebSite.WebServices) { vsProject.AddWebReference(webService.URL); } } else { // No error handling needed here. See documentation for AddProject. vsProject.References.AddProject(referencedProject); } } else { // If the Project recived is not a VsProject // Try it with a webProject VSWebSite webProject = referringProject.Object as VSWebSite; if (webProject != null) { if (DteHelper.IsWebProject(referencedProject)) { VSWebSite vsWebSite = (VSWebSite)(referencedProject.Object); foreach (VsWebSite.WebService webService in vsWebSite.WebServices) { webProject.WebReferences.Add(webService.URL, webService.ClassName); } } else { // Check if the reference already exists in the WebProject if (!IsAlreadyReferenced(webProject, referencedProject)) { webProject.References.AddFromProject(referencedProject); } } } } } else if (this.referencedAssembly != string.Empty) { //Add Assembly reference VSProject vsProject = referringProject.Object as VSProject; string assemblyName = Path.GetFileNameWithoutExtension(this.referencedAssembly); if (vsProject != null) { if (!IsAlreadyReferenced(vsProject, assemblyName)) { vsProject.References.Add(this.referencedAssembly); } } else { VSWebSite webProject = referringProject.Object as VSWebSite; if (webProject != null) { if (!IsAlreadyReferenced(webProject, assemblyName)) { webProject.References.AddFromFile(this.referencedAssembly); } } } } }