Exemplo n.º 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();
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// This function is the callback used to execute the command when the menu item is clicked.
        /// See the constructor to see how the menu item is associated with this function using
        /// OleMenuCommandService service and MenuCommand class.
        /// </summary>
        /// <param name="sender">Event sender.</param>
        /// <param name="e">Event args.</param>
        private void SetTargets(object sender, EventArgs e)
        {
            ThreadHelper.ThrowIfNotOnUIThread();
            IServiceProvider serviceProvider = package;
            Project          project         = GetProject();

            if (project == null)
            {
                return;
            }
            string projectDirectory = Path.GetDirectoryName(project.FullName);

            //show dialog
            ProjectTargetValueEditorModel     model     = new ProjectTargetValueEditorModel(serviceProvider, projectDirectory);
            ProjectTargetValueEditorViewModel viewModel = new ProjectTargetValueEditorViewModel(model);
            ProjectTargetValueEditorView      view      = new ProjectTargetValueEditorView(viewModel);

            view.ShowModal();

            if (view.DialogResult == true)
            {
                if (!(serviceProvider.GetService(typeof(SPlcncliCommunication)) is IPlcncliCommunication cliCommunication))
                {
                    MessageBox.Show("Could not set project targets because no plcncli communication found.");
                    return;
                }

                VCProject p = project.Object as VCProject;
                bool      needProjectInformation  = false;
                bool      needCompilerInformation = false;

                var(includesSaved, macrosSaved) = ProjectIncludesManager.CheckSavedIncludesAndMacros(p);
                needProjectInformation          = !includesSaved;
                needCompilerInformation         = !macrosSaved;

                IEnumerable <string> includesBefore                        = null;
                IEnumerable <CompilerMacroResult>  macrosBefore            = Enumerable.Empty <CompilerMacroResult>();
                CompilerSpecificationCommandResult compilerSpecsAfter      = null;
                ProjectInformationCommandResult    projectInformationAfter = null;


                IVsTaskStatusCenterService taskCenter = Package.GetGlobalService(typeof(SVsTaskStatusCenterService)) as IVsTaskStatusCenterService;
                ITaskHandler taskHandler = taskCenter.PreRegister(
                    new TaskHandlerOptions()
                {
                    Title = $"Setting project targets"
                },
                    new TaskProgressData());
                Task task = Task.Run(async() =>
                {
                    try
                    {
                        if (needProjectInformation)
                        {
                            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)
                            {
                                try
                                {
                                    projectInformationBefore = cliCommunication.ConvertToTypedCommandResult <ProjectInformationCommandResult>(ex.InfoMessages);
                                }
                                catch (PlcncliException) {}
                            }

                            includesBefore = projectInformationBefore?.IncludePaths.Select(x => x.PathValue);
                            if (includesBefore == null)
                            {
                                includesBefore = Enumerable.Empty <string>();
                            }
                        }

                        if (needCompilerInformation)
                        {
                            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>();
                        }

                        SetTargets();

                        try
                        {
                            projectInformationAfter = cliCommunication.ExecuteCommand(Resources.Command_get_project_information, null,
                                                                                      typeof(ProjectInformationCommandResult), Resources.Option_get_project_information_project,
                                                                                      $"\"{projectDirectory}\"") as ProjectInformationCommandResult;
                        }
                        catch (PlcncliException ex)
                        {
                            projectInformationAfter = cliCommunication.ConvertToTypedCommandResult <ProjectInformationCommandResult>(ex.InfoMessages);
                        }
                        try
                        {
                            compilerSpecsAfter = cliCommunication.ExecuteCommand(Resources.Command_get_compiler_specifications, null,
                                                                                 typeof(CompilerSpecificationCommandResult), Resources.Option_get_compiler_specifications_project,
                                                                                 $"\"{projectDirectory}\"") as CompilerSpecificationCommandResult;
                        }
                        catch (PlcncliException ex)
                        {
                            compilerSpecsAfter = cliCommunication.ConvertToTypedCommandResult <CompilerSpecificationCommandResult>(ex.InfoMessages);
                        }

                        await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

                        ProjectConfigurationManager.CreateConfigurationsForAllProjectTargets
                            (projectInformationAfter?.Targets.Select(t => t.GetNameFormattedForCommandLine()), project);


                        ProjectIncludesManager.UpdateIncludesAndMacrosForExistingProject(p, macrosBefore,
                                                                                         compilerSpecsAfter, includesBefore, projectInformationAfter);

                        p.Save();

                        ProjectIncludesManager.AddTargetsFileToOldProjects(p);

                        void SetTargets()
                        {
                            foreach (TargetResult target in model.TargetsToAdd)
                            {
                                cliCommunication.ExecuteCommand(Resources.Command_set_target, null, null,
                                                                Resources.Option_set_target_add, Resources.Option_set_target_project,
                                                                $"\"{projectDirectory}\"",
                                                                Resources.Option_set_target_name, target.Name, Resources.Option_set_target_version,
                                                                $"\"{target.LongVersion}\"");
                            }

                            foreach (TargetResult target in model.TargetsToRemove)
                            {
                                cliCommunication.ExecuteCommand(Resources.Command_set_target, null, null,
                                                                Resources.Option_set_target_remove, Resources.Option_set_target_project,
                                                                $"\"{projectDirectory}\"",
                                                                Resources.Option_set_target_name, target.Name, Resources.Option_set_target_version,
                                                                $"\"{target.LongVersion}\"");
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message + ex.StackTrace ?? string.Empty, "Exception during setting of targets");
                        throw ex;
                    }
                });
                taskHandler.RegisterTask(task);
            }
        }