Example #1
0
        private Task OnProjectManagerSavingFailedAsync(object sender, ProjectErrorEventArgs e)
        {
            IsSuspended = false;

            return(TaskHelper.Completed);
        }
        private async Task <bool> SyncedRefreshAsync(IProject project)
        {
            var projectLocation = project.Location;

            var activeProjectLocation = this.GetActiveProjectLocation();

            Log.Debug("Refreshing project from '{0}'", projectLocation);

            var isRefreshingActiveProject = activeProjectLocation.EndsWithIgnoreCase(projectLocation);

            _projectStateSetter.SetProjectRefreshing(projectLocation, true, isRefreshingActiveProject);

            var cancelEventArgs = new ProjectCancelEventArgs(project);
            await ProjectRefreshingAsync.SafeInvokeAsync(this, cancelEventArgs, false).ConfigureAwait(false);

            Exception          error             = null;
            IValidationContext validationContext = null;

            try
            {
                if (cancelEventArgs.Cancel)
                {
                    _projectStateSetter.SetProjectRefreshing(projectLocation, false, true);

                    await ProjectRefreshingCanceledAsync.SafeInvokeAsync(this, new ProjectErrorEventArgs(project)).ConfigureAwait(false);

                    return(false);
                }

                UnregisterProject(project);

                if (isRefreshingActiveProject)
                {
                    await SetActiveProjectAsync(null).ConfigureAwait(false);
                }

                validationContext = await _projectValidator.ValidateProjectBeforeLoadingAsync(projectLocation);

                if (validationContext.HasErrors)
                {
                    throw Log.ErrorAndCreateException <InvalidOperationException>($"Project could not be loaded from '{projectLocation}', the validator returned errors");
                }

                var loadedProject = await QuietlyLoadProjectAsync(projectLocation, false).ConfigureAwait(false);

                validationContext = await _projectValidator.ValidateProjectAsync(loadedProject);

                if (validationContext.HasErrors)
                {
                    throw Log.ErrorAndCreateException <InvalidOperationException>($"Project data was loaded from '{projectLocation}', but the validator returned errors");
                }

                RegisterProject(loadedProject);

                // Note: we disable IsRefreshingActiveProject at Activated event, that is why isActiveProject == false
                _projectStateSetter.SetProjectRefreshing(projectLocation, true, false);

                await ProjectRefreshedAsync.SafeInvokeAsync(this, new ProjectEventArgs(loadedProject)).ConfigureAwait(false);

                if (isRefreshingActiveProject)
                {
                    await SetActiveProjectAsync(loadedProject).ConfigureAwait(false);
                }

                Log.Info("Refreshed project from '{0}'", projectLocation);
            }
            catch (Exception exception)
            {
                error = exception;
            }

            if (error == null)
            {
                return(true);
            }

            _projectStateSetter.SetProjectRefreshing(projectLocation, false, true);

            var eventArgs = new ProjectErrorEventArgs(project,
                                                      new ProjectException(project, $"Failed to load project from location '{projectLocation}' while refreshing.", error),
                                                      validationContext);

            await ProjectRefreshingFailedAsync.SafeInvokeAsync(this, eventArgs).ConfigureAwait(false);

            return(false);
        }