Example #1
0
        public int OnAfterSave(uint docCookie)
        {
            try
            {
                if (vcProject == null || string.IsNullOrEmpty(projectDirectory) || wrapper == null)
                {
                    return(VSConstants.S_OK);
                }

                UpdateIncludesOnAfterSave();
                SaveAndReset();
                ProjectIncludesManager.AddTargetsFileToOldProjects(vcProject);
                vcProject = null;
                return(VSConstants.S_OK);
            }
            catch (Exception e)
            {
                Reset();
                try
                {
                    ThreadHelper.ThrowIfNotOnUIThread();
                    IVsActivityLog log = Package.GetGlobalService(typeof(SVsActivityLog)) as IVsActivityLog;
                    log.LogEntry((uint)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, this.ToString(),
                                 "An error occurred while updating the includes: " + e.Message + e.StackTrace);
                }
                catch (Exception) { /*try to log error in activity log*/ }
                return(VSConstants.S_OK);
            }

            void SaveAndReset()
            {
                projectDirectory = string.Empty;
                wrapper          = null;
                vcProject.Save();
            }
        }
Example #2
0
 private void Reset()
 {
     projectDirectory = string.Empty;
     wrapper          = null;
     vcProject        = null;
 }
Example #3
0
        private void UpdateIncludesOnBeforeSave(VCProject p, string projectDirectory)
        {
            var(includesSaved, macrosSaved) = ProjectIncludesManager.CheckSavedIncludesAndMacros(p);
            IEnumerable <string> includesBefore            = null;
            IEnumerable <CompilerMacroResult> macrosBefore = null;

            try
            {
                ThreadHelper.JoinableTaskFactory.Run("Updating includes and macros", async(progress) =>
                {
                    await Task.Run(() => GetIncludesAndMacros());

                    void GetIncludesAndMacros()
                    {
                        if (!includesSaved)
                        {
                            progress.Report(new ThreadedWaitDialogProgressData("Fetching project information"));
                            ProjectInformationCommandResult projectInformationBefore = null;
                            try
                            {
                                projectInformationBefore = cliCommunication.ExecuteCommand(Resources.Command_get_project_information, null,
                                                                                           typeof(ProjectInformationCommandResult), Resources.Option_get_project_information_project,
                                                                                           $"\"{projectDirectory}\"") as ProjectInformationCommandResult;
                            }
                            catch (PlcncliException ex)
                            {
                                projectInformationBefore = cliCommunication.ConvertToTypedCommandResult <ProjectInformationCommandResult>(ex.InfoMessages);
                            }
                            includesBefore = projectInformationBefore?.IncludePaths.Select(x => x.PathValue);
                            if (includesBefore == null)
                            {
                                includesBefore = Enumerable.Empty <string>();
                            }
                        }

                        if (!macrosSaved)
                        {
                            progress.Report(new ThreadedWaitDialogProgressData("Fetching compiler information"));

                            CompilerSpecificationCommandResult compilerSpecsBefore = null;
                            try
                            {
                                compilerSpecsBefore = cliCommunication.ExecuteCommand(Resources.Command_get_compiler_specifications, null,
                                                                                      typeof(CompilerSpecificationCommandResult), Resources.Option_get_compiler_specifications_project,
                                                                                      $"\"{projectDirectory}\"") as CompilerSpecificationCommandResult;
                            }
                            catch (PlcncliException ex)
                            {
                                compilerSpecsBefore = cliCommunication.ConvertToTypedCommandResult <CompilerSpecificationCommandResult>(ex.InfoMessages);
                            }
                            macrosBefore = compilerSpecsBefore?.Specifications.FirstOrDefault()
                                           ?.CompilerMacros.Where(m => !m.Name.StartsWith("__has_include(")) ?? Enumerable.Empty <CompilerMacroResult>();
                        }
                    }
                });

                this.vcProject        = p;
                this.projectDirectory = p.ProjectDirectory;
                this.wrapper          = new IncludesAndMacrosWrapper(includesBefore, macrosBefore);
            }
            catch (Exception e)
            {
                Reset();
                try
                {
                    ThreadHelper.ThrowIfNotOnUIThread();
                    IVsActivityLog log = Package.GetGlobalService(typeof(SVsActivityLog)) as IVsActivityLog;
                    log.LogEntry((uint)__ACTIVITYLOG_ENTRYTYPE.ALE_ERROR, this.ToString(),
                                 "An error occurred while updating the includes: " + e.Message + e.StackTrace);
                }
                catch (Exception) { /*try to log error in activity log*/ }
            }
        }