Ejemplo n.º 1
0
        private static async Task <ITaskHandler> SetupTaskStatusCenter()
        {
            IVsTaskStatusCenterService tsc =
                await _package.GetServiceAsync(typeof(SVsTaskStatusCenterService)) as IVsTaskStatusCenterService;

            TaskHandlerOptions options = default(TaskHandlerOptions);

            options.Title = Vsix.Name;
            options.DisplayTaskDetails     = task => { Logger.ShowOutputWindowPane(); };
            options.ActionsAfterCompletion = CompletionActions.None;
            TaskProgressData data = default(TaskProgressData);

            data.CanBeCanceled = true;
            return(tsc.PreRegister(options, data));
        }
        public async Task <ITaskHandler> CreateTaskHandlerAsync(string title)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsTaskStatusCenterService taskStatusCenter = (IVsTaskStatusCenterService)Package.GetGlobalService(typeof(SVsTaskStatusCenterService));
            TaskHandlerOptions         options          = default(TaskHandlerOptions);

            options.Title = title;
            options.ActionsAfterCompletion = CompletionActions.None;

            TaskProgressData data = default(TaskProgressData);

            data.CanBeCanceled = true;

            ITaskHandler handler = taskStatusCenter.PreRegister(options, data);

            return(handler);
        }
Ejemplo n.º 3
0
        public async Task RunAsync()
        {
            var outputWindow = OutputWindowRedirector.GetGeneral(_site);
            var taskHandler  = _statusCenter.PreRegister(
                new TaskHandlerOptions()
            {
                ActionsAfterCompletion = CompletionActions.RetainAndNotifyOnFaulted | CompletionActions.RetainAndNotifyOnRanToCompletion,
                Title = Strings.VirtualEnvStatusCenterCreateTitle.FormatUI(PathUtils.GetFileOrDirectoryName(_virtualEnvPath)),
                DisplayTaskDetails = (t) => { outputWindow.ShowAndActivate(); }
            },
                new TaskProgressData()
            {
                CanBeCanceled   = false,
                ProgressText    = Strings.VirtualEnvStatusCenterCreateProgressPreparing,
                PercentComplete = null,
            }
                );

            var task = CreateVirtualEnvironmentAsync(taskHandler);

            taskHandler?.RegisterTask(task);
            _site.ShowTaskStatusCenter();
        }
Ejemplo n.º 4
0
        public async Task RunAsync()
        {
            var outputWindow = OutputWindowRedirector.GetGeneral(_site);
            var taskHandler  = _statusCenter?.PreRegister(
                new TaskHandlerOptions()
            {
                ActionsAfterCompletion = CompletionActions.RetainAndNotifyOnFaulted | CompletionActions.RetainAndNotifyOnRanToCompletion,
                Title = Strings.InstallPackagesStatusCenterTitle.FormatUI(PathUtils.GetFileOrDirectoryName(_pm.Factory.Configuration.Description)),
                DisplayTaskDetails = (t) => { outputWindow.ShowAndActivate(); }
            },
                new TaskProgressData()
            {
                CanBeCanceled   = false,
                ProgressText    = Strings.InstallPackagesStatusCenterProgressPreparing,
                PercentComplete = null,
            }
                );

            var task = InstallPackagesAsync(taskHandler);

            taskHandler?.RegisterTask(task);
            _site.ShowTaskStatusCenter();
        }
Ejemplo n.º 5
0
        private void UpdateUI(ProgressData progressData)
        {
            if (progressData.Status == ProgressStatus.Stopped)
            {
                StopTaskCenter();
                return;
            }

            // Update the pending item count if the progress data specifies a value.
            if (progressData.PendingItemCount.HasValue)
            {
                _lastPendingItemCount = progressData.PendingItemCount.Value;
            }

            // Start the task center task if not already running.
            if (_taskHandler == null)
            {
                // Register a new task handler to handle a new task center task.
                // Each task handler can only register one task, so we must create a new one each time we start.
                _taskHandler = _taskCenterService.PreRegister(_options, data: default);

                // Create a new non-completed task to be tracked by the task handler.
                _taskCenterTask = new TaskCompletionSource <VoidResult>();
                _taskHandler.RegisterTask(_taskCenterTask.Task);
            }

            var statusMessage = progressData.Status == ProgressStatus.Paused
                ? ServicesVSResources.Paused_0_tasks_in_queue
                : ServicesVSResources.Evaluating_0_tasks_in_queue;

            _taskHandler.Progress.Report(new TaskProgressData
            {
                ProgressText    = string.Format(statusMessage, _lastPendingItemCount),
                CanBeCanceled   = false,
                PercentComplete = null,
            });
        }
