示例#1
0
            public async Task WhenTypeHasDocumentationThenReadsDocumentation()
            {
                var tree    = CSharpSyntaxTree.ParseText(Snippet);
                var metrics = await _analyzer.Calculate(new[] { tree });

                Assert.NotNull(metrics);
            }
            public async Task CanCalculateMetricsForSnippet()
            {
                const string Snippet = @"namespace SomeNamespace
{
	public class Something {
		publis string Name { get; set; }
	}
}
";
                var          tree    = CSharpSyntaxTree.ParseText(Snippet);
                var          metrics = await _analyzer.Calculate(new[] { tree });

                Assert.NotNull(metrics);
            }
        private static async Task <List <FileMetric> > GetFileMetrics()
        {
            Dictionary <string, FileMetric> fileMetricsDictionary = new Dictionary <string, FileMetric>();
            int i = 0;

            using (var workspace = MSBuildWorkspace.Create())
            {
                Solution solution = await workspace.OpenSolutionAsync(SolutionFilePath);

                foreach (var project in solution.Projects)
                {
                    Console.WriteLine($"{Environment.NewLine}-----------Calculate project {++i} {project.Name}----------{Environment.NewLine}");
                    CodeMetricsCalculator          metricsCalculator = new CodeMetricsCalculator();
                    IEnumerable <INamespaceMetric> metrics           = await metricsCalculator.Calculate(project, solution);

                    foreach (INamespaceMetric namespaceMetric in metrics)
                    {
                        foreach (ITypeMetric metric in namespaceMetric.TypeMetrics)
                        {
                            if (metric.MemberMetrics.Select(x => x.CodeFile).Where(x => !string.IsNullOrEmpty(x)).Distinct().Count() > 1)
                            {
                                Console.WriteLine("More than one file name...");
                            }

                            var codeFiles = metric.
                                            MemberMetrics
                                            .Where(x => !string.IsNullOrEmpty(x.CodeFile))
                                            .Select(x => x.CodeFile)
                                            .Distinct();

                            foreach (var codeFile in codeFiles)
                            {
                                Document document = project
                                                    .Documents
                                                    .SingleOrDefault(x => x.FilePath
                                                                     .EndsWith(codeFile));

                                if (!fileMetricsDictionary.ContainsKey(document.FilePath))
                                {
                                    fileMetricsDictionary[document.FilePath] = GetFileMetric(metric, document.FilePath);
                                }
                                else
                                {
                                    Console.WriteLine($"{document.FilePath} already in the dictionary");
                                    PrintMetric(fileMetricsDictionary[document.FilePath]);
                                    PrintMetric(GetFileMetric(metric, document.FilePath));
                                }
                            }
                        }
                    }
                }
            }

            return(fileMetricsDictionary
                   .Select(x => x.Value)
                   .ToList());
        }
示例#4
0
        private static async Task Run(Options opts)
        {
            try
            {
                Console.WriteLine("Loading Solution");

                var workspace = MSBuildWorkspace.Create();
                workspace.WorkspaceFailed += (s, e) =>
                {
                    Console.WriteLine($"Workspace failed with: {e.Diagnostic}");
                };
                var solution = await workspace.OpenSolutionAsync(opts.Path);

                var projects = string.IsNullOrEmpty(opts.Project) ? solution.Projects.Where(p => p.FilePath.EndsWith("csproj")) : solution.Projects.Where(p => p.Name == opts.Project);

                Console.WriteLine("Loading metrics, wait it may take a while.");
                var metricsCalculator = new CodeMetricsCalculator();
                var calculateTasks    = projects.Select(p => metricsCalculator.Calculate(p, solution));
                var metrics           = (await Task.WhenAll(calculateTasks)).SelectMany(nm => nm);

                var currentAmount   = 0;
                var maintainability = 0.0;
                var modules         = new List <ResultModule>();
                var loc             = 0;

                foreach (var metric in metrics)
                {
                    currentAmount  += 1;
                    maintainability = maintainability + (metric.MaintainabilityIndex - maintainability) / (currentAmount + 1.0);
                    loc            += metric.LinesOfCode;

                    if (!opts.MinMode)
                    {
                        modules.Add(new ResultModule(metric.Name, metric.MaintainabilityIndex));
                    }
                }

                var result = new Result(
                    maintainability,
                    opts.MinMode ? 0 : loc,
                    0,
                    opts.Revision,
                    opts.RevisionDate,
                    modules
                    );

                UploadResult(opts, result);
            } catch (ReflectionTypeLoadException ex)
            {
                foreach (var item in ex.LoaderExceptions)
                {
                    Console.WriteLine(item.Message);
                }
            }
        }
            public void WhenCalculatingMetricsForCodeSnippetThenReturnsMetrics()
            {
                var snippet = @"public int GetValue(int x)
{
	if(x% 2 == 0)
	{
		return x-2;
	}

	return x;
}";
                var tree    = CSharpSyntaxTree.ParseText(snippet);
                var metrics = _calculator.Calculate(new[] { tree });

                Assert.NotNull(metrics);
            }
        private static IEnumerable <INamespaceMetric> GetFileMetric(SyntaxTree syntaxTree)
        {
            List <SyntaxTree> syntaxTrees = new List <SyntaxTree>()
            {
                syntaxTree
            };
            CodeMetricsCalculator metricsCalculator = new CodeMetricsCalculator();

            try
            {
                return(metricsCalculator.Calculate(syntaxTrees).Result);
            }
            catch (Exception)
            {
                return(new List <INamespaceMetric>());
            }
        }
示例#7
0
        private static async Task <IEnumerable <INamespaceMetric> > RunCodeMetrics(MetricConfiguration configuration)
        {
            WriteLine("Loading Solution");
            var solutionProvider = new SolutionProvider();
            var solution         = await solutionProvider.Get(configuration.Solution).ConfigureAwait(false);

            WriteLine("Solution loaded");

            var projects = solution.Projects.Where(p => !configuration.IgnoredProjects.Contains(p.Name)).ToList();

            WriteLine("Loading metrics, wait it may take a while.");

            var metrics           = new List <IEnumerable <INamespaceMetric> >();
            var metricsCalculator = new CodeMetricsCalculator();

            foreach (var project in projects)
            {
                var calculate = await metricsCalculator.Calculate(project, solution).ConfigureAwait(false);

                metrics.Add(calculate);
            }

            return(metrics.SelectMany(nm => nm));
        }