public bool FindStartupProject(out EnvDTE.Project startupProject)
        {
            startupProject = null;

            string prjName = StartupProjectUniqueName();

            if (prjName != null)
            {
                try
                {
                    var project = this.appObject?.Solution.Item(prjName);
                    if (ProjectArguments.IsSupportedProject(project))
                    {
                        startupProject = project;
                    }
                }
                catch
                {
                    // If we couldn't find it in the solution directly, check in the nested projects
                    startupProject = FindAllProjects().FirstOrDefault(p => p.UniqueName == prjName);
                }
                return(startupProject != null);
            }

            return(false);
        }
        private void UpdateConfigurationForProject(IVsHierarchy project)
        {
            if (project == null)
            {
                return;
            }
            IEnumerable <CmdArgument> checkedArgs = ToolWindowViewModel.TreeViewModel.Projects.GetValueOrDefault(project.GetGuid())?.CheckedArguments;

            if (checkedArgs == null)
            {
                return;
            }

            IEnumerable <string> enabledEntries;

            if (IsMacroEvaluationEnabled)
            {
                enabledEntries = checkedArgs.Select(
                    e => msBuildPropertyRegex.Replace(e.Value,
                                                      match => vsHelper.GetMSBuildPropertyValue(project, match.Groups["propertyName"].Value) ?? match.Value));
            }
            else
            {
                enabledEntries = checkedArgs.Select(e => e.Value);
            }
            string prjCmdArgs = string.Join(" ", enabledEntries);

            ProjectArguments.SetArguments(project, prjCmdArgs);
            Logger.Info($"Updated Configuration for Project: {project.GetName()}");
        }
 private void SolutionEvents_ProjectRemoved(Project project)
 {
     if (ProjectArguments.IsSupportedProject(project))
     {
         ProjectRemoved?.Invoke(this, project);
     }
 }
        public void GetProjects(EnvDTE.Project project, ref List <EnvDTE.Project> allProjects)
        {
            // Make sure we have a valid list
            if (allProjects == null)
            {
                allProjects = new List <EnvDTE.Project>();
            }

            // We determine if this is an actual project by looking if it has a ConfigurationManager
            // This could be wrong for some types of project, but it works for our needs
            if (ProjectArguments.IsSupportedProject(project))
            {
                allProjects.Add(project);
            }
            else if (project.ProjectItems != null)
            {
                foreach (EnvDTE.ProjectItem item in project.ProjectItems)
                {
                    if (item.SubProject != null && item.SubProject != project)
                    {
                        GetProjects(item.SubProject, ref allProjects);
                    }
                }
            }
        }
        int IVsSolutionEvents.OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
        {
            var project = ProjectForHierarchy(pHierarchy);

            if (!ProjectArguments.IsSupportedProject(project))
            {
                return(LogIgnoringUnsupportedProjectType());
            }

            Guid   projectGuid = pHierarchy.GetGuid();
            string projectPath = project.FullName;
            string projectName = project.UniqueName;
            bool   isLoaded    = pHierarchy.IsLoaded();

            bool isLoadProcess = ProjectStateMap.TryGetValue(projectGuid, out var state) && state.IsLoaded;

            ProjectStateMap[projectGuid] = new ProjectState {
                FilePath = projectPath, UniqueName = projectName, IsLoaded = isLoaded
            };

            ProjectAfterOpen?.Invoke(this, new ProjectAfterOpenEventArgs {
                Project = project, IsLoadProcess = isLoadProcess, IsSolutionOpenProcess = fAdded == 0
            });

            return(S_OK);
        }
 private void SolutionEvents_ProjectRenamed(Project project, string oldName)
 {
     if (ProjectArguments.IsSupportedProject(project))
     {
         ProjectRenamed?.Invoke(this, new ProjectRenamedEventArgs {
             project = project, oldName = oldName
         });
     }
 }
        int IVsSolutionEvents.OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
        {
            if (!ProjectArguments.IsSupportedProject(pRealHierarchy))
            {
                return(LogIgnoringUnsupportedProjectType());
            }

            ProjectStateMap[pRealHierarchy.GetGuid()].IsLoaded = false;

            ProjectBeforeUnload?.Invoke(this, pRealHierarchy);

            return(S_OK);
        }
        private void UpdateCurrentStartupProject()
        {
            Project startupProject = vsHelper.GetStartupProject();

            if (ProjectArguments.IsSupportedProject(startupProject))
            {
                // update StartupProject
                ToolWindowViewModel.UpdateStartupProject(startupProject.UniqueName);
            }
            else
            {
                Logger.Info($"Unsupported startup project '{startupProject?.UniqueName}' of kind '{startupProject?.Kind}'");
                ToolWindowViewModel.UpdateStartupProject(null);
            }
        }
        int IVsSolutionEvents.OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
        {
            var project = ProjectForHierarchy(pRealHierarchy);

            if (!ProjectArguments.IsSupportedProject(project))
            {
                return(LogIgnoringUnsupportedProjectType());
            }

            ProjectStateMap[pRealHierarchy.GetGuid()].IsLoaded = true;

            ProjectAfterLoad?.Invoke(this, project);

            return(S_OK);
        }
        private void UpdateConfigurationForProject(IVsHierarchy project)
        {
            if (project == null)
            {
                return;
            }

            var commandLineArgs = CreateCommandLineArgsForProject(project);

            if (commandLineArgs == null)
            {
                return;
            }

            ProjectArguments.SetArguments(project, commandLineArgs);
            Logger.Info($"Updated Configuration for Project: {project.GetName()}");
        }
        int IVsSolutionEvents4.OnAfterRenameProject(IVsHierarchy pHierarchy)
        {
            if (!ProjectArguments.IsSupportedProject(pHierarchy))
            {
                return(LogIgnoringUnsupportedProjectType());
            }

            Guid projectGuid    = pHierarchy.GetGuid();
            var  oldProjectDir  = ProjectStateMap[projectGuid].ProjectDir;
            var  oldProjectName = ProjectStateMap[projectGuid].ProjectName;

            ProjectStateMap[projectGuid].ProjectDir  = pHierarchy.GetProjectDir();
            ProjectStateMap[projectGuid].ProjectName = pHierarchy.GetName();

            ProjectAfterRename?.Invoke(this, new ProjectAfterRenameEventArgs {
                OldProjectDir = oldProjectDir, OldProjectName = oldProjectName, Project = pHierarchy
            });

            return(S_OK);
        }
        public IEnumerable <IVsHierarchy> GetSupportedProjects(bool includeUnloaded = false)
        {
            __VSENUMPROJFLAGS property = includeUnloaded ? __VSENUMPROJFLAGS.EPF_ALLINSOLUTION : __VSENUMPROJFLAGS.EPF_LOADEDINSOLUTION;

            Guid guid = Guid.Empty;

            solutionService.GetProjectEnum((uint)property, ref guid, out IEnumHierarchies enumerator);

            IVsHierarchy[] hierarchy = new IVsHierarchy[1] {
                null
            };
            uint fetched = 0;

            for (enumerator.Reset(); enumerator.Next(1, hierarchy, out fetched) == VSConstants.S_OK && fetched == 1; /*nothing*/)
            {
                if (ProjectArguments.IsSupportedProject(hierarchy[0]))
                {
                    yield return(hierarchy[0]);
                }
            }
        }
        int IVsSolutionEvents.OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
        {
            if (!ProjectArguments.IsSupportedProject(pHierarchy))
            {
                return(LogIgnoringUnsupportedProjectType());
            }

            Guid projectGuid = pHierarchy.GetGuid();

            var isUloadProcess = ProjectStateMap.TryGetValue(projectGuid, out var state) && !state.IsLoaded;

            if (!isUloadProcess)
            {
                ProjectStateMap.Remove(projectGuid);
            }

            ProjectBeforeClose?.Invoke(this, new ProjectBeforeCloseEventArgs {
                Project = pHierarchy, IsUnloadProcess = isUloadProcess, IsSolutionCloseProcess = fRemoved == 0
            });

            return(S_OK);
        }
        int IVsSolutionEvents4.OnAfterRenameProject(IVsHierarchy pHierarchy)
        {
            var project = ProjectForHierarchy(pHierarchy);

            if (!ProjectArguments.IsSupportedProject(project))
            {
                return(LogIgnoringUnsupportedProjectType());
            }

            Guid projectGuid    = pHierarchy.GetGuid();
            var  oldFilePath    = ProjectStateMap[projectGuid].FilePath;
            var  oldProjectName = ProjectStateMap[projectGuid].UniqueName;

            ProjectStateMap[projectGuid].FilePath   = project.FullName;
            ProjectStateMap[projectGuid].UniqueName = project.UniqueName;

            ProjectAfterRename?.Invoke(this, new ProjectAfterRenameEventArgs {
                OldFilePath = oldFilePath, OldUniqueName = oldProjectName, Project = project
            });

            return(S_OK);
        }
        private void UpdateConfigurationForProject(Project project)
        {
            if (project == null)
            {
                return;
            }
            IEnumerable <string> enabledEntries;

            if (IsMacroEvaluationEnabled)
            {
                enabledEntries = ToolWindowViewModel.EnabledItemsForCurrentProject().Select(
                    e => msBuildPropertyRegex.Replace(e.Command,
                                                      match => vsHelper.GetMSBuildPropertyValue(project.UniqueName, match.Groups["propertyName"].Value) ?? match.Value));
            }
            else
            {
                enabledEntries = ToolWindowViewModel.EnabledItemsForCurrentProject().Select(e => e.Command);
            }
            string prjCmdArgs = string.Join(" ", enabledEntries);

            ProjectArguments.SetArguments(project, prjCmdArgs);
            Logger.Info($"Updated Configuration for Project: {project.UniqueName}");
        }