Ejemplo n.º 6
0
        public void ApplyChanges()
        {
            IVsTaskStatusCenterService taskCenter = Package.GetGlobalService(typeof(SVsTaskStatusCenterService)) as IVsTaskStatusCenterService;
            ITaskHandler taskHandler = taskCenter.PreRegister(
                new TaskHandlerOptions()
            {
                Title = "Updating sdks..."
            },
                new TaskProgressData());

            if (model.SdkChangesCollector.SdksToRemove.Count > 0)
            {
                RemoveSdkViewModel dialogVM = new RemoveSdkViewModel(model.SdkChangesCollector.SdksToRemove);
                RemoveSdkDialog    dialog   = new RemoveSdkDialog(dialogVM);
                bool?result = dialog.ShowModal();
                if (result == null || result == false)
                {
                    return;
                }
                if (dialogVM.RemoveFromDisk)
                {
                    foreach (string path in model.SdkChangesCollector.SdksToRemove)
                    {
                        Directory.Delete(path, true);
                    }
                }
            }


            taskHandler.RegisterTask(Task.Run(async() =>
            {
                foreach (string sdk in model.SdkChangesCollector.SdksToRemove)
                {
                    ITaskHandler subTaskHandler = taskCenter.PreRegister(
                        new TaskHandlerOptions()
                    {
                        Title = $"Removing sdk {sdk}"
                    },
                        new TaskProgressData());

                    Task task = Task.Run(() =>
                    {
                        try
                        {
                            plcncliCommunication.ExecuteCommand("set setting", null, null, "-r", "SdkPaths", $"\"{sdk}\"");
                        }
                        catch (PlcncliException e)
                        {
                            MessageBox.Show(e.Message, "PLCnCLI error");
                        }
                    });
                    subTaskHandler.RegisterTask(task);
                    await task;
                }

                foreach (string sdk in model.SdkChangesCollector.SdksToAdd)
                {
                    ITaskHandler subTaskHandler = taskCenter.PreRegister(
                        new TaskHandlerOptions()
                    {
                        Title = $"Adding sdk {sdk}"
                    },
                        new TaskProgressData());

                    Task task = Task.Run(() =>
                    {
                        try
                        {
                            plcncliCommunication.ExecuteCommand("set setting", null, null, "-a", "SdkPaths", $"\"{sdk}\"");
                        }
                        catch (PlcncliException e)
                        {
                            MessageBox.Show(e.Message, "PLCnCLI error");
                        }
                    });
                    subTaskHandler.RegisterTask(task);
                    await task;
                }
                foreach (InstallSdk sdk in model.SdkChangesCollector.SdksToInstall)
                {
                    ITaskHandler subTaskHandler = taskCenter.PreRegister(
                        new TaskHandlerOptions()
                    {
                        Title = $"Installing sdk {sdk.ArchiveFile} to {sdk.Destination}"
                    },
                        new TaskProgressData());
                    Task task = Task.Run(() =>
                    {
                        try
                        {
                            plcncliCommunication.ExecuteCommand("install sdk", null, null, "--path", $"\"{sdk.ArchiveFile}\"",
                                                                "--destination", $"\"{sdk.Destination}\"", sdk.Force ? "--force" : "");
                        }
                        catch (PlcncliException e)
                        {
                            MessageBox.Show(e.Message, "PLCnCLI error");
                        }
                    });
                    subTaskHandler.RegisterTask(task);
                    await task;
                }
            }));

            if (model.SdkChangesCollector.SdksToInstall.Count > 0)
            {
                MessageBox.Show("Installing sdks in background. This may take a while. New sdks are available after background task has finished." +
                                " Check lower left corner for active background tasks.", "Background installation started", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }
Ejemplo n.º 7
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);
            }
        }