Ejemplo n.º 1
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(CommandStatus.Success);
        }