public static void Serialize(
            IList <AnalyzerAssemblyInfo> analyzerAssemblies,
            string filePath)
        {
            var analyzerAssemblyElements = new XElement("AnalyzerAssemblies", analyzerAssemblies.Select(f => SerializeAnalyzerAssembly(f)));

            DiagnosticMap map = DiagnosticMap.Create(analyzerAssemblies.Select(f => f.AnalyzerAssembly));

            XElement diagnosticsElement = SerializeDiagnostics(
                map,
                allProperties: true,
                useAssemblyQualifiedName: true);

            XElement fixAllProvidersElement = SerializeFixAllProviders(map.FixAllProviders);

            SerializeDocument(filePath, analyzerAssemblyElements, diagnosticsElement, fixAllProvidersElement);
        }
示例#2
0
        private static XElement SerializeAnalyzerAssembly(AnalyzerAssemblyInfo analyzerAssemblyInfo, IFormatProvider formatProvider)
        {
            AnalyzerAssembly analyzerAssembly = analyzerAssemblyInfo.AnalyzerAssembly;

            DiagnosticMap map = DiagnosticMap.Create(analyzerAssembly);

            return(new XElement(
                       "AnalyzerAssembly",
                       new XAttribute("Name", analyzerAssembly.FullName),
                       new XElement("Location", analyzerAssemblyInfo.FilePath),
                       new XElement("Summary", SerializeSummary()),
                       new XElement("Analyzers", SerializeDiagnosticAnalyzers()),
                       new XElement("Fixers", SerializeCodeFixProviders()),
                       SerializeDiagnostics(map, allProperties: false, useAssemblyQualifiedName: false, formatProvider: formatProvider)));

            IEnumerable <XElement> SerializeSummary()
            {
                if (analyzerAssembly.HasAnalyzers)
                {
                    yield return(new XElement("Analyzers",
                                              new XAttribute("Count", map.Analyzers.Length),
                                              new XElement("Languages",
                                                           analyzerAssembly.AnalyzersByLanguage
                                                           .OrderBy(f => f.Key)
                                                           .Select(f => new XElement("Language", new XAttribute("Name", f.Key), new XAttribute("Count", f.Value.Length)))),
                                              new XElement("SupportedDiagnostics",
                                                           new XAttribute("Count", map.SupportedDiagnostics.Length),
                                                           new XElement("Prefixes",
                                                                        map.SupportedDiagnosticsByPrefix
                                                                        .OrderBy(f => f.Key)
                                                                        .Select(f => new XElement("Prefix", new XAttribute("Value", f.Key), new XAttribute("Count", f.Value.Length)))))));
                }

                if (analyzerAssembly.HasFixers)
                {
                    yield return(new XElement("Fixers",
                                              new XAttribute("Count", map.Fixers.Length),
                                              new XElement("Languages",
                                                           analyzerAssembly.FixersByLanguage
                                                           .OrderBy(f => f.Key)
                                                           .Select(f => new XElement("Language", new XAttribute("Name", f.Key), new XAttribute("Count", f.Value.Length)))),
                                              new XElement("FixableDiagnostics",
                                                           new XAttribute("Count", map.FixableDiagnosticIds.Length),
                                                           new XElement("Prefixes",
                                                                        map.FixableDiagnosticIdsByPrefix
                                                                        .OrderBy(f => f.Key)
                                                                        .Select(f => new XElement("Prefix", new XAttribute("Value", f.Key), new XAttribute("Count", f.Value.Length)))))));
                }
            }

            IEnumerable <XElement> SerializeDiagnosticAnalyzers()
            {
                foreach (DiagnosticAnalyzer analyzer in map.Analyzers.OrderBy(f => f.GetType(), TypeComparer.NamespaceThenName))
                {
                    Type type = analyzer.GetType();

                    DiagnosticAnalyzerAttribute attribute = type.GetCustomAttribute <DiagnosticAnalyzerAttribute>();

                    yield return(new XElement("Analyzer",
                                              new XAttribute("Name", type.FullName),
                                              new XElement("Languages", attribute.Languages.Select(f => new XElement("Language", f))),
                                              new XElement("SupportedDiagnostics",
                                                           analyzer.SupportedDiagnostics
                                                           .Select(f => f.Id)
                                                           .Distinct()
                                                           .OrderBy(f => f)
                                                           .Select(f => new XElement("Id", f)))));
                }
            }

            IEnumerable <XElement> SerializeCodeFixProviders()
            {
                foreach (CodeFixProvider fixer in map.Fixers.OrderBy(f => f.GetType(), TypeComparer.NamespaceThenName))
                {
                    Type type = fixer.GetType();

                    ExportCodeFixProviderAttribute attribute = type.GetCustomAttribute <ExportCodeFixProviderAttribute>();

                    yield return(new XElement("Fixer",
                                              new XAttribute("Name", type.FullName),
                                              new XElement("Languages", attribute.Languages.Select(f => new XElement("Language", f))),
                                              new XElement("FixableDiagnostics", fixer.FixableDiagnosticIds
                                                           .Distinct()
                                                           .OrderBy(f => f)
                                                           .Select(f => new XElement("Id", f))),
                                              CreateFixAllProviderElement(fixer)));
                }

                XElement CreateFixAllProviderElement(CodeFixProvider fixer)
                {
                    FixAllProvider fixAllProvider = fixer.GetFixAllProvider();

                    if (fixAllProvider != null)
                    {
                        return(new XElement("FixAllProvider", new XAttribute("Name", fixAllProvider.GetType().FullName)));
                    }

                    return(null);
                }
            }
        }