private void ValidateMetricsList(CodeMetricsRunner runner,  TaskMetricList ml)
 {
     foreach (TaskMetric p in ml)
     {
         if (String.IsNullOrEmpty(p.ProviderName))
             throw new MSBuildCodeMetricsTaskException("ProviderName must be informed in Metrics property");
         if (!_providers.ContainsKey(p.ProviderName))
             throw new MSBuildCodeMetricsTaskException("Invalid provider name in Metrics property: ProviderName: " + p.ProviderName);
         if (String.IsNullOrEmpty(p.MetricName))
             throw new MSBuildCodeMetricsTaskException("Metric name in property Metrics can't be null (ProviderName: " + p.ProviderName);
         if (!runner.IsMetricRegistered(p.ProviderName, p.MetricName))
             throw new MSBuildCodeMetricsTaskException("Provider " + p.ProviderName + " doesn't know how to handle metric " + p.MetricName);
     }
 }
 private MSBuildCodeMetricsReport GenerateReport(CodeMetricsRunner runner, TaskMetricList ml)
 {
     MSBuildCodeMetricsReport report = runner.GenerateReport(ml.ToMetricList(), ShowSummaryReport, ShowDetailsReport);
     if (FileOutput)
         GenerateFileOutput(report);
     if (ShowConsoleOutput)
         GenerateConsoleOutput(report);
     return report;
 }
        private void RegisterProviders(CodeMetricsRunner runner, ILogger logger, IProcessExecutor processExecutor)
        {
            if (Providers.Length <= 0)
                throw new MSBuildCodeMetricsTaskException("At least one Provider must me informed in Providers property");

            foreach (ITaskItem item in Providers)
            {
                string typeName = item.ItemSpec;
                ICodeMetricsProvider provider = CreateProviderInstance(typeName, item);
                _providers.Add(provider.Name, provider);

                runner.RegisterProvider(provider);
                if (provider is ILoggableCodeMetricsProvider)
                    ((ILoggableCodeMetricsProvider)provider).Logger = logger;
                if (provider is IProcessExecutorCodeMetricsProvider)
                    ((IProcessExecutorCodeMetricsProvider)provider).ProcessExecutor = processExecutor;
            }
        }
        /// <summary>
        /// Called from MSBuild when the task executes
        /// </summary>
        /// <returns>boolean indicating if the execution was sucessful</returns>
        public override bool Execute()
        {
            try
            {
                TaskMetricList ml = ParseMetrics();

                var logger = new MSBuildLogger(Log);
                var processExecutor = new ProcessExecutor(logger);
                CodeMetricsRunner runner = new CodeMetricsRunner(logger);
                RegisterProviders(runner, logger, processExecutor);
                ValidateMetricsList(runner, ml);

                runner.ComputeMetrics(ml.ToComputeMetricsParameterList());
                var report = GenerateReport(runner, ml);
                RunBuildFailChecks(report, ml);
                return true;
            }
            catch (MSBuildCodeMetricsTaskException e)
            {
                Log.LogError(e.Message);
                return false;
            }
        }