Exemplo n.º 1
0
        private static async Task <int> FixAsync(FixCommandLineOptions options)
        {
            if (!options.TryGetDiagnosticSeverity(CodeFixerOptions.Default.SeverityLevel, out DiagnosticSeverity severityLevel))
            {
                return(1);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixMap, out Dictionary <string, string> diagnosticFixMap))
            {
                return(1);
            }

            if (!TryParseKeyValuePairs(options.DiagnosticFixerMap, out Dictionary <string, string> diagnosticFixerMap))
            {
                return(1);
            }

            if (!options.TryGetLanguage(out string language))
            {
                return(1);
            }

            var executor = new FixCommandExecutor(
                options: options,
                severityLevel: severityLevel,
                diagnosticFixMap: diagnosticFixMap?.ToImmutableDictionary() ?? ImmutableDictionary <string, string> .Empty,
                diagnosticFixerMap: diagnosticFixerMap?.ToImmutableDictionary() ?? ImmutableDictionary <string, string> .Empty,
                language: language);

            CommandResult result = await executor.ExecuteAsync(options.Path, options.MSBuildPath, options.Properties);

            return((result.Kind == CommandResultKind.Success) ? 0 : 1);
        }
Exemplo n.º 2
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 FixCommandExecutor.FixAsync(
                           projectOrSolution,
                           FixCommandExecutor.RoslynatorAnalyzersAssemblies,
                           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);
        }