Beispiel #1
0
        public async Task FixCode(CancellationToken cancellationToken)
        {
            RuleSetFilePath       = CommandHelper.GetAbsoluteOrDefaultFilePath(RuleSetFilePath, "./stylecop.ruleset");
            StyleCopJsonFilePath  = CommandHelper.GetAbsoluteOrDefaultFilePath(StyleCopJsonFilePath, "./stylecop.json");
            TargetFileOrDirectory = CommandHelper.GetAbsolutePath(TargetFileOrDirectory);

            this.logger.LogDebug($"Arguments ============================");
            this.logger.LogDebug($"Verbose Log : {LogLevelIsVerbose}");
            this.logger.LogDebug($"ruleset : {RuleSetFilePath}");
            this.logger.LogDebug($"stylecop.json : {RuleSetFilePath}");
            this.logger.LogDebug($"fix : {TargetFileOrDirectory}");
            this.logger.LogDebug($"======================================");

            if (LogLevelIsVerbose)
            {
                this.logger.SetLogLevel(LogLevel.Verbose);
            }

            Initialize();

            var inputKind = CommandHelper.GetInputKindFromFileOrDirectory(TargetFileOrDirectory);

            if (!inputKind.HasValue)
            {
                return;
            }

            foreach (var analyzer in this.allAnalyzers)
            {
                this.logger.LogVerbose("Analyze :" + string.Join(",", analyzer.SupportedDiagnostics.Select(d => d.Id)));
                foreach (var descriptor in analyzer.SupportedDiagnostics)
                {
                    this.logger.LogVerbose(" " + descriptor.Description);
                }

                var projects = LoadProject(inputKind.Value);
                if (projects.Length <= 0)
                {
                    return;
                }

                var diagnostics = await CommandHelper.GetAnalyzerDiagnosticsAsync(
                    projects,
                    ImmutableArray.Create(analyzer),
                    cancellationToken)
                                  .ConfigureAwait(false);

                if (diagnostics.Length <= 0)
                {
                    continue;
                }

                var fixableCodeFixProviders = GetFixableCodeFixProviders(diagnostics.Select(d => d.Id).ToImmutableArray());
                if (fixableCodeFixProviders.Count() <= 0)
                {
                    this.logger.LogVerbose($"Not Fixed : {diagnostics[0].Location.SourceTree.FilePath}\n    {diagnostics[0].Id} {diagnostics[0].GetMessage()}\n    NotFound codeFixProvider");
                    continue;
                }

                try
                {
                    this.logger.LogVerbose($"Try Fix : {diagnostics[0].Id} {diagnostics[0].GetMessage()}");
                    var fixedContexts = await FixDiagnosticsAsync(projects, diagnostics, fixableCodeFixProviders, cancellationToken);

                    var documentWriter = new FixedDocumentContextWriter() as IFixedContextWriter;
                    documentWriter.SetLogger(this.logger);
                    foreach (var context in fixedContexts)
                    {
                        documentWriter.Write(context);
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }
        }