Exemple #1
0
        // private readonly Stopwatch stopwatch = new Stopwatch();

        // [Conditional("DEBUG")]
        // private void DebugTimeLog(string message)
        // {
        //     this.logger.LogDebug($"{message} in {stopwatch.ElapsedMilliseconds}ms");
        // }

        public async Task Check(CancellationToken cancellationToken)
        {
            RuleSetFilePath      = CommandHelper.GetAbsoluteOrDefaultFilePath(RuleSetFilePath, "./stylecop.ruleset");
            StyleCopJsonFilePath = CommandHelper.GetAbsoluteOrDefaultFilePath(StyleCopJsonFilePath, "./stylecop.json");

            if (!Targets.Any())
            {
                return;
            }

            this.logger.LogDebug("Arguments ============================");
            this.logger.LogDebug($"ruleset : {RuleSetFilePath}");
            this.logger.LogDebug($"stylecop.json : {RuleSetFilePath}");
            this.logger.LogDebug($"format : {OutputFormat}");
            this.logger.LogDebug($"check : \n{string.Join("\n", Targets)}");
            this.logger.LogDebug("======================================");

            var projects = ImmutableArray.CreateBuilder <Project>();

            foreach (var target in Targets)
            {
                var targetFileOrDirectory = CommandHelper.GetAbsolutePath(target);

                var inputKind = CommandHelper.GetInputKindFromFileOrDirectory(targetFileOrDirectory);
                if (!inputKind.HasValue)
                {
                    return;
                }

                var readableProjects = inputKind.Value.ToReader().ReadAllSourceCodeFiles(targetFileOrDirectory, StyleCopJsonFilePath);
                if (readableProjects.Length == 0)
                {
                    return;
                }

                projects.AddRange(readableProjects);
            }

            var outputKind = OutputKindHelper.ToOutputKind(OutputFormat);

            if (outputKind == OutputKind.Undefined)
            {
                Console.Error.WriteLine($"output format is undefined. -f {OutputFormat}");
                return;
            }

            var analyzerLoader = new AnalyzerLoader(RuleSetFilePath);
            var analyzers      = analyzerLoader.GetAnalyzers();
            var diagnostics    = await CommandHelper.GetAnalyzerDiagnosticsAsync(
                projects.ToImmutable(),
                analyzers,
                analyzerLoader.RuleSets,
                cancellationToken).ConfigureAwait(false);

            var writer = outputKind.ToWriter();

            writer.Write(diagnostics);
        }
        public async Task Check(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($"ruleset : {RuleSetFilePath}");
            this.logger.LogDebug($"stylecop.json : {RuleSetFilePath}");
            this.logger.LogDebug($"format : {OutputFormat}");
            this.logger.LogDebug($"check : {TargetFileOrDirectory}");
            this.logger.LogDebug($"======================================");

            var inputKind = CommandHelper.GetInputKindFromFileOrDirectory(TargetFileOrDirectory);

            if (!inputKind.HasValue)
            {
                return;
            }

            var projects = inputKind.Value.ToReader().ReadAllSourceCodeFiles(TargetFileOrDirectory, StyleCopJsonFilePath);

            if (projects.Length <= 0)
            {
                return;
            }

            var outputKind = OutputKindHelper.ToOutputKind(OutputFormat);

            if (outputKind == OutputKind.Undefined)
            {
                Console.Error.WriteLine($"output format is undefined. -f {OutputFormat}");
                return;
            }

            var analyzerLoader = new AnalyzerLoader(RuleSetFilePath);
            var analyzers      = analyzerLoader.GetAnalyzers();
            var diagnostics    = await CommandHelper.GetAnalyzerDiagnosticsAsync(projects, analyzers, cancellationToken).ConfigureAwait(false);

            var writer = outputKind.ToWriter();

            writer.Write(diagnostics);
        }
Exemple #3
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);
                }
            }
        }