Beispiel #1
0
        /// <summary>
        /// Sets up the service and disables all rules except the rule you wish to test.
        ///
        /// If you want all rules to run then do not change the
        /// <see cref="CodeAnalysisRuleSettings.DisableRulesNotInSettings"/> flag, as it is set to "false" by default which
        /// ensures that all rules are run.
        ///
        /// To run some (but not all) of the built-in rules then you could query the
        /// <see cref="CodeAnalysisService.GetRules"/> method to get a list of all the rules, then set their
        /// <see cref="RuleConfiguration.Enabled"/> and other flags as needed, or alternatively call the
        /// <see cref="CodeAnalysisService.ApplyRuleSettings"/> method to apply whatever rule settings you wish
        ///
        /// </summary>
        private CodeAnalysisService CreateCodeAnalysisService(string ruleIdToRun)
        {
            CodeAnalysisServiceFactory factory = new CodeAnalysisServiceFactory();
            var ruleSettings = new CodeAnalysisRuleSettings()
            {
                new RuleConfiguration(ruleIdToRun)
            };

            ruleSettings.DisableRulesNotInSettings = true;
            CodeAnalysisService service = factory.CreateAnalysisService(this.ModelForAnalysis.Version, new CodeAnalysisServiceSettings()
            {
                RuleSettings = ruleSettings
            });

            this.DumpErrors(service.GetRuleLoadErrors());

            Assert.IsTrue(service.GetRules().Any((rule) => rule.RuleId.Equals(ruleIdToRun, StringComparison.OrdinalIgnoreCase)),
                          "Expected rule '{0}' not found by the service", ruleIdToRun);
            return(service);
        }
        public void Create(ReportRequest request)
        {
            var fileName = Path.GetFileNameWithoutExtension(request.InputPath);

            #region Loading dacpac
            SendNotification($"Loading {request.FileName}.dacpac");
            var sw = Stopwatch.StartNew();

            //load the dacpac
            TSqlModel model = TSqlModel.LoadFromDacpac(
                request.InputPath
                , new ModelLoadOptions()
            {
                LoadAsScriptBackedModel = true,
                ModelStorageType        = Microsoft.SqlServer.Dac.DacSchemaModelStorageType.Memory
            });
            CodeAnalysisServiceFactory factory = new CodeAnalysisServiceFactory();
            CodeAnalysisService        service = factory.CreateAnalysisService(model);

            //surpress rules
            service.SetProblemSuppressor(request.Suppress);
            sw.Stop();
            SendNotification($"Loading {request.FileName}.dacpac complete, elapsed: {sw.Elapsed.ToString(@"hh\:mm\:ss")}");
            #endregion

            #region Running rules
            SendNotification("Running rules");
            sw = Stopwatch.StartNew();
            //process non-suppressed rules
            var result = service.Analyze(model);

            if (!result.AnalysisSucceeded)
            {
                foreach (var err in result.AnalysisErrors)
                {
                    SendNotification(err.Exception.Message, NotificationType.Error);
                }
                return;
            }
            sw.Stop();
            SendNotification($"Running rules complete, elapsed: {sw.Elapsed.ToString(@"hh\:mm\:ss")}");
            #endregion

            #region Writing report
            //create report object
            var report = new Report(
                request.Solution,
                GetIssueTypes(service.GetRules(), request.SuppressIssueTypes).ToList(),
                request.FileName,
                GetProblems(result.Problems).ToList());

            SendNotification("Writing report");
            sw = Stopwatch.StartNew();
            //write out the xml
            switch (request.ReportOutputType)
            {
            case ReportOutputType.XML:
                var outFileName = GetOutputFileName(request, request.ReportOutputType);
                SerializeReport(report, outFileName);
                var outDir   = GetOutputDirectory(request);
                var xlstPath = Path.Combine(outDir, "RulesTransform.xslt");
                if (!File.Exists(xlstPath))
                {
                    File.WriteAllText(xlstPath, Resources.RulesTransform);
                }

                var xPathDoc     = new XPathDocument(outFileName);
                var xslTransform = new XslCompiledTransform();
                using (var xmlWriter = new XmlTextWriter(Path.Combine(outDir, $"{request.FileName}.html"), null))
                {
                    xslTransform.Load(xlstPath);
                    xslTransform.Transform(xPathDoc, null, xmlWriter);
                }
                break;

            case ReportOutputType.CSV:
                SerializeReportToCSV(report, GetOutputFileName(request, request.ReportOutputType));
                break;

            default:
                SendNotification($"Invalid report type: {request.ReportOutputType}");
                break;
            }
            sw.Stop();
            SendNotification($"Writing report complete, elapsed: {sw.Elapsed.ToString(@"hh\:mm\:ss")}");
            #endregion


            SendNotification($"Done with {request.FileName}.dacpac");
        }