コード例 #1
0
        private List <MetricProject> GetSolutionMetric(List <Project> projects, Solution solution, CodeMetricSetting setting)
        {
            var metricCalculator = new CodeMetricsCalculator();
            var depo             = new List <MetricProject>(projects.Count);

            foreach (var prj in projects)
            {
                if (setting.ExcludedProjects.Any(x => x == prj.Name))
                {
                    continue;
                }

                var tmpProject = new MetricProject();
                tmpProject.ProjectName  = prj.Name;
                tmpProject.AssemblyName = prj.AssemblyName;
                try
                {
                    tmpProject.Metric = (metricCalculator.Calculate(prj, solution).Result).ToList();

                    depo.Add(tmpProject);
                }
                catch (Exception ex)
                {
                    string errMsg = string.Format("ERROR : [{0}] project has errors. Could not be processed. Check your project with SyleCop. Correct SA1603 error. ErrorDetail = {1}", prj.Name, ex.ToString());
                    WriteDetail(errMsg);
                    return(null);
                }
            }

            return(depo);
        }
コード例 #2
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($"{projects.Count} projects");
            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));
        }
コード例 #3
0
        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
ファイル: CSharpProbe.cs プロジェクト: skayred/CSharpProbe
        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);
                }
            }
        }
        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>());
            }
        }
コード例 #6
0
 public GivenACodeMetricsCalculator()
 {
     _analyzer = new CodeMetricsCalculator();
 }
コード例 #7
0
 public GivenACodeMetricsCalculatorAndReadingDocumentation()
 {
     _analyzer = new CodeMetricsCalculator(new TypeDocumentationFactory(), new MemberDocumentationFactory());
 }
コード例 #8
0
 public GivenACodeMetricsCalculator()
 {
     _analyzer = new CodeMetricsCalculator();
 }
コード例 #9
0
 public GivenACodeMetricsCalculatorAndReadingDocumentation()
 {
     _analyzer = new CodeMetricsCalculator(new TypeDocumentationFactory(), new MemberDocumentationFactory());
 }
コード例 #10
0
 public GivenACodeMetricsCalculator()
 {
     _calculator = new CodeMetricsCalculator(new TypeDocumentationFactory(), new MemberDocumentationFactory());
 }
コード例 #11
0
			public void Setup()
			{
				_calculator = new CodeMetricsCalculator(new TypeDocumentationFactory(), new MemberDocumentationFactory());
			}
コード例 #12
0
 public void Setup()
 {
     _analyzer = new CodeMetricsCalculator(new TypeDocumentationFactory(), new MemberDocumentationFactory());
 }
コード例 #13
0
 public GivenACodeMetricsCalculator()
 {
     _calculator = new CodeMetricsCalculator(new TypeDocumentationFactory(), new MemberDocumentationFactory());
 }