private HashSet <Skimmer <TContext> > CreateSkimmers(TContext context)
        {
            IEnumerable <Skimmer <TContext> > skimmers;
            HashSet <Skimmer <TContext> >     result = new HashSet <Skimmer <TContext> >();

            try
            {
                skimmers = CompositionUtilities.GetExports <Skimmer <TContext> >(DefaultPlugInAssemblies);

                SupportedPlatform currentOS = GetCurrentRunningOS();
                foreach (Skimmer <TContext> skimmer in skimmers)
                {
                    if (skimmer.SupportedPlatforms.HasFlag(currentOS))
                    {
                        result.Add(skimmer);
                    }
                    else
                    {
                        Warnings.LogUnsupportedPlatformForRule(context, skimmer.Name, skimmer.SupportedPlatforms, currentOS);
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.LogExceptionInstantiatingSkimmers(context, DefaultPlugInAssemblies, ex);
                ThrowExitApplicationException(context, ExitReason.UnhandledExceptionInstantiatingSkimmers, ex);
            }

            if (result.Count == 0)
            {
                Errors.LogNoRulesLoaded(context);
                ThrowExitApplicationException(context, ExitReason.NoRulesLoaded);
            }
            return(result);
        }
        protected virtual ISet <Skimmer <TContext> > CreateSkimmers(TOptions analyzeOptions, TContext context)
        {
            IEnumerable <Skimmer <TContext> > skimmers;
            var result = new SortedSet <Skimmer <TContext> >(SkimmerIdComparer <TContext> .Instance);

            try
            {
                skimmers = CompositionUtilities.GetExports <Skimmer <TContext> >(RetrievePluginAssemblies(DefaultPluginAssemblies, analyzeOptions.PluginFilePaths));

                SupportedPlatform currentOS = GetCurrentRunningOS();
                foreach (Skimmer <TContext> skimmer in skimmers)
                {
                    if (skimmer.SupportedPlatforms.HasFlag(currentOS))
                    {
                        result.Add(skimmer);
                    }
                    else
                    {
                        Warnings.LogUnsupportedPlatformForRule(context, skimmer.Name, skimmer.SupportedPlatforms, currentOS);
                    }
                }
            }
            catch (Exception ex)
            {
                Errors.LogExceptionInstantiatingSkimmers(context, DefaultPluginAssemblies, ex);
                ThrowExitApplicationException(context, ExitReason.UnhandledExceptionInstantiatingSkimmers, ex);
            }

            if (result.Count == 0)
            {
                Errors.LogNoRulesLoaded(context);
                ThrowExitApplicationException(context, ExitReason.NoRulesLoaded);
            }
            return(result);
        }
Exemple #3
0
        public override int Run(ExportRulesMetadataOptions exportOptions)
        {
            int result = FAILURE;

            try
            {
                ImmutableArray <ReportingDescriptor> skimmers = CompositionUtilities.GetExports <ReportingDescriptor>(DefaultPlugInAssemblies);

                string format         = "";
                string outputFilePath = exportOptions.OutputFilePath;
                string extension      = Path.GetExtension(outputFilePath);

                switch (extension)
                {
                case (".json"):
                case (SarifConstants.SarifFileExtension):
                {
                    format = "SARIF";
                    OutputSarifRulesMetadata(outputFilePath, skimmers);
                    break;
                }

                case (".xml"):
                {
                    format = "SonarQube";
                    OutputSonarQubeRulesMetadata(outputFilePath, skimmers);
                    break;
                }

                case (".md"):
                {
                    format = "Markdown";
                    OutputMarkdownRulesMetadata(outputFilePath, skimmers);
                    break;
                }

                default:
                {
                    throw new InvalidOperationException("Unrecognized output file extension: " + extension);
                }
                }

                result = SUCCESS;
                Console.WriteLine(format + " rules metadata exported to: " + Path.GetFullPath(outputFilePath));
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }

            return(result);
        }
Exemple #4
0
        public override int Run(ExportRulesDocumentationOptions options)
        {
            try
            {
                var rules = CompositionUtilities.GetExports <Skimmer <TContext> >(DefaultPlugInAssemblies).ToList();

                var sb = new StringBuilder();
                sb.AppendLine($"# Rules{Environment.NewLine}");

                foreach (Skimmer <TContext> rule in rules)
                {
                    BuildRule(rule, sb);
                }

                _fileSystem.WriteAllText(options.OutputFilePath, sb.ToString());
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return(FAILURE);
            }

            return(SUCCESS);
        }
        public override int Run(ExportConfigurationOptions exportOptions)
        {
            int result = FAILURE;

            try
            {
                PropertiesDictionary allOptions = new PropertiesDictionary();

                // The export command could be updated in the future to accept an arbitrary set
                // of analyzers for which to build an options XML file suitable for configuring them.
                ImmutableArray <IOptionsProvider> providers = CompositionUtilities.GetExports <IOptionsProvider>(DefaultPlugInAssemblies);
                foreach (IOptionsProvider provider in providers)
                {
                    IOption sampleOption = null;

                    // Every analysis options provider has access to the following default configuration knobs
                    foreach (IOption option in provider.GetOptions())
                    {
                        sampleOption = sampleOption ?? option;
                        allOptions.SetProperty(option, option.DefaultValue, cacheDescription: true);
                    }
                }

                IEnumerable <ReportingDescriptor> rules;
                rules = CompositionUtilities.GetExports <ReportingDescriptor>(DefaultPlugInAssemblies);

                // This code injects properties that are provided for every rule instance.
                foreach (ReportingDescriptor rule in rules)
                {
                    object objectResult;
                    PropertiesDictionary properties;

                    string ruleOptionsKey = rule.Id + "." + rule.Name + ".Options";

                    if (!allOptions.TryGetValue(ruleOptionsKey, out objectResult))
                    {
                        objectResult = allOptions[ruleOptionsKey] = new PropertiesDictionary();
                    }
                    properties = (PropertiesDictionary)objectResult;

                    foreach (IOption option in DefaultDriverOptions.Instance.GetOptions())
                    {
                        properties.SetProperty(option, option.DefaultValue, cacheDescription: true, persistToSettingsContainer: false);
                    }
                }

                string extension = Path.GetExtension(exportOptions.OutputFilePath);

                if (extension.Equals(".xml", StringComparison.OrdinalIgnoreCase))
                {
                    allOptions.SaveToXml(exportOptions.OutputFilePath);
                }
                else if (extension.Equals(".json", StringComparison.OrdinalIgnoreCase))
                {
                    allOptions.SaveToJson(exportOptions.OutputFilePath);
                }
                else if (exportOptions.FileFormat == FileFormat.Xml)
                {
                    allOptions.SaveToXml(exportOptions.OutputFilePath);
                }
                else
                {
                    allOptions.SaveToJson(exportOptions.OutputFilePath);
                }

                Console.WriteLine("Configuration file saved to: " + Path.GetFullPath(exportOptions.OutputFilePath));

                result = SUCCESS;
            }
            catch (Exception ex)
            {
                Console.Error.WriteLine(ex.ToString());
            }

            return(result);
        }