Пример #1
0
        /// <summary>
        ///     CodeAnalyzer Constructor
        ///     This constructs the CodeAnalyzer class with a given
        ///     file extension pattern (e.g.: .cs)
        /// </summary>
        /// <param name="fileExtension">file extension pattern</param>
        protected CodeAnalyzer(string fileExtension = ".*")
        {
            var patternsJSON = File.ReadAllText(DefaultPatternsFile);

            Patterns = JsonConvert.DeserializeObject <CodacyPatterns>(patternsJSON);

            if (File.Exists(DefaultConfigFile))
            {
                var configJSON = File.ReadAllText(DefaultConfigFile);
                Config = JsonConvert.DeserializeObject <CodacyConfiguration>(configJSON);

                CurrentTool = Array.Find(Config.Tools, tool => tool.Name == Patterns.Name);

                if (Config.Files is null)
                {
                    Config.Files = GetSourceFiles(DefaultSourceFolder, fileExtension);
                }

                if (!(CurrentTool.Patterns is null))
                {
                    var patternIds = new List <string>();
                    foreach (var pattern in CurrentTool.Patterns)
                    {
                        patternIds.Add(pattern.PatternId);
                    }

                    PatternIds = patternIds.ToImmutableList();
                }
            }
            else
            {
                Config = new CodacyConfiguration
                {
                    Files = GetSourceFiles(DefaultSourceFolder, fileExtension),
                    Tools = new[]
                    {
                        new Tool
                        {
                            Name = Patterns.Name
                        }
                    }
                };
            }
        }
Пример #2
0
        private static void Main()
        {
            var sonarVersion = File.ReadAllText(".SONAR_VERSION")
                               .TrimEnd(Environment.NewLine.ToCharArray())
                               .Split('.');

            Directory.CreateDirectory(docsFolder);
            Directory.CreateDirectory(descriptionFolder);

            var patternsFile = new CodacyPatterns
            {
                Name     = "Sonar C#",
                Version  = string.Format("{0}.{1}", sonarVersion),
                Patterns = new List <Pattern>()
            };

            var descriptions = new List <Description>();

            var doc = XDocument.Load(@".res/sonar-csharp-rules.xml");

            foreach (var rule in doc.Root.Elements())
            {
                var lvl        = LevelHelper.ToLevel((rule.Element("severity") ?? new XElement("undefined")).Value);
                var parameters = rule.Elements().Where(e => e.Name == "param");
                var pattern    = new Pattern
                {
                    PatternId  = rule.Element("key").Value,
                    Level      = lvl,
                    Category   = CategoryHelper.ToCategory(rule, lvl),
                    Parameters = parameters.Select(param => new Parameter
                    {
                        Name    = param.Element("key").Value,
                        Default = param.Element("defaultValue")?.Value ?? ""
                    }).ToArray()
                };

                var description = new Description
                {
                    PatternId  = pattern.PatternId,
                    Title      = rule.Element("name").Value,
                    Parameters = parameters.Select(param => new DescriptionParameter
                    {
                        Name        = param.Element("key").Value,
                        Description = param.Element("description").Value
                    }).ToArray(),
                    TimeToFix = TTFHelper.ToCodacyTimeToFix(rule.Element("remediationFunctionBaseEffort")?.Value ?? "")
                };

                patternsFile.Patterns.Add(pattern);
                descriptions.Add(description);

                var descriptionHTML = rule.Element("description").Value;
                var descriptionMD   = new Converter().Convert(descriptionHTML);
                File.WriteAllText(descriptionFolder + pattern.PatternId + ".md", descriptionMD);
            }

            var patternsJson = JsonConvert.SerializeObject(patternsFile,
                                                           Formatting.Indented,
                                                           new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            var descriptionJson = JsonConvert.SerializeObject(descriptions,
                                                              Formatting.Indented,
                                                              new JsonSerializerSettings
            {
                NullValueHandling = NullValueHandling.Ignore
            });

            File.WriteAllText("docs/patterns.json", patternsJson);
            File.WriteAllText("docs/description/description.json", descriptionJson);
        }
Пример #3
0
        public static int Main(string[] args)
        {
            Directory.CreateDirectory(docsFolder);
            Directory.CreateDirectory(descriptionFolder);

            var files = Directory.GetFiles(rulesDocumentation);

            foreach (var file in files)
            {
                var fileName = Path.GetFileName(file);
                var destFile = Path.Combine(descriptionFolder, fileName);
                File.Copy(file, destFile, true);
            }


            var tsqllintVersion = XDocument.Load(@"tsqllint/source/TSQLLint.Console/TSQLLint.Console.csproj").Root
                                  .Elements("PropertyGroup")
                                  .SelectMany(pg => pg.Elements())
                                  .First(e => e.Name == "Version")
                                  .Value;

            var patternsFile = new CodacyPatterns
            {
                Name     = "tsqllint",
                Version  = tsqllintVersion,
                Patterns = new List <Pattern>()
            };

            var descriptions = new CodacyDescription();

            var types = Assembly
                        .LoadFile(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + @"/TSQLLint.Infrastructure.dll")
                        .GetTypes()
                        .Where(t => t.Namespace == "TSQLLint.Infrastructure.Rules" &&
                               t.GetInterfaces().Contains(typeof(ISqlRule)) &&
                               t.Name.EndsWith("Rule")
                               );

            foreach (var ruleType in types)
            {
                var instance = Activator.CreateInstance(ruleType,
                                                        (Action <string, string, int, int>)((_, __, ___, ____) => { }));

                var description = new Description();

                foreach (var prop in ruleType.GetProperties())
                {
                    if (prop.Name == "RULE_NAME")
                    {
                        var patternId = (string)prop.GetValue(instance);
                        var enabled   = DefaultPatterns.Patterns.Contains(patternId);
                        var pattern   = new Pattern(patternId,
                                                    LevelHelper.ToLevel(patternId),
                                                    CategoryHelper.ToCategory(patternId),
                                                    enabled: enabled);
                        patternsFile.Patterns.Add(pattern);

                        description.PatternId = patternId;
                    }
                    else if (prop.Name == "RULE_TEXT")
                    {
                        description.Title = (string)prop.GetValue(instance);
                    }
                }

                descriptions.Add(description);
            }

            File.WriteAllText(docsFolder + @"/patterns.json", patternsFile.ToString(true) + Environment.NewLine);
            File.WriteAllText(descriptionFolder + @"/description.json",
                              descriptions.ToString(true) + Environment.NewLine);

            return(0);
        }