private static EnvDTE.Properties TryGetDtePropertiesFromHierarchy(IVsHierarchy hierarchy) { try { EnvDTE.Project project = TryGetExtensibilityObject(hierarchy) as EnvDTE.Project; if (project == null) { return(null); } EnvDTE.ConfigurationManager configurationManager = project.ConfigurationManager; if (configurationManager == null) { return(null); } EnvDTE.Configuration activeConfiguration = configurationManager.ActiveConfiguration; if (activeConfiguration == null) { return(null); } return(activeConfiguration.Properties); } catch (Exception ex) { if (ErrorHandler.IsCriticalException(ex)) { throw; } return(null); } }
/// <summary> /// We find the collection of configurations (Release, Debug etc) /// and parse each one. /// </summary> private void parseConfigurations() { EnvDTE.ConfigurationManager configurationManager = Utils.call(() => (m_dteProject.ConfigurationManager)); int numConfigurations = Utils.call(() => (configurationManager.Count)); for (int i = 1; i <= numConfigurations; ++i) { EnvDTE.Configuration dteConfiguration = Utils.call(() => (configurationManager.Item(i, "") as EnvDTE.Configuration)); parseConfiguration(dteConfiguration); } }
EnvDTE.Property GetPropertySave(string name, bool onConfig) { EnvDTE.Property property = null; EnvDTE.Properties props = null; ThreadHelper.ThrowIfNotOnUIThread(); try { if (onConfig) { EnvDTE.ConfigurationManager confManager = this.ReferencedProjectObject.ConfigurationManager; if (null != confManager) { // Get the active configuration. EnvDTE.Configuration config = confManager.ActiveConfiguration; if (null != config) { // Get the output path for the current configuration. var obj = config.Properties; EnvDTE.Properties props1 = obj; if (null != props1) { property = props1.Item(name); } } } } else { // Most project types should implement the OutputPath property on their // configuration-dependent Properties object above. But if it wasn't found // there, check the project node Properties. props = this.ReferencedProjectObject.Properties; if (null != props) { property = props.Item(name); } } } catch (COMException) { } catch (ArgumentException) { } return(property); }
private string GetReferencedProjectOutputPath() { // Make sure that the referenced project implements the automation object. if (null == this.ReferencedProject) { return(null); } // Get the configuration manager from the project. EnvDTE.ConfigurationManager confManager = this.ReferencedProject.ConfigurationManager; if (null == confManager) { return(null); } // Get the active configuration. EnvDTE.Configuration config = null; try { config = confManager.ActiveConfiguration; } catch (ArgumentException) { // 4951: exeception happens sometimes when ToolBox queries references on worker thread // (apparently hitting a race or bad state of referenced project's ConfigurationManager) return(null); } if (null == config) { return(null); } // Get the output path for the current configuration. string outputPath = null; try { EnvDTE.Property outputPathProperty = config.Properties.Item("OutputPath"); if (null == outputPathProperty) { return(null); } outputPath = outputPathProperty.Value.ToString(); } catch (System.Runtime.InteropServices.COMException) { // just catch the exception and return null, which means the user will see an empty 'FullPath' field of a ProjectReference property. return(null); } // Ususally the output path is relative to the project path, but it is possible // to set it as an absolute path. If it is not absolute, then evaluate its value // based on the project directory. if (!System.IO.Path.IsPathRooted(outputPath)) { string projectDir = System.IO.Path.GetDirectoryName(referencedProjectFullPath); outputPath = System.IO.Path.Combine(projectDir, outputPath); } // Now get the name of the assembly from the project. // Some project system throw if the property does not exist. We expect an ArgumentException. string assemblyName = null; try { assemblyName = this.ReferencedProject.Properties.Item("OutputFileName").Value.ToString(); } catch (ArgumentException) { } catch (NullReferenceException) { } if (null == assemblyName) { try { var group = config.OutputGroups.Item("Built"); var files = (object[])group.FileNames; if (files.Length > 0) { assemblyName = (string)files[0]; } } catch (InvalidCastException) { return(null); } catch (ArgumentException) { return(null); } catch (NullReferenceException) { return(null); } } // build the full path adding the name of the assembly to the output path. outputPath = System.IO.Path.Combine(outputPath, assemblyName); return(outputPath); }
public void Dispose() { configurationManager = null; }
/// [disconnect from VS] public ProjectInfo ReadSettingsOfAllProjects() { ProjectInfo projectInfo = new ProjectInfo(); WriteLine(3, "ReadSettingsOfAllProjects-Begin"); try { if (dte == null) { WriteLine(1, "dte is null (checking for projects is not possible)"); return(projectInfo); } if (dte.Solution == null) { WriteLine(1, "dte.Solution is null (checking for projects is not possible)"); return(projectInfo); } if (dte.Solution.SolutionBuild == null) { WriteLine(1, "dte.Solution.SolutionBuild is null (checking for projects is not possible)"); return(projectInfo); } if (dte.Solution.SolutionBuild.ActiveConfiguration == null) { WriteLine(1, "dte.Solution.SolutionBuild.ActiveConfiguration is null (checking for projects is not possible)"); return(projectInfo); } projectInfo.solutionFullPath = dte.Solution.FileName; /// [get config name] //Get name of config (e.g. "Debug", "Release" string configName = dte.Solution.SolutionBuild.ActiveConfiguration.Name; /// [get config name] string msg = ""; /// [get name startup project] EnvDTE80.SolutionBuild2 sb = (EnvDTE80.SolutionBuild2)dte.Solution.SolutionBuild; if (sb == null) { WriteLine(1, "SolutionBuild is null (checking for projects is not possible)"); return(projectInfo); } // for further processing extract the name without path and extension // e.g. nameStartupProject = "TestRunner" // (extraction code not shown here) /// [get name startup project] // To simple solution which does not work for startup projects within a filter //EnvDTE.Project startupProj = dte.Solution.Item(msg); /// [get startup project] // Perform recursive search for the startup project through all solution filters: EnvDTE.Project[] projects = GetAllProjects(false); /// [get startup project] //iterate through main projects foreach (var dteProject in projects) { var project = new Project(); project.DTEProject = dteProject; project.ProjectName = dteProject.Name; var projectFiles = new List <FileInfo>(); foreach (EnvDTE.ProjectItem fileItem in GetAllProjectItemsFromKind(project.DTEProject, EnvDTE.Constants.vsProjectItemKindPhysicalFile)) { for (short i = 0; i < fileItem.FileCount; i++) { var fileInfo = new FileInfo(fileItem.FileNames[i]); projectFiles.Add(fileInfo); } } project.ProjectFiles = projectFiles.Distinct().ToArray(); try { project.SourceDirPath = System.IO.Path.GetDirectoryName(dteProject.FullName); } catch (Exception ex) { WriteLine(3, "ReadSettingsOfAllProjects EXCEPTION: " + ex.ToString()); continue; } EnvDTE.ConfigurationManager cm = dteProject.ConfigurationManager; if (cm == null) { WriteLine(1, "No ConfigurationManager found"); WriteLine(3, "ReadSettingsOfAllProjects: no ConfigurationManager found"); continue; } if (cm.ActiveConfiguration == null) { WriteLine(1, "No ActiveConfiguration found"); WriteLine(3, "ReadSettingsOfAllProjects: no ActiveConfiguration found"); continue; } msg = "Platform=" + cm.ActiveConfiguration.PlatformName; WriteLine(2, msg); EnvDTE.Properties props = cm.ActiveConfiguration.Properties; if (props != null) { WriteLine(2, "Now iterating over ActiveConfiguration.Properties..."); // Scan properties of ActiveConfiguration to be used for future extended requests msg = "ReadSettingsOfStartupProject: ActiveConfiguration.Properties"; foreach (EnvDTE.Property p in props) { msg += " " + p.Name; } WriteLine(2, msg); } /// [get exe path] // Get full path of executable depending on found // startup project and configuration (Debug/Release) Microsoft.VisualStudio.VCProjectEngine.VCProject vcProj = (Microsoft.VisualStudio.VCProjectEngine.VCProject)dteProject.Object; Microsoft.VisualStudio.VCProjectEngine.IVCCollection configs = (Microsoft.VisualStudio.VCProjectEngine.IVCCollection)vcProj.Configurations; Microsoft.VisualStudio.VCProjectEngine.VCConfiguration config = FindConfig(configs, configName); if (config == null) { WriteLine(1, "Config " + configName + " not found"); continue; } msg = "PrimaryOutput (FullExePath)=" + config.PrimaryOutput; WriteLine(2, msg); /// [get exe path] project.FullExePath = config.PrimaryOutput; string delimiter = "/\\"; int posPathEnd = project.FullExePath.LastIndexOfAny(delimiter.ToCharArray()); if (posPathEnd > 0) { project.TargetDirPath = project.FullExePath.Substring(0, posPathEnd); } msg = "ReadSettingsOfAllProjects: OutputPath=" + project.TargetDirPath; WriteLine(2, msg); // Scan properties to be used for future extended requests msg = "ReadSettingsOfAllProjects: startupProj.Properties"; foreach (EnvDTE.Property p in dteProject.Properties) { msg += " " + p.Name; } WriteLine(3, msg); projectInfo.AddProject(project); } projectInfo.config = configName; WriteLine(3, "ReadSettingsOfAllProjects-End"); } catch (Exception ex) { WriteLine(1, "ReadSettingsOfAllProjects-End: EXCEPTION: " + ex.ToString()); } return(projectInfo); }