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));
        }
Example #2
0
        static void Main(string[] args)
        {
            MetricConfiguration metricConfiguration;

            try
            {
                var arguments = new Docopt().Apply(USAGE, args, version: "CodeMetrics Extractor 2.0");

                metricConfiguration = new MetricConfiguration(arguments);
            }
            catch (Exception ex)
            {
                WriteLine(ex.Message);
                return;
            }

            var stopwatch = Stopwatch.StartNew();
            var result    = RunCodeMetrics(metricConfiguration).Result;

            stopwatch.Stop();

            WriteLine($"All projects measure(Elapsed: {stopwatch.Elapsed}), creating report");
            var ignoredNamespaces = metricConfiguration.IgnoredNamespaces;
            var namespaceMetrics  = result.Where(nm => !ignoredNamespaces.Contains(nm.Name)).ToList();

            var types = namespaceMetrics
                        .Where(n => !metricConfiguration.IgnoredNamespaces.Contains(n.Name))
                        .SelectMany(x => x.TypeMetrics, (nm, t) => new TypeMetricWithNamespace(t).WithNamespace(nm.Name))
                        .Where(x => !metricConfiguration.IgnoredTypes.Contains(x.FullName))
                        .Distinct()
                        .ToArray();

            const int MAX_LINES_OF_CODE_ON_METHOD = 30;

            var metodos = types.SelectMany(x => x.MemberMetrics, (type, member) => new MetodoComTipo {
                Tipo = type, Metodo = member
            }).ToList();

            var metodosRuins = GetMetodosRuins(metodos, MAX_LINES_OF_CODE_ON_METHOD);

            var resultadoGeral = CreateEstadoDoProjeto(types, metodosRuins, metodos.Count, namespaceMetrics);

            var reportPath = GenerateReport(resultadoGeral, metricConfiguration.SolutionDirectory);

            WriteLine("Report generated in: {0}", reportPath);
#if DEBUG
            Process.Start(reportPath);
#endif
        }
Example #3
0
        private static void SendToS3(MetricConfiguration metricConfiguration, string reportPath, string reportDirectory, out string pathOnS3)
        {
            IAmazonS3Integration amazonS3Integration = null;
            if (metricConfiguration.AwsAccessKey == null)
                throw new ArgumentNullException("AwsAccessKey", "When SendToS3 is true, AwsAccesskey is required");
            if (metricConfiguration.AwsSecretKey == null)
                throw new ArgumentNullException("AwsSecretKey", "When SendToS3 is true, AwsSecretKey is required");
            if (metricConfiguration.BucketS3 == null)
                throw new ArgumentNullException("BucketS3", "When SendToS3 is true, BucketS3 is required");
            if (metricConfiguration.PathOnBucketS3 == null)
                throw new ArgumentNullException("PathOnBucketS3", "When SendToS3 is true, PathOnBucketS3 is required");

            amazonS3Integration = new AmazonS3Integration(metricConfiguration.AwsAccessKey, metricConfiguration.AwsSecretKey);
            pathOnS3 = Path.Combine(string.Format("{0}/metrics-{1}", metricConfiguration.PathOnBucketS3, string.Format("{0:yy-MM-dd_HH-mm}", DateTime.Now)));
            amazonS3Integration.SendDocument(reportPath, metricConfiguration.BucketS3, pathOnS3);
            amazonS3Integration.SendDocument(string.Format(@"{0}\site.css", reportDirectory), metricConfiguration.BucketS3, pathOnS3, "site.css", true);
        }
Example #4
0
        private static void SendToSlack(MetricConfiguration metricConfiguration, string pathOnS3)
        {
            ISlackIntegration slackIntegration = null;
            IAmazonS3Integration amazonS3Integration = null;

            if (metricConfiguration.SlackChannel == null)
                throw new ArgumentNullException("SendToS3", "When SendSignUrlToSlack is true, SendToS3 enabled is required");
            if (metricConfiguration.SlackChannel == null)
                throw new ArgumentNullException("SlackChannel", "When SendSignUrlToSlack is true, SlackChannel is required");
            if (metricConfiguration.SlackMessage == null)
                throw new ArgumentNullException("SlackMessage", "When SendSignUrlToSlack is true, SlackMessage is required");
            if (metricConfiguration.SlackToken == null)
                throw new ArgumentNullException("SlackToken", "When SendSignUrlToSlack is true, SlackToken is required");
            if (metricConfiguration.SlackUserName == null)
                throw new ArgumentNullException("SlackUserName", "When SendSignUrlToSlack is true, SlackUserName is required");

            amazonS3Integration = new AmazonS3Integration(metricConfiguration.AwsAccessKey, metricConfiguration.AwsSecretKey);
            var signedUrl = amazonS3Integration.SignUrl(pathOnS3, "index.html", metricConfiguration.BucketS3,
                metricConfiguration.SlackUrlExpirationInSeconds.GetValueOrDefault(86400));

            var dtExpirationLink =
                DateTime.Now.AddSeconds(metricConfiguration.SlackUrlExpirationInSeconds.GetValueOrDefault(86400));

            Console.WriteLine("Signed Url Metrics Report generated. Date Expiration: {0:u}.", dtExpirationLink);

            slackIntegration = new SlackIntegration(metricConfiguration.SlackToken);
            slackIntegration.PostMessage(metricConfiguration.SlackChannel, string.Format("{0}{1}. Link expire at {2}: ", metricConfiguration.SlackMessage, signedUrl, dtExpirationLink),
                metricConfiguration.SlackUserName);


            Console.WriteLine("Link Url Metrics Report sent to Slack: {0}. Dt Expiration", signedUrl);
        }
Example #5
0
        private static async Task<IEnumerable<INamespaceMetric>> RunCodeMetrics(MetricConfiguration configuration)
        {
            Console.WriteLine("Loading Solution");
            var solutionProvider = new SolutionProvider();
            var solution = await solutionProvider.Get(configuration.Solution).ConfigureAwait(false);
            Console.WriteLine("Solution loaded");

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

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

            var metrics = new List<IEnumerable<INamespaceMetric>>();
            var metricsCalculator = new CodeMetricsCalculator(new CalculationConfiguration
            {
                NamespacesIgnored = configuration.IgnoredNamespaces,
                TypesIgnored = configuration.IgnoredTypes
            });
            foreach (var project in projects)
            {
                var calculate = await metricsCalculator.Calculate(project, solution);
                metrics.Add(calculate);
            }

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