public CommandResult Execute(AnalyzeAssemblyCommandLineOptions options)
        {
            var assemblies = new HashSet <Assembly>();

            AnalyzerAssemblyInfo[] analyzerAssemblies = options.GetPaths()
                                                        .SelectMany(path => AnalyzerAssemblyLoader.LoadFrom(
                                                                        path: path,
                                                                        loadAnalyzers: !options.NoAnalyzers,
                                                                        loadFixers: !options.NoFixers,
                                                                        language: Language))
                                                        .OrderBy(f => f.AnalyzerAssembly.Assembly.GetName().Name)
                                                        .ThenBy(f => f.FilePath)
                                                        .ToArray();

            for (int i = 0; i < analyzerAssemblies.Length; i++)
            {
                AnalyzerAssembly analyzerAssembly = analyzerAssemblies[i].AnalyzerAssembly;

                if (assemblies.Add(analyzerAssembly.Assembly))
                {
                    WriteLine(analyzerAssembly.FullName, ConsoleColor.Cyan, Verbosity.Minimal);

                    if (ShouldWrite(Verbosity.Normal))
                    {
                        WriteAnalyzerAssembly(analyzerAssemblies[i], DiagnosticMap.Create(analyzerAssembly));

                        if (i < analyzerAssemblies.Length - 1)
                        {
                            WriteLine(Verbosity.Normal);
                        }
                    }
                }
                else
                {
                    Write(analyzerAssembly.FullName, ConsoleColor.DarkGray, Verbosity.Minimal);
                    WriteLine($" [{analyzerAssemblies[i].FilePath}]", ConsoleColor.DarkGray, Verbosity.Minimal);
                }
            }

            if (ShouldWrite(Verbosity.Detailed) &&
                analyzerAssemblies.Length > 1)
            {
                DiagnosticMap map = DiagnosticMap.Create(analyzerAssemblies.Select(f => f.AnalyzerAssembly));

                WriteLine(Verbosity.Detailed);
                WriteDiagnostics(map, allProperties: true, useAssemblyQualifiedName: true);
            }

            WriteLine(Verbosity.Minimal);
            WriteLine($"{assemblies.Count} analyzer {((assemblies.Count == 1) ? "assembly" : "assemblies")} found", ConsoleColor.Green, Verbosity.Minimal);
            WriteLine(Verbosity.Minimal);

            if (options.Output != null &&
                analyzerAssemblies.Length > 0)
            {
                AnalyzerAssemblyXmlSerializer.Serialize(analyzerAssemblies, options.Output);
            }

            return(CommandResult.Success);
        }
