Exemplo n.º 1
0
        private static async Task FormatProjectAsync(Project project, CodeFormatterOptions options, CancellationToken cancellationToken)
        {
            Solution solution = project.Solution;

            string solutionDirectory = Path.GetDirectoryName(solution.FilePath);

            WriteLine($"  Analyze '{project.Name}'", Verbosity.Minimal);

            ISyntaxFactsService syntaxFacts = MefWorkspaceServices.Default.GetService <ISyntaxFactsService>(project.Language);

            Project newProject = await CodeFormatter.FormatProjectAsync(project, syntaxFacts, options, cancellationToken);

            ImmutableArray <DocumentId> formattedDocuments = await CodeFormatter.GetFormattedDocumentsAsync(project, newProject, syntaxFacts);

            LogHelpers.WriteFormattedDocuments(formattedDocuments, project, solutionDirectory);

            if (formattedDocuments.Length > 0)
            {
                Solution newSolution = newProject.Solution;

                WriteLine($"Apply changes to solution '{newSolution.FilePath}'", Verbosity.Normal);

                if (!solution.Workspace.TryApplyChanges(newSolution))
                {
                    Debug.Fail($"Cannot apply changes to solution '{newSolution.FilePath}'");
                    WriteLine($"Cannot apply changes to solution '{newSolution.FilePath}'", ConsoleColor.Yellow, Verbosity.Diagnostic);
                }
            }

            WriteLine();
            WriteLine($"{formattedDocuments.Length} {((formattedDocuments.Length == 1) ? "document" : "documents")} formatted", ConsoleColor.Green, Verbosity.Normal);
            WriteLine();
        }
Exemplo n.º 2
0
        private async Task FormatSolutionAsync(Solution solution, CodeFormatterOptions options, CancellationToken cancellationToken)
        {
            string solutionDirectory = Path.GetDirectoryName(solution.FilePath);

            WriteLine($"Analyze solution '{solution.FilePath}'", ConsoleColor.Cyan, Verbosity.Minimal);

            Stopwatch stopwatch = Stopwatch.StartNew();

            var changedDocuments = new ConcurrentBag <ImmutableArray <DocumentId> >();

            Parallel.ForEach(FilterProjects(solution, Options), project =>
            {
                WriteLine($"  Analyze '{project.Name}'", Verbosity.Minimal);

                ISyntaxFactsService syntaxFacts = MefWorkspaceServices.Default.GetService <ISyntaxFactsService>(project.Language);

                Project newProject = CodeFormatter.FormatProjectAsync(project, syntaxFacts, options, cancellationToken).Result;

                ImmutableArray <DocumentId> formattedDocuments = CodeFormatter.GetFormattedDocumentsAsync(project, newProject, syntaxFacts).Result;

                if (formattedDocuments.Any())
                {
                    changedDocuments.Add(formattedDocuments);
                    LogHelpers.WriteFormattedDocuments(formattedDocuments, project, solutionDirectory);
                }

                WriteLine($"  Done analyzing '{project.Name}'", Verbosity.Normal);
            });

            if (changedDocuments.Count > 0)
            {
                Solution newSolution = solution;

                foreach (DocumentId documentId in changedDocuments.SelectMany(f => f))
                {
                    SourceText sourceText = await solution.GetDocument(documentId).GetTextAsync(cancellationToken);

                    newSolution = newSolution.WithDocumentText(documentId, sourceText);
                }

                WriteLine($"Apply changes to solution '{solution.FilePath}'", Verbosity.Normal);

                if (!solution.Workspace.TryApplyChanges(newSolution))
                {
                    Debug.Fail($"Cannot apply changes to solution '{solution.FilePath}'");
                    WriteLine($"Cannot apply changes to solution '{solution.FilePath}'", ConsoleColor.Yellow, Verbosity.Diagnostic);
                }
            }

            WriteLine();
            WriteLine($"{changedDocuments.Count} {((changedDocuments.Count == 1) ? "document" : "documents")} formatted", ConsoleColor.Green, Verbosity.Normal);
            WriteLine();

            WriteLine($"Done formatting solution '{solution.FilePath}' in {stopwatch.Elapsed:mm\\:ss\\.ff}", Verbosity.Minimal);
        }
Exemplo n.º 3
0
        public override async Task <CommandResult> ExecuteAsync(ProjectOrSolution projectOrSolution, CancellationToken cancellationToken = default)
        {
            ImmutableArray <string> supportedDiagnosticIds = Options
                                                             .GetSupportedDiagnostics()
                                                             .Select(f => f.Id)
                                                             .ToImmutableArray();

            if (supportedDiagnosticIds.Any())
            {
                var codeFixerOptions = new CodeFixerOptions(
                    severityLevel: DiagnosticSeverity.Hidden,
                    ignoreCompilerErrors: true,
                    ignoreAnalyzerReferences: true,
                    supportedDiagnosticIds: supportedDiagnosticIds,
                    projectNames: Options.Projects,
                    ignoredProjectNames: Options.IgnoredProjects,
                    language: Language,
                    batchSize: 1000,
                    format: true);

                CultureInfo culture = (Options.Culture != null) ? CultureInfo.GetCultureInfo(Options.Culture) : null;

                return(await FixCommand.FixAsync(
                           projectOrSolution,
                           RoslynatorAnalyzerAssemblies.AnalyzersAndCodeFixes,
                           codeFixerOptions,
                           culture,
                           cancellationToken));
            }
            else if (projectOrSolution.IsProject)
            {
                Project project = projectOrSolution.AsProject();

                var options = new CodeFormatterOptions(includeGeneratedCode: Options.IncludeGeneratedCode);

                await FormatProjectAsync(project, options, cancellationToken);
            }
            else
            {
                Solution solution = projectOrSolution.AsSolution();

                var options = new CodeFormatterOptions(includeGeneratedCode: Options.IncludeGeneratedCode);

                await FormatSolutionAsync(solution, options, cancellationToken);
            }

            return(CommandResult.Success);
        }