Beispiel #1
0
        private void RunRulesAndVerifyResult(CodeAnalysisService service, Action <CodeAnalysisResult, string> verify)
        {
            CodeAnalysisResult analysisResult = service.Analyze(this.ModelForAnalysis);

            // Only considering analysis errors for now - might want to expand to initialization and suppression errors in the future
            this.DumpErrors(analysisResult.AnalysisErrors);

            string problemsString = this.DumpProblemsToString(analysisResult.Problems);


            verify(analysisResult, problemsString);
        }
        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");
        }