Beispiel #1
0
        private async Task <bool> DefinitionFilesShouldBeAddedAsync(ICollection <Project> projectsWithoutLicenseHeaderFile, Window window)
        {
            if (!projectsWithoutLicenseHeaderFile.Any())
            {
                return(false);
            }

            var    errorResourceString = Resources.Error_MultipleProjectsNoLicenseHeaderFile;
            string projects;

            if (projectsWithoutLicenseHeaderFile.Count > c_maxProjectsWithoutDefinitionFileShownInMessage)
            {
                projects = string.Join(
                    "\n",
                    projectsWithoutLicenseHeaderFile.Select(
                        x =>
                {
                    ThreadHelper.ThrowIfNotOnUIThread();
                    return(x.Name);
                }).Take(c_maxProjectsWithoutDefinitionFileShownInMessage));
                projects += "\n...";
            }
            else
            {
                projects = string.Join(
                    "\n",
                    projectsWithoutLicenseHeaderFile.Select(
                        x =>
                {
                    ThreadHelper.ThrowIfNotOnUIThread();
                    return(x.Name);
                }));
            }

            var message = string.Format(errorResourceString, projects).ReplaceNewLines();

            return(await MessageBoxHelper.AskYesNoAsync(window, message).ConfigureAwait(true));
        }
Beispiel #2
0
        public override async Task DoWorkAsync(CancellationToken cancellationToken, BaseUpdateViewModel viewModel, Solution solution, Window window)
        {
            if (solution == null)
            {
                return;
            }

            if (!(viewModel is SolutionUpdateViewModel updateViewModel))
            {
                throw new ArgumentException($"Argument {nameof(viewModel)} must be of type {nameof(SolutionUpdateViewModel)}");
            }

            await _licenseHeaderExtension.JoinableTaskFactory.SwitchToMainThreadAsync();

            var solutionHeaderDefinitions = LicenseHeaderFinder.GetHeaderDefinitionForSolution(solution);

            var allSolutionProjectsSearcher = new AllSolutionProjectsSearcher();
            var projectsInSolution          = allSolutionProjectsSearcher.GetAllProjects(solution);

            var projectsWithoutLicenseHeaderFile = projectsInSolution
                                                   .Where(project => LicenseHeaderFinder.GetHeaderDefinitionForProjectWithoutFallback(project) == null)
                                                   .ToList();

            var projectsWithLicenseHeaderFile = projectsInSolution
                                                .Where(project => LicenseHeaderFinder.GetHeaderDefinitionForProjectWithoutFallback(project) != null)
                                                .ToList();

            if (solutionHeaderDefinitions != null || !projectsWithoutLicenseHeaderFile.Any())
            {
                // Every project is covered either by a solution or project level license header definition, go ahead and add them.
                await AddLicenseHeaderToProjectsAsync(cancellationToken, projectsInSolution, updateViewModel);
            }
            else
            {
                // Some projects are not covered by a header.

                var someProjectsHaveDefinition = projectsWithLicenseHeaderFile.Count > 0;
                if (someProjectsHaveDefinition)
                {
                    // Some projects have a header. Ask the user if they want to add an existing header to the uncovered projects.
                    if (await DefinitionFilesShouldBeAddedAsync(projectsWithoutLicenseHeaderFile, window))
                    {
                        ExistingLicenseHeaderDefinitionFileAdder.AddDefinitionFileToMultipleProjects(projectsWithoutLicenseHeaderFile);
                    }

                    await AddLicenseHeaderToProjectsAsync(cancellationToken, projectsInSolution, updateViewModel);
                }
                else
                {
                    // No projects have definition. Ask the user if they want to add a solution level header definition.
                    if (await MessageBoxHelper.AskYesNoAsync(window, Resources.Question_AddNewLicenseHeaderDefinitionForSolution.ReplaceNewLines()).ConfigureAwait(true))
                    {
                        AddNewSolutionLicenseHeaderDefinitionFileCommand.Instance.Invoke(solution);

                        // They want to go ahead and apply without editing.
                        if (!await MessageBoxHelper.AskYesNoAsync(window, Resources.Question_StopForConfiguringDefinitionFilesSingleFile).ConfigureAwait(true))
                        {
                            await AddLicenseHeaderToProjectsAsync(cancellationToken, projectsInSolution, updateViewModel);
                        }
                    }
                }
            }
        }