Example #2
0
        public static void WriteXml(
            XmlWriter writer,
            IEnumerable <AnalyzerAssembly> analyzerAssemblies,
            string name,
            Version toolsVersion,
            string description             = null,
            IFormatProvider formatProvider = null)
        {
            writer.WriteStartDocument();

            writer.WriteStartElement("RuleSet");
            writer.WriteAttributeString("Name", name);
            writer.WriteAttributeString("ToolsVersion", toolsVersion.ToString(2));

            if (!string.IsNullOrEmpty(description))
            {
                writer.WriteAttributeString("Description", description);
            }

            foreach (AnalyzerAssembly analyzerAssembly in analyzerAssemblies.OrderBy(f => f.Name))
            {
                using (IEnumerator <DiagnosticDescriptor> en = DiagnosticMap.Create(analyzerAssembly).SupportedDiagnostics
                                                               .Where(f => !f.CustomTags.Contains(WellKnownDiagnosticTags.NotConfigurable) &&
                                                                      !f.IsAnalyzerExceptionDescriptor())
                                                               .OrderBy(f => f.Id)
                                                               .GetEnumerator())
                {
                    if (en.MoveNext())
                    {
                        writer.WriteStartElement("Rules");
                        writer.WriteAttributeString("AnalyzerId", analyzerAssembly.Name);
                        writer.WriteAttributeString("RuleNamespace", analyzerAssembly.Name);

                        do
                        {
                            writer.WriteWhitespace(writer.Settings.NewLineChars);
                            writer.WriteWhitespace(writer.Settings.IndentChars);
                            writer.WriteWhitespace(writer.Settings.IndentChars);
                            writer.WriteStartElement("Rule");
                            DiagnosticDescriptor current = en.Current;
                            writer.WriteAttributeString("Id", current.Id);
                            writer.WriteAttributeString("Action", GetAction(current));
                            writer.WriteEndElement();

                            string title = current.Title.ToString(formatProvider);

                            if (!string.IsNullOrEmpty(title))
                            {
                                writer.WriteWhitespace(" ");
                                writer.WriteComment($" {title} ");
                            }
                        }while (en.MoveNext());

                        writer.WriteWhitespace(writer.Settings.NewLineChars);
                        writer.WriteWhitespace(writer.Settings.IndentChars);
                        writer.WriteEndElement();
                    }
                }
            }
Example #3
0
        public CommandStatus Execute(AnalyzeAssemblyCommandLineOptions options)
        {
            var assemblies = new HashSet <Assembly>();

            AnalyzerAssemblyInfo[] analyzerAssemblies = options.GetPaths()
                                                        .SelectMany(path => AnalyzerAssemblyLoader.LoadFrom(
                                                                        path: path,
                                                                        searchPattern: options.FileNamePattern ?? AnalyzerAssemblyLoader.DefaultSearchPattern,
                                                                        loadAnalyzers: !options.NoAnalyzers,
                                                                        loadFixers: !options.NoFixers,
                                                                        language: Language))
                                                        .OrderBy(f => f.AnalyzerAssembly.Assembly.GetName().Name)
                                                        .ThenBy(f => f.FilePath)
                                                        .ToArray();

            for (int i = 0; i < analyzerAssemblies.Length; i++)
            {
                AnalyzerAssembly analyzerAssembly = analyzerAssemblies[i].AnalyzerAssembly;

                if (assemblies.Add(analyzerAssembly.Assembly))
                {
                    WriteLine(analyzerAssembly.FullName, ConsoleColors.Cyan, Verbosity.Minimal);

                    if (ShouldWrite(Verbosity.Normal))
                    {
                        WriteAnalyzerAssembly(analyzerAssemblies[i], DiagnosticMap.Create(analyzerAssembly));

                        if (i < analyzerAssemblies.Length - 1)
                        {
                            WriteLine(Verbosity.Normal);
                        }
                    }
                }
                else
                {
                    Write(analyzerAssembly.FullName, ConsoleColors.DarkGray, Verbosity.Minimal);
                    WriteLine($" [{analyzerAssemblies[i].FilePath}]", ConsoleColors.DarkGray, Verbosity.Minimal);
                }
            }

            if (ShouldWrite(Verbosity.Detailed) &&
                analyzerAssemblies.Length > 1)
            {
                DiagnosticMap map = DiagnosticMap.Create(analyzerAssemblies.Select(f => f.AnalyzerAssembly));

                WriteLine(Verbosity.Detailed);
                WriteDiagnostics(map, allProperties: true, useAssemblyQualifiedName: true);
            }

            WriteLine(Verbosity.Minimal);
            WriteLine($"{assemblies.Count} analyzer {((assemblies.Count == 1) ? "assembly" : "assemblies")} found", ConsoleColors.Green, Verbosity.Minimal);

            if (analyzerAssemblies.Length > 0)
            {
                CultureInfo culture = (options.Culture != null) ? CultureInfo.GetCultureInfo(options.Culture) : null;

                foreach (string path in options.Output)
                {
                    WriteLine($"Save '{path}'", ConsoleColors.DarkGray, Verbosity.Diagnostic);

                    string extension = Path.GetExtension(path);

                    if (string.Equals(extension, ".xml", StringComparison.OrdinalIgnoreCase))
                    {
                        AnalyzerAssemblyXmlSerializer.Serialize(path, analyzerAssemblies, culture);
                    }
                    else if (string.Equals(extension, ".ruleset", StringComparison.OrdinalIgnoreCase))
                    {
                        WriteLine($"Save ruleset to '{path}'", ConsoleColors.DarkGray, Verbosity.Diagnostic);

                        using (var fileStream = new FileStream(path, FileMode.Create))
                            using (XmlWriter xmlWriter = XmlWriter.Create(fileStream, new XmlWriterSettings()
                            {
                                Indent = true, IndentChars = "  ", CloseOutput = false
                            }))
                            {
                                RuleSetUtility.WriteXml(
                                    writer: xmlWriter,
                                    analyzerAssemblies: analyzerAssemblies.Select(f => f.AnalyzerAssembly),
                                    name: "",
                                    toolsVersion: new Version(15, 0),
                                    description: null,
                                    formatProvider: culture);
                            }
                    }
                }
            }

            return((analyzerAssemblies.Length > 0) ? CommandStatus.Success : CommandStatus.NotSuccess);
        }